Class: Tml::CacheAdapters::Rails

Inherits:
Tml::Cache
  • Object
show all
Defined in:
lib/tml/cache_adapters/rails.rb

Overview

– Copyright © 2015 Translation Exchange Inc. translationexchange.com

_______                  _       _   _             ______          _

|__ __| | | | | (_) | __| | |

| |_ __ __ _ _ __  ___| | __ _| |_ _  ___  _ __ | |__  __  _____| |__   __ _ _ __   __ _  ___
| | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \|  __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
| | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ >  < (__| | | | (_| | | | | (_| |  __/
|_|_|  \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
                                                                                    __/ |
                                                                                   |___/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Instance Method Summary collapse

Constructor Details

#initializeRails

Returns a new instance of Rails.



35
36
37
# File 'lib/tml/cache_adapters/rails.rb', line 35

def initialize
  @cache = Rails.cache
end

Instance Method Details

#cache_nameObject



39
40
41
# File 'lib/tml/cache_adapters/rails.rb', line 39

def cache_name
  @cache.class.name
end

#clear(opts = {}) ⇒ Object



92
93
94
95
96
# File 'lib/tml/cache_adapters/rails.rb', line 92

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

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



75
76
77
78
79
80
81
82
# File 'lib/tml/cache_adapters/rails.rb', line 75

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)


84
85
86
87
88
89
90
# File 'lib/tml/cache_adapters/rails.rb', line 84

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tml/cache_adapters/rails.rb', line 47

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)


43
44
45
# File 'lib/tml/cache_adapters/rails.rb', line 43

def read_only?
  false
end

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



66
67
68
69
70
71
72
73
# File 'lib/tml/cache_adapters/rails.rb', line 66

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