Class: Tr8n::CacheAdapters::Rails

Inherits:
Tr8n::Cache
  • Object
show all
Defined in:
lib/tr8n/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
38
# File 'lib/tr8n/cache_adapters/rails.rb', line 35

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

Instance Method Details

#cache_nameObject



40
41
42
# File 'lib/tr8n/cache_adapters/rails.rb', line 40

def cache_name
  @cache.class.name
end

#clear(opts = {}) ⇒ Object



93
94
95
96
97
# File 'lib/tr8n/cache_adapters/rails.rb', line 93

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

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



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

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)


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

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



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

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)


44
45
46
# File 'lib/tr8n/cache_adapters/rails.rb', line 44

def read_only?
  false
end

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



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

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