@@ -2276,6 +2276,31 @@ def scan(cursor, options={})
2276
2276
_scan ( :scan , cursor , [ ] , options )
2277
2277
end
2278
2278
2279
+ # Scan the keyspace
2280
+ #
2281
+ # @example Retrieve all of the keys (with possible duplicates)
2282
+ # redis.scan_each.to_a
2283
+ # # => ["key:21", "key:47", "key:42"]
2284
+ # @example Execute block for each key matching a pattern
2285
+ # redis.scan_each(:match => "key:1?") {|key| puts key}
2286
+ # # => key:13
2287
+ # # => key:18
2288
+ #
2289
+ # @param [Hash] options
2290
+ # - `:match => String`: only return keys matching the pattern
2291
+ # - `:count => Integer`: return count keys at most per iteration
2292
+ #
2293
+ # @return [Enumerator] an enumerator for all found keys
2294
+ def scan_each ( options = { } )
2295
+ return to_enum ( :scan_each , options ) unless block_given?
2296
+ cursor = 0
2297
+ loop do
2298
+ cursor , keys = scan ( cursor , options )
2299
+ keys . each { |key | yield key }
2300
+ break if cursor == "0"
2301
+ end
2302
+ end
2303
+
2279
2304
# Scan a hash
2280
2305
#
2281
2306
# @example Retrieve the first batch of key/value pairs in a hash
@@ -2293,6 +2318,27 @@ def hscan(key, cursor, options={})
2293
2318
end
2294
2319
end
2295
2320
2321
+ # Scan a hash
2322
+ #
2323
+ # @example Retrieve all of the key/value pairs in a hash
2324
+ # redis.hscan_each("hash").to_a
2325
+ # # => [["key70", "70"], ["key80", "80"]]
2326
+ #
2327
+ # @param [Hash] options
2328
+ # - `:match => String`: only return keys matching the pattern
2329
+ # - `:count => Integer`: return count keys at most per iteration
2330
+ #
2331
+ # @return [Enumerator] an enumerator for all found keys
2332
+ def hscan_each ( key , options = { } )
2333
+ return to_enum ( :hscan_each , key , options ) unless block_given?
2334
+ cursor = 0
2335
+ loop do
2336
+ cursor , key_values = hscan ( key , cursor , options )
2337
+ key_values . each { |key , val | yield key , val }
2338
+ break if cursor == "0"
2339
+ end
2340
+ end
2341
+
2296
2342
# Scan a sorted set
2297
2343
#
2298
2344
# @example Retrieve the first batch of key/value pairs in a hash
@@ -2311,6 +2357,27 @@ def zscan(key, cursor, options={})
2311
2357
end
2312
2358
end
2313
2359
2360
+ # Scan a sorted set
2361
+ #
2362
+ # @example Retrieve all of the members/scores in a sorted set
2363
+ # redis.zscan_each("zset").to_a
2364
+ # # => [["key70", "70"], ["key80", "80"]]
2365
+ #
2366
+ # @param [Hash] options
2367
+ # - `:match => String`: only return keys matching the pattern
2368
+ # - `:count => Integer`: return count keys at most per iteration
2369
+ #
2370
+ # @return [Enumerator] an enumerator for all found scores and members
2371
+ def zscan_each ( key , options = { } )
2372
+ return to_enum ( :zscan_each , key , options ) unless block_given?
2373
+ cursor = 0
2374
+ loop do
2375
+ cursor , member_score = zscan ( key , cursor , options )
2376
+ member_score . each { |member , score | yield member , score }
2377
+ break if cursor == "0"
2378
+ end
2379
+ end
2380
+
2314
2381
# Scan a set
2315
2382
#
2316
2383
# @example Retrieve the first batch of keys in a set
@@ -2326,6 +2393,27 @@ def sscan(key, cursor, options={})
2326
2393
_scan ( :sscan , cursor , [ key ] , options )
2327
2394
end
2328
2395
2396
+ # Scan a set
2397
+ #
2398
+ # @example Retrieve all of the keys in a set
2399
+ # redis.sscan("set").to_a
2400
+ # # => ["key1", "key2", "key3"]
2401
+ #
2402
+ # @param [Hash] options
2403
+ # - `:match => String`: only return keys matching the pattern
2404
+ # - `:count => Integer`: return count keys at most per iteration
2405
+ #
2406
+ # @return [Enumerator] an enumerator for all keys in the set
2407
+ def sscan_each ( key , options = { } )
2408
+ return to_enum ( :sscan_each , key , options ) unless block_given?
2409
+ cursor = 0
2410
+ loop do
2411
+ cursor , keys = sscan ( key , cursor , options )
2412
+ keys . each { |key | yield key }
2413
+ break if cursor == "0"
2414
+ end
2415
+ end
2416
+
2329
2417
def id
2330
2418
synchronize do |client |
2331
2419
client . id
0 commit comments