Class: TaggableCache::Store::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/taggable_cache/store/redis.rb

Instance Method Summary collapse

Methods inherited from Base

#id_for, #in_scope?, #is_scope?

Constructor Details

#initialize(attrs = {}) ⇒ Redis

Returns a new instance of Redis.



6
7
8
# File 'lib/taggable_cache/store/redis.rb', line 6

def initialize(attrs = {})
  @redis = ::Redis.new attrs
end

Instance Method Details

#add(tag, *members) ⇒ Object

Add tag to multiple cache entries



11
12
13
14
15
16
17
# File 'lib/taggable_cache/store/redis.rb', line 11

def add(tag, *members)
  members.each do |element|
    add_scope(tag, element) if is_scope? element
    ident = id_for(element)
    @redis.sadd ident, tag unless ident.nil?
  end
end

#get(*members) ⇒ Object

Get cache entries by given keys. This list is cleaned up after request:

page = Page.create
store.add('tag_name', page)
store.get_scope(p).should == ['tag_name']
store.get_scope(p).should == []


25
26
27
28
29
30
31
32
33
34
# File 'lib/taggable_cache/store/redis.rb', line 25

def get(*members)
  keys = members.map { |tag| id_for(tag) }
  elements = @redis.sunion(keys)
  @redis.del(keys)
  
  scopes = members.delete_if {|a| not a.is_a? ActiveRecord::Base}

  elements += get_scope(*scopes)
  elements.flatten.uniq.compact
end