ruby-redis 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/LICENSE +9 -0
  2. data/README +90 -0
  3. data/bin/ruby-redis +12 -0
  4. data/bin/ruby-redis.compiled.rbc +243 -0
  5. data/lib/redis/bin.rb +74 -0
  6. data/lib/redis/bin.rbc +1325 -0
  7. data/lib/redis/buftok.rbc +2658 -0
  8. data/lib/redis/config.rb +46 -0
  9. data/lib/redis/config.rbc +1009 -0
  10. data/lib/redis/connection.rb +69 -0
  11. data/lib/redis/connection.rbc +1354 -0
  12. data/lib/redis/database.rb +109 -0
  13. data/lib/redis/database.rbc +2275 -0
  14. data/lib/redis/hashes.rb +72 -0
  15. data/lib/redis/hashes.rbc +1843 -0
  16. data/lib/redis/hiredis.rbc +658 -0
  17. data/lib/redis/keys.rb +165 -0
  18. data/lib/redis/keys.rbc +3386 -0
  19. data/lib/redis/lists.rb +227 -0
  20. data/lib/redis/lists.rbc +5241 -0
  21. data/lib/redis/logger.rb +81 -0
  22. data/lib/redis/logger.rbc +2106 -0
  23. data/lib/redis/protocol.rb +170 -0
  24. data/lib/redis/protocol.rbc +3735 -0
  25. data/lib/redis/pubsub.rb +153 -0
  26. data/lib/redis/pubsub.rbc +3447 -0
  27. data/lib/redis/reader.rb +164 -0
  28. data/lib/redis/reader.rbc +2769 -0
  29. data/lib/redis/send.rbc +1268 -0
  30. data/lib/redis/sender.rb +49 -0
  31. data/lib/redis/sender.rbc +1057 -0
  32. data/lib/redis/server.rb +62 -0
  33. data/lib/redis/server.rbc +1177 -0
  34. data/lib/redis/sets.rb +105 -0
  35. data/lib/redis/sets.rbc +2800 -0
  36. data/lib/redis/strict.rb +67 -0
  37. data/lib/redis/strict.rbc +1419 -0
  38. data/lib/redis/strings.rb +144 -0
  39. data/lib/redis/strings.rbc +3338 -0
  40. data/lib/redis/synchrony.rb +58 -0
  41. data/lib/redis/synchrony.rbc +1397 -0
  42. data/lib/redis/version.rb +7 -0
  43. data/lib/redis/version.rbc +180 -0
  44. data/lib/redis/zsets.rb +281 -0
  45. data/lib/redis/zsets.rbc +6596 -0
  46. data/lib/redis.rb +215 -0
  47. data/lib/redis.rbc +4391 -0
  48. metadata +117 -0
@@ -0,0 +1,109 @@
1
+ require File.expand_path '../redis', File.dirname(__FILE__)
2
+
3
+ class Redis
4
+
5
+ def self.databases
6
+ @@databases ||= [Database.new]
7
+ end
8
+
9
+ class Database
10
+
11
+ # Redis databases are volatile dictionaries.
12
+
13
+ attr_reader :blocked_pops, :watchers
14
+
15
+ def initialize
16
+ @dict = {}
17
+ @expiry = {}
18
+ @blocked_pops = {}
19
+ @watchers = {}
20
+ end
21
+
22
+ def touch key
23
+ (@watchers[key]||[]).each do |watcher|
24
+ watcher.succeed self, key
25
+ end
26
+ end
27
+
28
+ def expire key, seconds
29
+ return false unless @dict.has_key? key
30
+ touch key
31
+ @expiry[key] = Time.now + seconds
32
+ return true
33
+ end
34
+
35
+ def expire_at key, unixtime
36
+ return false unless @dict.has_key? key
37
+ touch key
38
+ @expiry[key] = Time.at unixtime
39
+ return true
40
+ end
41
+
42
+ def ttl key
43
+ check_expiry key
44
+ time = @expiry[key]
45
+ return -1 unless time
46
+ (time - Time.now).round
47
+ end
48
+
49
+ def persist key
50
+ result = @expiry.has_key? key
51
+ touch key if result
52
+ @expiry.delete key
53
+ result
54
+ end
55
+
56
+ def random_key
57
+ @dict.keys[rand @dict.size]
58
+ end
59
+
60
+ def [] key
61
+ check_expiry key
62
+ @dict[key]
63
+ end
64
+
65
+ def []= key, value
66
+ touch key
67
+ @expiry.delete key
68
+ @dict[key] = value
69
+ end
70
+
71
+ def has_key? key
72
+ check_expiry key
73
+ @dict.has_key? key
74
+ end
75
+
76
+ def delete key
77
+ touch key
78
+ @dict.delete key
79
+ @expiry.delete key
80
+ end
81
+
82
+ def reduce *args, &block
83
+ @dict.reduce *args, &block
84
+ end
85
+
86
+ def size
87
+ @dict.size
88
+ end
89
+
90
+ def clear
91
+ # We don't trigger watchers of unset records
92
+ @dict.each_key { |key| touch key }
93
+ @dict.clear
94
+ @expiry.clear
95
+ end
96
+
97
+ def empty?
98
+ @dict.empty?
99
+ end
100
+
101
+ private
102
+
103
+ def check_expiry key
104
+ expires_at = @expiry[key]
105
+ delete key if expires_at and Time.now > expires_at
106
+ end
107
+
108
+ end
109
+ end