Class: MetalArchives::Cache::Redis

Inherits:
Base
  • Object
show all
Defined in:
lib/metal_archives/cache/redis.rb

Overview

Redis-backed cache

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#validate!

Constructor Details

#initialize(options = {}) ⇒ Redis

Returns a new instance of Redis.



11
12
13
14
15
16
# File 'lib/metal_archives/cache/redis.rb', line 11

def initialize(options = {})
  super

  # Default TTL is 1 month
  options[:ttl] ||= (30 * 24 * 60 * 60)
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/metal_archives/cache/redis.rb', line 18

def [](key)
  redis.get cache_key_for(key)
end

#[]=(key, value) ⇒ Object



22
23
24
# File 'lib/metal_archives/cache/redis.rb', line 22

def []=(key, value)
  redis.set cache_key_for(key), value, ex: options[:ttl]
end

#clearObject



26
27
28
# File 'lib/metal_archives/cache/redis.rb', line 26

def clear
  redis.keys(cache_key_for("*")).each { |key| redis.del key }
end

#delete(key) ⇒ Object



34
35
36
# File 'lib/metal_archives/cache/redis.rb', line 34

def delete(key)
  redis.del cache_key_for(key)
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/metal_archives/cache/redis.rb', line 30

def include?(key)
  redis.exists? cache_key_for(key)
end