diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-07-31 08:15:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-31 08:15:23 -0700 |
commit | 09b49c26ddfdb20ced61e7dfd4192e750ce40449 (patch) | |
tree | 1288d069cc8a199b8eb3b858935dffd377ee3d2d /vendor/github.com/go-redis/redis/iterator.go | |
parent | 6f4e38d129ffaf469d40fc8596d3957ee94d21e9 (diff) | |
download | chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.tar.gz chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.tar.bz2 chat-09b49c26ddfdb20ced61e7dfd4192e750ce40449.zip |
PLT-5308 Caching layer part 2 (#6973)
* Adding Reaction store cache layer example
* Implementing reaction store in new caching system.
* Redis for reaction store
* Adding redis library
* Adding invalidation for DeleteAllWithEmojiName and other minor enhancements
Diffstat (limited to 'vendor/github.com/go-redis/redis/iterator.go')
-rw-r--r-- | vendor/github.com/go-redis/redis/iterator.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/vendor/github.com/go-redis/redis/iterator.go b/vendor/github.com/go-redis/redis/iterator.go new file mode 100644 index 000000000..5d4bedfe5 --- /dev/null +++ b/vendor/github.com/go-redis/redis/iterator.go @@ -0,0 +1,73 @@ +package redis + +import "sync" + +// ScanIterator is used to incrementally iterate over a collection of elements. +// It's safe for concurrent use by multiple goroutines. +type ScanIterator struct { + mu sync.Mutex // protects Scanner and pos + cmd *ScanCmd + pos int +} + +// Err returns the last iterator error, if any. +func (it *ScanIterator) Err() error { + it.mu.Lock() + err := it.cmd.Err() + it.mu.Unlock() + return err +} + +// Next advances the cursor and returns true if more values can be read. +func (it *ScanIterator) Next() bool { + it.mu.Lock() + defer it.mu.Unlock() + + // Instantly return on errors. + if it.cmd.Err() != nil { + return false + } + + // Advance cursor, check if we are still within range. + if it.pos < len(it.cmd.page) { + it.pos++ + return true + } + + for { + // Return if there is no more data to fetch. + if it.cmd.cursor == 0 { + return false + } + + // Fetch next page. + if it.cmd._args[0] == "scan" { + it.cmd._args[1] = it.cmd.cursor + } else { + it.cmd._args[2] = it.cmd.cursor + } + + err := it.cmd.process(it.cmd) + if err != nil { + return false + } + + it.pos = 1 + + // Redis can occasionally return empty page. + if len(it.cmd.page) > 0 { + return true + } + } +} + +// Val returns the key/field at the current cursor position. +func (it *ScanIterator) Val() string { + var v string + it.mu.Lock() + if it.cmd.Err() == nil && it.pos > 0 && it.pos <= len(it.cmd.page) { + v = it.cmd.page[it.pos-1] + } + it.mu.Unlock() + return v +} |