Class: Ultracache::BelongsAsCachedQueue

Inherits:
Relationship show all
Defined in:
lib/ultracache/relationship/belongs_as_cached_queue.rb

Instance Attribute Summary

Attributes inherited from Relationship

#name

Instance Method Summary collapse

Methods inherited from Relationship

#associated_class, #read_cache, #self_class

Constructor Details

#initialize(name, block, options = {}) ⇒ BelongsAsCachedQueue

Returns a new instance of BelongsAsCachedQueue.



3
4
5
6
7
8
9
# File 'lib/ultracache/relationship/belongs_as_cached_queue.rb', line 3

def initialize(name, block, options={})
  super(name, options)
  @serializer_block = block
  @alias = options[:as]
  @need_update = options[:need_update]
  @unless = options[:unless]
end

Instance Method Details

#destroy_cache(obj) ⇒ Object

Destroys serialized cache from associated cache queue. In some cases like caching serialized documents of MongoDB, two or more cached objects may have the same score with other documents. To remove cache we want to delete only, we should deal with this problem.

If two or more documents are fetched with the computed score, ‘destroy_cache` deserializes the cached documents in order to find one cache having the same identifier.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ultracache/relationship/belongs_as_cached_queue.rb', line 38

def destroy_cache(obj)
  score = score_of(obj)
  key = key(obj)

  docs = storage.get_queue(key, :from => score, :to => score)

  if docs.count == 1
    # Only one document is fetched from queue, and it is okay to remove
    storage.remove_from_queue_by_range(key, :from => score, :to => score)
  elsif docs.count > 1
    # We should deserialize fetched documents to find the document having
    # the same id with `obj`
    docs.each do |doc|
      deserialized = serializer.deserialize(doc)
      _id = deserialized["id"]
      _id = deserialized["_id"] unless _id

      if _id == obj.id
        storage.remove_from_queue(key, doc)
      end
    end
  end
end

#key(obj) ⇒ Object

Returns key of cache queue which cache of the object will be stored into



71
72
73
74
75
76
77
78
# File 'lib/ultracache/relationship/belongs_as_cached_queue.rb', line 71

def key(obj)
  associated = @associated_class.to_s.underscore

  namespace = @name
  parent_id = obj.send("#{associated}_id")

  "#{@associated_class.to_s.underscore}:#{parent_id}:#{namespace}"
end

#save_cache(obj) ⇒ Object

Saves serialized form of object into cache queue which the object has a relationship to.

The first parameter, ‘obj`, is the object which will be stored into cache queue. `storage` parameter is

Returns:

  • Serialized form of the object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ultracache/relationship/belongs_as_cached_queue.rb', line 18

def save_cache(obj)
  return if @unless && obj.send(@unless)

  value = if @serializer_block
    @serializer_block.call obj
  else
    serializer.serialize(obj.as_json)
  end

  storage.put_queue(key(obj), score_of(obj), value)
end

#update_cache(obj) ⇒ Object

Updates object cache in queue. To update cache, we should destroy the document and regenerate it with updated information.



64
65
66
67
68
# File 'lib/ultracache/relationship/belongs_as_cached_queue.rb', line 64

def update_cache(obj)
  return unless @need_update
  delete_cache(obj)
  save_cache(obj)
end