trabajo-afectivo/lib/cache.rb

65 lines
912 B
Ruby
Raw Normal View History

2012-07-29 15:27:01 +00:00
module Cache
=begin
delete a cache
Cache.delete('some_key')
=end
def self.delete(key)
Rails.cache.delete(key.to_s)
2012-07-29 15:27:01 +00:00
end
=begin
write a cache
Cache.write(
'some_key',
{ some: { data: { 'structure' } } },
{ expires_in: 24.hours, # optional, default 7 days }
)
=end
def self.write(key, data, params = {})
2012-07-29 15:34:30 +00:00
if !params[:expires_in]
params[:expires_in] = 7.days
2012-07-29 15:34:30 +00:00
end
begin
Rails.cache.write(key.to_s, data, params)
rescue => e
2015-05-04 18:58:28 +00:00
Rails.logger.error "NOTICE: #{e.message}"
end
2012-07-29 15:27:01 +00:00
end
=begin
get a cache
2015-08-31 12:34:17 +00:00
value = Cache.get('some_key')
=end
def self.get(key)
Rails.cache.read(key.to_s)
2012-07-29 15:27:01 +00:00
end
=begin
clear whole cache store
Cache.clear
=end
2012-07-29 18:55:51 +00:00
def self.clear
# workaround, set test cache before clear whole cache, Rails.cache.clear complains about not existing cache dir
Cache.write('test', 1)
2012-07-29 18:55:51 +00:00
Rails.cache.clear
end
end