Module: Sequel::Plugins::Caching::ClassMethods
- Defined in:
- lib/sequel/plugins/caching.rb
Instance Attribute Summary collapse
-
#cache_ignore_exceptions ⇒ Object
readonly
If true, ignores exceptions when getting cached records (the memcached API).
-
#cache_store ⇒ Object
readonly
The cache store object for the model, which should implement the Ruby-Memcache (or memcached) API.
-
#cache_ttl ⇒ Object
readonly
The time to live for the cache store, in seconds.
Instance Method Summary collapse
-
#cache_delete_pk(pk) ⇒ Object
Delete the cached object with the given primary key.
-
#cache_get_pk(pk) ⇒ Object
Return the cached object with the given primary key, or nil if no such object is in the cache.
-
#cache_key(pk) ⇒ Object
Return a key string for the given primary key.
-
#cache_key_prefix ⇒ Object
Returns the prefix used to namespace this class in the cache.
-
#composite_cache_key(pk) ⇒ Object
Return an appropriate cache key string for a valid composite primary key.
-
#scalar_cache_key(pk) ⇒ Object
Return an appropriate cache key string for a valid scalar primary key.
-
#set_cache_ttl(ttl) ⇒ Object
Set the time to live for the cache store, in seconds (default is 3600, # so 1 hour).
Instance Attribute Details
#cache_ignore_exceptions ⇒ Object (readonly)
If true, ignores exceptions when getting cached records (the memcached API).
64 65 66 |
# File 'lib/sequel/plugins/caching.rb', line 64 def cache_ignore_exceptions @cache_ignore_exceptions end |
#cache_store ⇒ Object (readonly)
The cache store object for the model, which should implement the Ruby-Memcache (or memcached) API
68 69 70 |
# File 'lib/sequel/plugins/caching.rb', line 68 def cache_store @cache_store end |
#cache_ttl ⇒ Object (readonly)
The time to live for the cache store, in seconds.
71 72 73 |
# File 'lib/sequel/plugins/caching.rb', line 71 def cache_ttl @cache_ttl end |
Instance Method Details
#cache_delete_pk(pk) ⇒ Object
Delete the cached object with the given primary key.
74 75 76 |
# File 'lib/sequel/plugins/caching.rb', line 74 def cache_delete_pk(pk) cache_delete(cache_key(pk)) end |
#cache_get_pk(pk) ⇒ Object
Return the cached object with the given primary key, or nil if no such object is in the cache.
80 81 82 |
# File 'lib/sequel/plugins/caching.rb', line 80 def cache_get_pk(pk) cache_get(cache_key(pk)) end |
#cache_key(pk) ⇒ Object
Return a key string for the given primary key.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sequel/plugins/caching.rb', line 90 def cache_key(pk) case pk when Array raise(Error, 'no primary key for this record') unless pk.all? "#{cache_key_prefix}:#{composite_cache_key(pk)}" else raise(Error, 'no primary key for this record') unless pk "#{cache_key_prefix}:#{scalar_cache_key(pk)}" end end |
#cache_key_prefix ⇒ Object
Returns the prefix used to namespace this class in the cache.
85 86 87 |
# File 'lib/sequel/plugins/caching.rb', line 85 def cache_key_prefix to_s end |
#composite_cache_key(pk) ⇒ Object
Return an appropriate cache key string for a valid composite primary key. By default, joins with the parts of the compsite key with ",".
109 110 111 |
# File 'lib/sequel/plugins/caching.rb', line 109 def composite_cache_key(pk) pk.join(',') end |
#scalar_cache_key(pk) ⇒ Object
Return an appropriate cache key string for a valid scalar primary key. By default, converts the primary key to a string.
103 104 105 |
# File 'lib/sequel/plugins/caching.rb', line 103 def scalar_cache_key(pk) pk.to_s end |
#set_cache_ttl(ttl) ⇒ Object
Set the time to live for the cache store, in seconds (default is 3600, # so 1 hour).
116 117 118 |
# File 'lib/sequel/plugins/caching.rb', line 116 def set_cache_ttl(ttl) @cache_ttl = ttl end |