Class: Tml::CacheAdapters::Redis

Inherits:
Tml::Cache show all
Defined in:
lib/tml/cache_adapters/redis.rb

Instance Method Summary collapse

Methods inherited from Tml::Cache

#default_cache_path, #download, #enabled?, #extract_version, #info, #namespace, #namespace=, #reset_version, #strip_extensions, #upgrade_version, #version, #versioned_key, #warmup, #warmup_from_cdn, #warmup_from_files, #warn

Constructor Details

#initializeRedis

Returns a new instance of Redis.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

Instance Method Details

#cache_nameObject



57
58
59
# File 'lib/tml/cache_adapters/redis.rb', line 57

def cache_name
  'redis'
end

#clear(opts = {}) ⇒ Object



121
122
123
# File 'lib/tml/cache_adapters/redis.rb', line 121

def clear(opts = {})
  info('Cache clear has no effect')
end

#delete(key, opts = {}) ⇒ Object



105
106
107
108
109
110
111
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
# 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 = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/tml/cache_adapters/redis.rb', line 61

def read_only?
  false
end

#store(key, data, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# 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