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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_ignore_exceptionsObject (readonly)

If true, ignores exceptions when gettings cached records (the memcached API).



47
48
49
# File 'lib/sequel/plugins/caching.rb', line 47

def cache_ignore_exceptions
  @cache_ignore_exceptions
end

#cache_storeObject (readonly)

The cache store object for the model, which should implement the Ruby-Memcache (or memcached) API



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

def cache_store
  @cache_store
end

#cache_ttlObject (readonly)

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



54
55
56
# File 'lib/sequel/plugins/caching.rb', line 54

def cache_ttl
  @cache_ttl
end

Instance Method Details

#cache_delete_pk(pk) ⇒ Object

Delete the cached object with the given primary key.



57
58
59
# File 'lib/sequel/plugins/caching.rb', line 57

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.



63
64
65
# File 'lib/sequel/plugins/caching.rb', line 63

def cache_get_pk(pk)
  cache_get(cache_key(pk))
end

#cache_key(pk) ⇒ Object

Return a key string for the given primary key.

Raises:



68
69
70
71
# File 'lib/sequel/plugins/caching.rb', line 68

def cache_key(pk)
  raise(Error, 'no primary key for this record') unless pk.is_a?(Array) ? pk.all? : pk
  "#{self}:#{Array(pk).join(',')}"
end

#inherited(subclass) ⇒ Object

Copy the necessary class instance variables to the subclass.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sequel/plugins/caching.rb', line 74

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

#set_cache_ttl(ttl) ⇒ Object

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



87
88
89
# File 'lib/sequel/plugins/caching.rb', line 87

def set_cache_ttl(ttl)
  @cache_ttl = ttl
end