Class: Queris::HashCache
Constant Summary
Constants inherited
from Index
Index::DELTA_TTL
Instance Attribute Summary
Attributes inherited from Index
#attribute, #delta_ttl, #live, #model, #name, #redis
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Index
#digest, #distribution, #eliminate, #erase!, #exists?, #handle_range?, #incremental?, #index_val, #json_redis_dump, #key_attr, #keypattern, #keys, #live_delta_key, #no_live_update, #poke, #skip_create?, #skip_delete?, #skip_update?, #stateless?, #temp_keys, #update_live_delta, #usable_as_results?, #val, #value_diff, #value_is, #value_was
Constructor Details
#initialize(arg = {}) ⇒ HashCache
maintains a cached copy of an object with separetely marshaled attributes in redis
195
196
197
198
199
200
201
|
# File 'lib/queris/indices.rb', line 195
def initialize(arg={})
@name= "#{arg[:attribute] || "all_attribute"}_hashcache"
super arg
@attribute= arg[:attribute]
raise ClientError, "Model not passed to index." unless @model
@name=@model.to_s
end
|
Class Method Details
.skip_create? ⇒ Boolean
don’t add this index to the list of indices to be built when calling Queris.rebuild!
204
|
# File 'lib/queris/indices.rb', line 204
def self.skip_create?; true; end
|
Instance Method Details
#create(obj) ⇒ Object
223
224
225
226
227
228
229
230
|
# File 'lib/queris/indices.rb', line 223
def create(obj)
if @attriute.nil?
cache_attributes obj, obj.all_cacheable_attributes
elsif not obj.call(@attribute).nil?
cache_attributes obj, @attribute => send(@attribute)
end
end
|
#delete(obj) ⇒ Object
232
233
234
|
# File 'lib/queris/indices.rb', line 232
def delete(obj)
redis(obj).del hash_key obj
end
|
#fetch(id, opt = {}) ⇒ Object
Also known as:
load
236
237
238
239
240
241
242
243
244
245
246
247
248
|
# File 'lib/queris/indices.rb', line 236
def fetch(id, opt={})
if @attribute.nil?
hash = (opt[:redis] || Queris.redis(:slave, :master)).hgetall hash_key id
loaded = load_cached hash
if hash && loaded.nil?
Queris.redis(:master).del(hash_key id)
end
loaded
else
return (opt[:redis] || Queris.redis(:slave, :master)).hget hash_key(id), @attribute
end
end
|
#hash_key(obj, prefix = nil, raw_val = false) ⇒ Object
Also known as:
key
206
207
208
209
210
211
212
213
|
# File 'lib/queris/indices.rb', line 206
def hash_key(obj, prefix=nil, raw_val=false)
if raw_val
id = obj
else
id = obj.kind_of?(@model) ? obj.send(@key) : obj
end
(@keyf) %[prefix || @redis_prefix || @model.prefix, id]
end
|
#info ⇒ Object
276
277
278
279
|
# File 'lib/queris/indices.rb', line 276
def info
keycounts = distribution.values
"HashCache: #{keycounts.count} objects cached."
end
|
#load_cached(marshaled_hash) ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/queris/indices.rb', line 250
def load_cached(marshaled_hash)
@cached_attr_count ||= (not @attribute.nil?) ? 1 : @model.new.all_cacheable_attributes.length
if marshaled_hash.length >= @cached_attr_count
unmarshaled = {}
marshaled_hash.each_with_index do |v|
unmarshaled[v.first.to_sym]=Marshal.load v.last
end
obj= @model.new
begin
obj.assign_attributes(unmarshaled, :without_protection => true)
rescue Exception => e
return nil
end
obj.instance_eval do
@new_record= false
@changed_attributes={}
end
obj
else
nil
end
end
|
#update(obj) ⇒ Object
215
216
217
218
219
220
221
222
|
# File 'lib/queris/indices.rb', line 215
def update(obj)
changed_attrs = obj.changed_cacheable_attributes
if @attribute.nil?
cache_attributes obj, changed_attrs unless changed_attrs.length == 0
elsif changed_attrs.member? @attribute
cache_attributes obj, @attribute => send(@attribute)
end
end
|