KellyMahan-memcachedb-client 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/README.rdoc +1 -1
- data/lib/memcache_db.rb +86 -0
- metadata +1 -1
data/History.txt
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
= 1.1
|
2
|
+
|
3
|
+
added get_range method.
|
4
|
+
|
5
|
+
example:
|
6
|
+
|
7
|
+
CACHE.get_range(:a, :z, limit)
|
8
|
+
|
9
|
+
this will return all records between and including a through z with a limit on the records. The max is 100
|
10
|
+
|
1
11
|
= 1.0
|
2
12
|
|
3
13
|
derived from memcache-client 1.6.3 http://github.com/mperham for use with memcachedb
|
data/README.rdoc
CHANGED
data/lib/memcache_db.rb
CHANGED
@@ -221,6 +221,29 @@ class MemCacheDb
|
|
221
221
|
# cache["b"] = 2
|
222
222
|
# cache.get_multi "a", "b" # => { "a" => 1, "b" => 2 }
|
223
223
|
|
224
|
+
|
225
|
+
def get_range(key1, key2, limit=100)
|
226
|
+
raise MemCacheDbError, 'No active servers' unless active?
|
227
|
+
|
228
|
+
results = {}
|
229
|
+
|
230
|
+
begin
|
231
|
+
servers.each do |server|
|
232
|
+
values = cache_rget(server, key1, key2, limit)
|
233
|
+
values.each do |key, value|
|
234
|
+
results[key.gsub(/#{@namespace}\:/,'')] = Marshal.load value
|
235
|
+
end
|
236
|
+
end
|
237
|
+
rescue
|
238
|
+
logger.warn { "Unable to retrieve key range #{key1} to #{key2}"} if logger
|
239
|
+
end
|
240
|
+
return results
|
241
|
+
rescue TypeError => err
|
242
|
+
handle_error nil, err
|
243
|
+
end
|
244
|
+
|
245
|
+
|
246
|
+
|
224
247
|
def get_multi(*keys)
|
225
248
|
raise MemCacheDbError, 'No active servers' unless active?
|
226
249
|
|
@@ -576,6 +599,69 @@ class MemCacheDb
|
|
576
599
|
raise MemCacheDbError, "lost connection to #{server.host}:#{server.port}" # TODO: retry here too
|
577
600
|
end
|
578
601
|
end
|
602
|
+
|
603
|
+
def cache_rget(server, start_key, end_key, max=100)
|
604
|
+
|
605
|
+
# rget <start key> <end key> <left openness flag> <right openness flag> <max items>\r\n
|
606
|
+
#
|
607
|
+
# - <start key> where the query starts.
|
608
|
+
# - <end key> where the query ends.
|
609
|
+
# - <left openness flag> indicates the openness of left side, 0 means the result includes <start key>, while 1 means not.
|
610
|
+
# - <right openness flag> indicates the openness of right side, 0 means the result includes <end key>, while 1 means not.
|
611
|
+
# - <max items> how many items at most return, max is 100.
|
612
|
+
#
|
613
|
+
# After this command, the client expects zero or more items, each of
|
614
|
+
# which is received as a text line followed by a data block. After all
|
615
|
+
# the items have been transmitted, the server sends the string
|
616
|
+
#
|
617
|
+
# "END\r\n"
|
618
|
+
#
|
619
|
+
# to indicate the end of response.
|
620
|
+
#
|
621
|
+
# Each item sent by the server looks like this:
|
622
|
+
#
|
623
|
+
# VALUE <key> <flags> <bytes>\r\n
|
624
|
+
# <data block>\r\n
|
625
|
+
#
|
626
|
+
# - <key> is the key for the item being sent
|
627
|
+
#
|
628
|
+
# - <flags> is the flags value set by the storage command
|
629
|
+
#
|
630
|
+
# - <bytes> is the length of the data block to follow, *not* including
|
631
|
+
# its delimiting \r\n
|
632
|
+
#
|
633
|
+
# - <data block> is the data for this item.
|
634
|
+
#
|
635
|
+
# Notice: all keys in MemcacheDB is sorted alphabetically, so is the return of query result.
|
636
|
+
|
637
|
+
with_socket_management(server) do |socket|
|
638
|
+
values = {}
|
639
|
+
socket.write "rget #{start_key} #{end_key} 0 0 #{max}\r\n"
|
640
|
+
|
641
|
+
while keyline = socket.gets do
|
642
|
+
return values if keyline == "END\r\n"
|
643
|
+
raise_on_error_response! keyline
|
644
|
+
|
645
|
+
unless keyline =~ /\AVALUE (.+) (.+) (.+)/ then
|
646
|
+
server.close
|
647
|
+
raise MemCacheDbError, "unexpected response #{keyline.inspect}"
|
648
|
+
end
|
649
|
+
|
650
|
+
key, data_length = $1, $3
|
651
|
+
values[$1] = socket.read data_length.to_i
|
652
|
+
socket.read(2) # "\r\n"
|
653
|
+
end
|
654
|
+
|
655
|
+
server.close
|
656
|
+
raise MemCacheDbError, "lost connection to #{server.host}:#{server.port}" # TODO: retry here too
|
657
|
+
end
|
658
|
+
|
659
|
+
|
660
|
+
|
661
|
+
end
|
662
|
+
|
663
|
+
|
664
|
+
|
579
665
|
|
580
666
|
##
|
581
667
|
# Performs a raw incr for +cache_key+ from +server+. Returns nil if not
|