Class: Tr8n::CacheAdapters::Rails

Inherits:
Tr8n::Cache
  • Object
show all
Defined in:
lib/tr8n/cache_adapters/rails.rb

Instance Method Summary collapse

Constructor Details

#initializeRails

Returns a new instance of Rails.



37
38
39
40
# File 'lib/tr8n/cache_adapters/rails.rb', line 37

def initialize
  Tr8n.logger.info("Initializing Rails cache...")
  @cache = Rails.cache
end

Instance Method Details

#cache_nameObject



42
43
44
# File 'lib/tr8n/cache_adapters/rails.rb', line 42

def cache_name
  @cache.class.name
end

#clear(opts = {}) ⇒ Object



95
96
97
98
99
# File 'lib/tr8n/cache_adapters/rails.rb', line 95

def clear(opts = {})
  info("Cache clear")
rescue Exception => ex
  warn("Failed to clear cache: #{ex.message}")
end

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



78
79
80
81
82
83
84
85
# File 'lib/tr8n/cache_adapters/rails.rb', line 78

def delete(key, opts = {})
  info("Cache delete: #{key}")
  @cache.delete(versioned_key(key, opts))
  key
rescue Exception => ex
  warn("Failed to delete data: #{ex.message}")
  key
end

#exist?(key, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/tr8n/cache_adapters/rails.rb', line 87

def exist?(key, opts = {})
  data = @cache.fetch(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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tr8n/cache_adapters/rails.rb', line 50

def fetch(key, opts = {})
  miss = false
  data = @cache.fetch(versioned_key(key, opts)) do
    info("Cache miss: #{key}")
    miss = true
    if block_given?
      yield
    else
      nil
    end
  end
  info("Cache hit: #{key}") unless miss
  data
rescue Exception => ex
  warn("Failed to retrieve data: #{ex.message}")
  return nil unless block_given?
  yield
end

#read_only?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/tr8n/cache_adapters/rails.rb', line 46

def read_only?
  false
end

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



69
70
71
72
73
74
75
76
# File 'lib/tr8n/cache_adapters/rails.rb', line 69

def store(key, data, opts = {})
  info("Cache store: #{key}")
  @cache.write(versioned_key(key, opts), data)
  data
rescue Exception => ex
  warn("Failed to store data: #{ex.message}")
  key
end