Module: Kiosk::Cacheable::Resource::ClassMethods

Defined in:
lib/kiosk/cacheable/resource.rb

Instance Method Summary collapse

Instance Method Details

#cached_expire_in(expiry = nil, &blk) ⇒ Object

Specifies the length of time for which a resource should stay cached. Either a Fixnum (time in seconds) or block can be given. The block should accept the resource as its argument and should return the expiry time for the resource in seconds.

Keep in mind that the underlying cache store may not support expiration length. In that case, this option has no effect.



39
40
41
# File 'lib/kiosk/cacheable/resource.rb', line 39

def cached_expire_in(expiry = nil, &blk)
  connection.cache_expiry = expiry || blk
end

#connection(*args) ⇒ Object

Reimplements method to provide a cacheable connection.



45
46
47
48
49
# File 'lib/kiosk/cacheable/resource.rb', line 45

def connection(*args)
  connection = super(*args)
  connection.extend(Cacheable::Connection) unless connection.is_a?(Cacheable::Connection)
  connection
end

#expire(id) ⇒ Object

Expire from the cache the resource identified by the given id.



53
54
55
# File 'lib/kiosk/cacheable/resource.rb', line 53

def expire(id)
  connection.cache_expire_by_path(element_path(id))
end

#expire_by_slug(slug) ⇒ Object

Expire from the cache the resource identified by the given slug.



59
60
61
# File 'lib/kiosk/cacheable/resource.rb', line 59

def expire_by_slug(slug)
  connection.cache_expire_by_path(element_path_by_slug(slug))
end

#expire_resource(resource) ⇒ Object

Expire from the cache the resource identified by both the slug and id. Notify any observers of expiration.



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kiosk/cacheable/resource.rb', line 66

def expire_resource(resource)
  notify_observers(:before_expire, resource)

  expire(resource.id)
  expire_by_slug(resource.slug)

  begin
    all_connection_keys_to_expire.each { |key| connection.cache_expire_by_pattern(key) }
  rescue NotImplementedError
  end

  notify_observers(:after_expire, resource)
end

#expires_connection_methods(*methods) ⇒ Object

When a resource is explicitly expired from the cache, cache entries matching URLs to the given API method are deleted as well.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kiosk/cacheable/resource.rb', line 83

def expires_connection_methods(*methods)
  matchers = methods.map do |method|
    case connection.cache_key_matcher
    when :glob
      "#{api_path_to(method)}*"
    when :regexp
      /^#{Regexp.escape(api_path_to(method))}/
    end
  end

  expires_connection_keys(*matchers)
end