class Cache
Cache
class for HandsetDetection
Notes :
-
Cache
objects may be > 1Mb when serialized so ensure memcache or memcached can handle it.
-
Public Class Methods
new(config={})
click to toggle source
# File lib/handset_detection/cache.rb, line 40 def initialize(config={}) @prefix = nil @ttl = nil @cache = nil set_config config end
Public Instance Methods
delete(key)
click to toggle source
Remove a cache key (and its data)
param
string $key return
true on success, false otherwise
# File lib/handset_detection/cache.rb, line 96 def delete(key) @cache.del @prefix + key end
purge()
click to toggle source
Flush the whole cache
param
void return
true on success, false otherwise
# File lib/handset_detection/cache.rb, line 105 def purge @cache.flush end
read(key)
click to toggle source
Fetch a cache key
param
string $key return
value on success, null otherwise
# File lib/handset_detection/cache.rb, line 77 def read(key) @cache.get @prefix + key end
set_config(config)
click to toggle source
Set config file
param
array $config An assoc array of config data return
true on success, false otherwise
# 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)
click to toggle source
Store
a data at $key
param
string $key param
mixed $data return
true on success, false otherwise
# File lib/handset_detection/cache.rb, line 87 def write(key, data) @cache.set @prefix + key, data, @duration end