Class: Cache
Overview
Cache class for HandsetDetection
Notes :
-
Cache objects may be > 1Mb when serialized so ensure memcache or memcached can handle it.
-
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Remove a cache key (and its data).
-
#initialize(config = {}) ⇒ Cache
constructor
A new instance of Cache.
-
#purge ⇒ Object
Flush the whole cache.
-
#read(key) ⇒ Object
Fetch a cache key.
-
#set_config(config) ⇒ Object
Set config file.
-
#write(key, data) ⇒ Object
Store a data at $key.
Constructor Details
#initialize(config = {}) ⇒ Cache
Returns a new instance of Cache.
40 41 42 43 44 45 |
# File 'lib/handset_detection/cache.rb', line 40 def initialize(config={}) @prefix = nil @ttl = nil @cache = nil set_config config end |
Instance Method Details
#delete(key) ⇒ Object
Remove a cache key (and its data)
param
string $key return
true on success, false otherwise
96 97 98 |
# File 'lib/handset_detection/cache.rb', line 96 def delete(key) @cache.del @prefix + key end |
#purge ⇒ Object
Flush the whole cache
param
void return
true on success, false otherwise
105 106 107 |
# File 'lib/handset_detection/cache.rb', line 105 def purge @cache.flush end |
#read(key) ⇒ Object
Fetch a cache key
param
string $key return
value on success, null otherwise
77 78 79 |
# File 'lib/handset_detection/cache.rb', line 77 def read(key) @cache.get @prefix + key end |
#set_config(config) ⇒ Object
Set config file
param
array $config An assoc array of config data return
true on success, false otherwise
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/handset_detection/cache.rb', line 52 def set_config(config) @config = config @prefix = (config.include?('cache') and config['cache'].include?('prefix')) ? config['cache']['prefix'] : 'hd40' @duration = (config.include?('cache') and config['cache'].include?('ttl')) ? config['cache']['ttl'] : 7200 if config.include?('cache') and config['cache'].include?('memcached') @cache = Memcached.new(@config) elsif config.include?('cache') and config['cache'].include?('rails') @cache = RailsCache.new(@config) elsif config.include?('cache') and config['cache'].include?('none') @cache = None.new(@config) elsif config.include?('cache') and config['cache'].include?('memory') @cache = Memory.new(@config) elsif defined? Rails @cache = RailsCache.new(@config) else @cache = FileSystem.new(@config) end true end |
#write(key, data) ⇒ Object
Store a data at $key
param
string $key param
mixed $data return
true on success, false otherwise
87 88 89 |
# File 'lib/handset_detection/cache.rb', line 87 def write(key, data) @cache.set @prefix + key, data, @duration end |