# File lib/sequel/plugins/caching.rb 121 def primary_key_lookup(pk) 122 ck = cache_key(pk) 123 unless obj = cache_get(ck) 124 if obj = super(pk) 125 cache_set(ck, obj) 126 end 127 end 128 obj 129 end
module Sequel::Plugins::Caching::ClassMethods
Attributes
If true, ignores exceptions when gettings cached records (the memcached API).
The cache store object for the model, which should implement the Ruby-Memcache (or memcached) API
The time to live for the cache store, in seconds.
Public Instance Methods
Delete the cached object with the given primary key.
# File lib/sequel/plugins/caching.rb 64 def cache_delete_pk(pk) 65 cache_delete(cache_key(pk)) 66 end
Return the cached object with the given primary key, or nil if no such object is in the cache.
# File lib/sequel/plugins/caching.rb 70 def cache_get_pk(pk) 71 cache_get(cache_key(pk)) 72 end
Return a key string for the given primary key.
# File lib/sequel/plugins/caching.rb 80 def cache_key(pk) 81 raise(Error, 'no primary key for this record') unless pk.is_a?(Array) ? pk.all? : pk 82 "#{cache_key_prefix}:#{Array(pk).join(',')}" 83 end
Returns the prefix used to namespace this class in the cache.
# File lib/sequel/plugins/caching.rb 75 def cache_key_prefix 76 to_s 77 end
Set the time to live for the cache store, in seconds (default is 3600, # so 1 hour).
# File lib/sequel/plugins/caching.rb 88 def set_cache_ttl(ttl) 89 @cache_ttl = ttl 90 end
Private Instance Methods
Delete the entry with the matching key from the cache
# File lib/sequel/plugins/caching.rb 104 def cache_delete(ck) 105 cache_op(:delete, ck) 106 nil 107 end
Returned the cached object, or nil if the object was not in the cached
# File lib/sequel/plugins/caching.rb 111 def cache_get(ck) 112 cache_op(:get, ck) 113 end
Access
the cache using the given method and key, rescuing exceptions if necessary.
# File lib/sequel/plugins/caching.rb 95 def cache_op(meth, ck) 96 if @cache_ignore_exceptions 97 @cache_store.public_send(meth, ck) rescue nil 98 else 99 @cache_store.public_send(meth, ck) 100 end 101 end
Set the object in the cache_store
with the given key for cache_ttl
seconds.
# File lib/sequel/plugins/caching.rb 116 def cache_set(ck, obj) 117 @cache_store.set(ck, obj, @cache_ttl) 118 end
Check the cache before a database lookup unless a hash is supplied.