2012-07-29 15:27:01 +00:00
|
|
|
module Cache
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
delete a cache
|
|
|
|
|
|
|
|
Cache.delete( 'some_key' )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-07-29 15:27:01 +00:00
|
|
|
def self.delete( key )
|
2012-10-23 19:19:07 +00:00
|
|
|
# puts 'Cache.delete' + key.to_s
|
2012-07-29 15:27:01 +00:00
|
|
|
Rails.cache.delete( key.to_s )
|
|
|
|
end
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
write a cache
|
|
|
|
|
|
|
|
Cache.write(
|
|
|
|
'some_key',
|
|
|
|
{ :some => { :data => { 'structure' } } },
|
|
|
|
{
|
|
|
|
:expires_in => 24.hours, # optional
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-07-29 15:34:30 +00:00
|
|
|
def self.write( key, data, params = {} )
|
|
|
|
if !params[:expires_in]
|
2012-07-29 18:55:51 +00:00
|
|
|
params[:expires_in] = 24.hours
|
2012-07-29 15:34:30 +00:00
|
|
|
end
|
2012-10-23 19:19:07 +00:00
|
|
|
# puts 'Cache.write: ' + key.to_s
|
2014-11-06 20:44:13 +00:00
|
|
|
begin
|
|
|
|
Rails.cache.write( key.to_s, data, params)
|
|
|
|
rescue Exception => e
|
|
|
|
puts "NOTICE: #{e.message}"
|
|
|
|
end
|
2012-07-29 15:27:01 +00:00
|
|
|
end
|
2015-02-25 20:52:14 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get a cache
|
|
|
|
|
|
|
|
value = Cache.write( 'some_key' )
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-07-29 15:27:01 +00:00
|
|
|
def self.get( key )
|
2012-10-23 19:19:07 +00:00
|
|
|
# puts 'Cache.get: ' + key.to_s
|
2012-07-29 15:27:01 +00:00
|
|
|
Rails.cache.read( key.to_s )
|
|
|
|
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
|
2012-10-23 19:19:07 +00:00
|
|
|
# puts 'Cache.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
|
2014-11-06 20:44:13 +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
|