trabajo-afectivo/lib/cache.rb

67 lines
935 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
2012-07-29 15:27:01 +00:00
def self.delete( key )
Rails.cache.delete( key.to_s )
end
=begin
write a cache
Cache.write(
'some_key',
{ some: { data: { 'structure' } } },
{
:expires_in => 24.hours, # optional, default 7 days
}
)
=end
2012-07-29 15:34:30 +00:00
def self.write( key, data, params = {} )
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
value = Cache.write('some_key')
=end
2012-07-29 15:27:01 +00:00
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