Class: Redis::NativeHash
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from KeyHelper
#convert_key, #generate_key, #key, #redis_key
included
Methods inherited from TrackedHash
#added, #changed, #deleted, #dup, #original, #populate, #retrack, #untrack
Methods inherited from Hash
#stringify_keys!
Constructor Details
#initialize(aargh = nil) ⇒ NativeHash
Returns a new instance of NativeHash.
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/redis/native_hash.rb', line 12
def initialize(aargh = nil)
super(nil)
case aargh
when String,Symbol
self.namespace = aargh
when Hash
self.namespace = aargh.keys.first
self.key = aargh.values.first
end
track!
end
|
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
10
11
12
|
# File 'lib/redis/native_hash.rb', line 10
def namespace
@namespace
end
|
#version ⇒ Object
57
58
59
|
# File 'lib/redis/native_hash.rb', line 57
def version
@version ||= generate_key
end
|
Class Method Details
.attr_persist(*attributes) ⇒ Object
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/redis/native_hash.rb', line 175
def attr_persist(*attributes)
attributes.each do |attr|
class_eval <<-EOS
def #{attr}=(value)
self["#{attr}"] = value
end
def #{attr}
self["#{attr}"]
end
EOS
end
end
|
.build(namespace, key, values) ⇒ Object
162
163
164
165
166
167
168
|
# File 'lib/redis/native_hash.rb', line 162
def build(namespace, key, values)
h = self.new
h.namespace = namespace
h.key = key
h.populate(values)
h
end
|
.fetch_values(key) ⇒ Object
170
171
172
173
|
# File 'lib/redis/native_hash.rb', line 170
def fetch_values(key)
results = redis.hgetall(key)
results.each_pair { |key,value| results[key] = Redis::Marshal.load(value) }
end
|
.find(params) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/redis/native_hash.rb', line 135
def find(params)
case params
when Hash
hashes = []
params.each_pair do |namespace, key|
result = fetch_values( "#{namespace}:#{key}" )
unless result.empty?
hashes << build(namespace,key,result)
end
end
unless hashes.empty?
hashes.size == 1 ? hashes.first : hashes
else
nil
end
when String,Symbol
unless self == Redis::NativeHash
namespace = self.new.namespace.to_s
namespace = "#{namespace}:" unless namespace.empty?
result = fetch_values( "#{namespace}#{params}" )
else
result = fetch_values(params)
end
result.empty? ? nil : build(nil,params,result)
end
end
|
Instance Method Details
#[](key) ⇒ Object
33
34
35
|
# File 'lib/redis/native_hash.rb', line 33
def [](key)
super convert_key(key)
end
|
#[]=(key, value) ⇒ Object
Also known as:
store
24
25
26
|
# File 'lib/redis/native_hash.rb', line 24
def []=(key, value)
super(convert_key(key), value)
end
|
#delete(key) ⇒ Object
44
45
46
|
# File 'lib/redis/native_hash.rb', line 44
def delete(key)
super convert_key(key)
end
|
#destroy ⇒ Object
114
115
116
117
118
119
|
# File 'lib/redis/native_hash.rb', line 114
def destroy
redis.del( redis_key )
untrack!
clear
self.key = nil
end
|
#expire(seconds) ⇒ Object
130
131
132
|
# File 'lib/redis/native_hash.rb', line 130
def expire(seconds)
redis.expire(redis_key, seconds)
end
|
#fetch(key, *extras) ⇒ Object
29
30
31
|
# File 'lib/redis/native_hash.rb', line 29
def fetch(key, *)
super(convert_key(key),*)
end
|
#key=(new_key) ⇒ Object
52
53
54
|
# File 'lib/redis/native_hash.rb', line 52
def key=(new_key)
renew_key(new_key)
end
|
#key?(key) ⇒ Boolean
Also known as:
include?, has_key?, member?
37
38
39
|
# File 'lib/redis/native_hash.rb', line 37
def key?(key)
super convert_key(key)
end
|
#reload! ⇒ Object
Also known as:
reload
108
109
110
111
|
# File 'lib/redis/native_hash.rb', line 108
def reload!
hash = self.class.find( namespace ? {namespace => key} : key )
self.update( hash ) if hash
end
|
#renew_key(new_key = nil) ⇒ Object
121
122
123
124
125
126
127
128
|
# File 'lib/redis/native_hash.rb', line 121
def renew_key(new_key = nil)
unless @key.nil? || @key == new_key
redis.del( redis_key )
original.clear
end
@key = new_key
key
end
|
#replace(other_hash) ⇒ Object
103
104
105
106
|
# File 'lib/redis/native_hash.rb', line 103
def replace(other_hash)
clear
update(other_hash)
end
|
#save(max_attempts = 5) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/redis/native_hash.rb', line 61
def save( max_attempts = 5 )
(1..max_attempts).each do |n|
redis.watch redis_key
latest_version = redis.hget(redis_key, "__version")
reload! unless ( latest_version.nil? || latest_version == self.version )
self.version = nil changed_keys = (self.changed + self.added).uniq
changes = []
changed_keys.each do |key|
changes.push( key, Redis::Marshal.dump(self[key]) )
end
deleted_keys = self.deleted
if deleted_keys.empty? and changes.empty?
redis.unwatch
return true
end
success = redis.multi do
redis.hmset( redis_key, *changes.push("__version", self.version) ) unless changes.empty?
deleted_keys.each { |key| redis.hdel( redis_key, key) }
end
if success
untrack!; track! return true
end
end
raise "Unable to save hash after max attempts (#{max_attempts}). " +
"Amazing concurrency event may be underway. " +
"Make some popcorn."
false
end
|
#update(data) ⇒ Object
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/redis/native_hash.rb', line 92
def update(data)
v = case data
when self.class
data.version
when ::Hash
data.delete("__version")
end
self.version = v unless v.nil?
super(data.stringify_keys!)
end
|
#values_at(*indices) ⇒ Object
48
49
50
|
# File 'lib/redis/native_hash.rb', line 48
def values_at(*indices)
indices.collect { |key| self[ convert_key(key) ] }
end
|