class Tml::CacheAdapters::Redis
Public Class Methods
new()
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 38 def initialize config = Tml.config.cache if config[:adapter_config] @cache = ::Redis.new(config[:adapter_config]) else config[:host] ||= 'localhost' config[:port] ||= 6379 if config[:host].index(':') parts = config[:host].split(':') config[:host] = parts.first config[:port] = parts.last end @cache = ::Redis.new(config) end end
Public Instance Methods
cache_name()
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 57 def cache_name 'redis' end
clear(opts = {})
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 121 def clear(opts = {}) info('Cache clear has no effect') end
delete(key, opts = {})
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 105 def delete(key, opts = {}) info("Cache delete: #{key}") @cache.del(versioned_key(key, opts)) rescue Exception => ex warn("Failed to delete data: #{ex.message}") key end
exist?(key, opts = {})
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 113 def exist?(key, opts = {}) data = @cache.exist(versioned_key(key, opts)) not data.nil? rescue Exception => ex warn("Failed to check if key exists: #{ex.message}") false end
fetch(key, opts = {}) { || ... }
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 65 def fetch(key, opts = {}) data = @cache.get(versioned_key(key, opts)) if data info("Cache hit: #{key}") begin return JSON.parse(data) rescue Exception => ex warn("Failed to parse data: #{ex.message}") end end info("Cache miss: #{key}") return nil unless block_given? data = yield store(key, data) data rescue Exception => ex warn("Failed to retrieve data: #{ex.message}") return nil unless block_given? yield end
read_only?()
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 61 def read_only? false end
store(key, data, opts = {})
click to toggle source
# File lib/tml/cache_adapters/redis.rb, line 92 def store(key, data, opts = {}) info("Cache store: #{key}") ttl = opts[:ttl] || Tml.config.cache[:timeout] versioned_key = versioned_key(key, opts) @cache.set(versioned_key, strip_extensions(data.to_json)) @cache.expire(versioned_key, ttl) if ttl and ttl > 0 rescue Exception => ex warn("Failed to store data: #{ex.message}") data end