2012-07-29 15:27:01 +00:00
|
|
|
module Cache
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
delete a cache
|
|
|
|
|
2015-06-24 11:33:07 +00:00
|
|
|
Cache.delete('some_key')
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-13 13:12:05 +00:00
|
|
|
def self.delete(key)
|
|
|
|
Rails.cache.delete(key.to_s)
|
2012-07-29 15:27:01 +00:00
|
|
|
end
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
write a cache
|
|
|
|
|
|
|
|
Cache.write(
|
|
|
|
'some_key',
|
2015-06-24 11:33:07 +00:00
|
|
|
{ some: { data: { 'structure' } } },
|
2016-01-13 13:12:05 +00:00
|
|
|
{ expires_in: 24.hours, # optional, default 7 days }
|
2015-02-25 20:52:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-13 13:12:05 +00:00
|
|
|
def self.write(key, data, params = {})
|
2012-07-29 15:34:30 +00:00
|
|
|
if !params[:expires_in]
|
2015-06-24 11:33:07 +00:00
|
|
|
params[:expires_in] = 7.days
|
2012-07-29 15:34:30 +00:00
|
|
|
end
|
2016-06-15 08:53:01 +00:00
|
|
|
|
|
|
|
# in certain cases, caches are deleted by other thread at same
|
|
|
|
# time, just log it
|
|
|
|
begin
|
|
|
|
Rails.cache.write(key.to_s, data, params)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "Can't write cache #{key}: #{e.inspect}"
|
|
|
|
end
|
2012-07-29 15:27:01 +00:00
|
|
|
end
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get a cache
|
|
|
|
|
2015-08-31 12:34:17 +00:00
|
|
|
value = Cache.get('some_key')
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-01-13 13:12:05 +00:00
|
|
|
def self.get(key)
|
2015-06-24 11:33:07 +00:00
|
|
|
Rails.cache.read(key.to_s)
|
2012-07-29 15:27:01 +00:00
|
|
|
end
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
clear whole cache store
|
|
|
|
|
|
|
|
Cache.clear
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-07-29 18:55:51 +00:00
|
|
|
def self.clear
|
2014-08-03 13:06:43 +00:00
|
|
|
# workaround, set test cache before clear whole cache, Rails.cache.clear complains about not existing cache dir
|
2015-06-24 11:33:07 +00:00
|
|
|
Cache.write('test', 1)
|
2014-08-03 13:06:43 +00:00
|
|
|
|
2012-07-29 18:55:51 +00:00
|
|
|
Rails.cache.clear
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|