# File lib/sequel/plugins/caching.rb 149 def primary_key_lookup(pk) 150 ck = cache_key(pk) 151 unless obj = cache_get(ck) 152 if obj = super(pk) 153 cache_set(ck, obj) 154 end 155 end 156 obj 157 end
module Sequel::Plugins::Caching::ClassMethods
Attributes
If true, ignores exceptions when getting 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
Source
# File lib/sequel/plugins/caching.rb 74 def cache_delete_pk(pk) 75 cache_delete(cache_key(pk)) 76 end
Delete the cached object with the given primary key.
Source
# File lib/sequel/plugins/caching.rb 80 def cache_get_pk(pk) 81 cache_get(cache_key(pk)) 82 end
Return the cached object with the given primary key, or nil if no such object is in the cache.
Source
# File lib/sequel/plugins/caching.rb 90 def cache_key(pk) 91 case pk 92 when Array 93 raise(Error, 'no primary key for this record') unless pk.all? 94 "#{cache_key_prefix}:#{composite_cache_key(pk)}" 95 else 96 raise(Error, 'no primary key for this record') unless pk 97 "#{cache_key_prefix}:#{scalar_cache_key(pk)}" 98 end 99 end
Return a key string for the given primary key.
Source
# File lib/sequel/plugins/caching.rb 85 def cache_key_prefix 86 to_s 87 end
Returns the prefix used to namespace this class in the cache.
Source
# File lib/sequel/plugins/caching.rb 109 def composite_cache_key(pk) 110 pk.join(',') 111 end
Return an appropriate cache key string for a valid composite primary key. By default, joins with the parts of the compsite key with “,”.
Source
# File lib/sequel/plugins/caching.rb 103 def scalar_cache_key(pk) 104 pk.to_s 105 end
Return an appropriate cache key string for a valid scalar primary key. By default, converts the primary key to a string.
Source
# File lib/sequel/plugins/caching.rb 116 def set_cache_ttl(ttl) 117 @cache_ttl = ttl 118 end
Set the time to live for the cache store, in seconds (default is 3600, # so 1 hour).
Private Instance Methods
Source
# File lib/sequel/plugins/caching.rb 132 def cache_delete(ck) 133 cache_op(:delete, ck) 134 nil 135 end
Delete the entry with the matching key from the cache
Source
# File lib/sequel/plugins/caching.rb 139 def cache_get(ck) 140 cache_op(:get, ck) 141 end
Returned the cached object, or nil if the object was not in the cached
Source
# File lib/sequel/plugins/caching.rb 123 def cache_op(meth, ck) 124 if @cache_ignore_exceptions 125 @cache_store.public_send(meth, ck) rescue nil 126 else 127 @cache_store.public_send(meth, ck) 128 end 129 end
Access the cache using the given method and key, rescuing exceptions if necessary.
Source
# File lib/sequel/plugins/caching.rb 144 def cache_set(ck, obj) 145 @cache_store.set(ck, obj, @cache_ttl) 146 end
Set the object in the cache_store with the given key for cache_ttl seconds.
Source
Check the cache before a database lookup unless a hash is supplied.