# File lib/sequel/plugins/caching.rb, line 113 def primary_key_lookup(pk) ck = cache_key(pk) unless obj = cache_get(ck) if obj = super(pk) cache_set(ck, obj) end end obj 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, line 57 def cache_delete_pk(pk) cache_delete(cache_key(pk)) 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, line 63 def cache_get_pk(pk) cache_get(cache_key(pk)) end
Return a key string for the given primary key.
# File lib/sequel/plugins/caching.rb, line 73 def cache_key(pk) raise(Error, 'no primary key for this record') unless pk.is_a?(Array) ? pk.all? : pk "#{cache_key_prefix}:#{Array(pk).join(',')}" end
Returns the prefix used to namespace this class in the cache.
# File lib/sequel/plugins/caching.rb, line 68 def cache_key_prefix "#{self}" end
Set the time to live for the cache store, in seconds (default is 3600, # so 1 hour).
# File lib/sequel/plugins/caching.rb, line 81 def set_cache_ttl(ttl) @cache_ttl = ttl end
Private Instance Methods
Delete the entry with the matching key from the cache
# File lib/sequel/plugins/caching.rb, line 88 def cache_delete(ck) if @cache_ignore_exceptions @cache_store.delete(ck) rescue nil else @cache_store.delete(ck) end nil end
Returned the cached object, or nil if the object was not in the cached
# File lib/sequel/plugins/caching.rb, line 99 def cache_get(ck) if @cache_ignore_exceptions @cache_store.get(ck) rescue nil else @cache_store.get(ck) end end
Set the object in the #cache_store with the given key for #cache_ttl seconds.
# File lib/sequel/plugins/caching.rb, line 108 def cache_set(ck, obj) @cache_store.set(ck, obj, @cache_ttl) end
Check the cache before a database lookup unless a hash is supplied.