Module: Sequel::Plugins::Caching::ClassMethods

Defined in:
lib/sequel/plugins/caching.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_storeObject (readonly)

The cache store object for the model, which should implement the Ruby-Memcache API



31
32
33
# File 'lib/sequel/plugins/caching.rb', line 31

def cache_store
  @cache_store
end

#cache_ttlObject (readonly)

The time to live for the cache store, in seconds.



34
35
36
# File 'lib/sequel/plugins/caching.rb', line 34

def cache_ttl
  @cache_ttl
end

Instance Method Details

#[](*args) ⇒ Object

Check the cache before a database lookup unless a hash is supplied.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sequel/plugins/caching.rb', line 37

def [](*args)
  args = args.first if (args.size == 1)
  return super(args) if args.is_a?(Hash)
  ck = cache_key(args)
  if obj = @cache_store.get(ck)
    return obj
  end
  if obj = super(args)
    @cache_store.set(ck, obj, @cache_ttl)
  end 
  obj
end

#inherited(subclass) ⇒ Object

Copy the cache_store and cache_ttl to the subclass.



56
57
58
59
60
61
62
63
64
# File 'lib/sequel/plugins/caching.rb', line 56

def inherited(subclass)
  super
  store = @cache_store
  ttl = @cache_ttl
  subclass.instance_eval do
    @cache_store = store
    @cache_ttl = ttl
  end
end

#set_cache_ttl(ttl) ⇒ Object

Set the time to live for the cache store, in seconds (default is 3600, # so 1 hour).



51
52
53
# File 'lib/sequel/plugins/caching.rb', line 51

def set_cache_ttl(ttl)
  @cache_ttl = ttl
end