Module: CacheAndFetch::Cacheable

Extended by:
ActiveSupport::Concern
Included in:
Fetchable
Defined in:
lib/cache_and_fetch/cacheable.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEFAULT_CACHE_EXPIRY_TIME =
20.minutes
DEFAULT_PRIMARY_KEY_METHOD =
:id

Instance Method Summary collapse

Instance Method Details

#cacheObject



61
62
63
64
# File 'lib/cache_and_fetch/cacheable.rb', line 61

def cache
  self.cache_expires_at = self.class.cache_duration.since.to_i
  self.cache_client.write(self.cache_key, self)
end

#cache_clientObject



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

def cache_client
  self.class.cache_client
end

#cache_keyObject



57
58
59
# File 'lib/cache_and_fetch/cacheable.rb', line 57

def cache_key
  self.class.cache_key(self.__send__(self.class.primary_key))
end

#recacheObject



70
71
72
73
74
75
# File 'lib/cache_and_fetch/cacheable.rb', line 70

def recache
  Thread.new do
    p_key = self.__send__(self.class.primary_key)
    self.class.__send__(:find, p_key).cache
  end
end

#stale?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/cache_and_fetch/cacheable.rb', line 66

def stale?
  cache_expires_at && Time.now > Time.at(cache_expires_at)
end