Class: Redis::Database
- Inherits:
-
Object
- Object
- Redis::Database
- Defined in:
- lib/redis/database.rb
Defined Under Namespace
Classes: Watcher
Instance Attribute Summary collapse
-
#blocked_pops ⇒ Object
readonly
Redis databases are volatile dictionaries.
-
#watchers ⇒ Object
readonly
Redis databases are volatile dictionaries.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #empty? ⇒ Boolean
- #expire(key, seconds) ⇒ Object
- #expire_at(key, unixtime) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #persist(key) ⇒ Object
- #random_key ⇒ Object
- #reduce(*args, &block) ⇒ Object
- #size ⇒ Object
- #touch(key) ⇒ Object
- #ttl(key) ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
39 40 41 42 43 44 |
# File 'lib/redis/database.rb', line 39 def initialize @dict = {} @expiry = {} @blocked_pops = {} @watchers = {} end |
Instance Attribute Details
#blocked_pops ⇒ Object (readonly)
Redis databases are volatile dictionaries.
37 38 39 |
# File 'lib/redis/database.rb', line 37 def blocked_pops @blocked_pops end |
#watchers ⇒ Object (readonly)
Redis databases are volatile dictionaries.
37 38 39 |
# File 'lib/redis/database.rb', line 37 def watchers @watchers end |
Instance Method Details
#[](key) ⇒ Object
84 85 86 87 |
# File 'lib/redis/database.rb', line 84 def [] key check_expiry key @dict[key] end |
#[]=(key, value) ⇒ Object
89 90 91 92 93 |
# File 'lib/redis/database.rb', line 89 def []= key, value touch key @expiry.delete key @dict[key] = value end |
#clear ⇒ Object
114 115 116 117 118 119 |
# File 'lib/redis/database.rb', line 114 def clear # We don't trigger watchers of unset records @dict.each_key { |key| touch key } @dict.clear @expiry.clear end |
#delete(key) ⇒ Object
100 101 102 103 104 |
# File 'lib/redis/database.rb', line 100 def delete key touch key @dict.delete key @expiry.delete key end |
#empty? ⇒ Boolean
121 122 123 |
# File 'lib/redis/database.rb', line 121 def empty? @dict.empty? end |
#expire(key, seconds) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/redis/database.rb', line 52 def expire key, seconds return false unless @dict.has_key? key touch key @expiry[key] = Time.now + seconds return true end |
#expire_at(key, unixtime) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/redis/database.rb', line 59 def expire_at key, unixtime return false unless @dict.has_key? key touch key @expiry[key] = Time.at unixtime return true end |
#has_key?(key) ⇒ Boolean
95 96 97 98 |
# File 'lib/redis/database.rb', line 95 def has_key? key check_expiry key @dict.has_key? key end |
#persist(key) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/redis/database.rb', line 73 def persist key result = @expiry.has_key? key touch key if result @expiry.delete key result end |
#random_key ⇒ Object
80 81 82 |
# File 'lib/redis/database.rb', line 80 def random_key @dict.keys[rand @dict.size] end |
#reduce(*args, &block) ⇒ Object
106 107 108 |
# File 'lib/redis/database.rb', line 106 def reduce *args, &block @dict.reduce *args, &block end |
#size ⇒ Object
110 111 112 |
# File 'lib/redis/database.rb', line 110 def size @dict.size end |
#touch(key) ⇒ Object
46 47 48 49 50 |
# File 'lib/redis/database.rb', line 46 def touch key (@watchers[key]||[]).each do |watcher| watcher.unbind end end |
#ttl(key) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/redis/database.rb', line 66 def ttl key check_expiry key time = @expiry[key] return -1 unless time (time - Time.now).round end |