Class: Tr8n::CacheAdapters::Memory

Inherits:
Tr8n::Cache show all
Defined in:
lib/tr8n/cache_adapters/memory.rb

Overview

– Copyright © 2014 Michael Berkovich, 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. ++

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tr8n::Cache

#cached_by_source?, #enabled?, #info, #read_only?, #reset_version, #segmented?, #update_version, #upgrade_version, #version, #versioned_key, #warn

Class Method Details

.cacheObject



35
36
37
# File 'lib/tr8n/cache_adapters/memory.rb', line 35

def self.cache
  @cache ||= {}
end

.clear_cacheObject



39
40
41
# File 'lib/tr8n/cache_adapters/memory.rb', line 39

def self.clear_cache
  @cache = {}
end

Instance Method Details

#cache_nameObject



43
44
45
# File 'lib/tr8n/cache_adapters/memory.rb', line 43

def cache_name
  'memory'
end

#clear(opts = {}) ⇒ Object



81
82
83
# File 'lib/tr8n/cache_adapters/memory.rb', line 81

def clear(opts = {})
  self.class.clear_cache
end

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



73
74
75
# File 'lib/tr8n/cache_adapters/memory.rb', line 73

def delete(key, opts = {})
  self.class.cache[key] = nil
end

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

Returns:

  • (Boolean)


77
78
79
# File 'lib/tr8n/cache_adapters/memory.rb', line 77

def exist?(key, opts = {})
  not self.class.cache[key].nil?
end

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



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

def fetch(key, opts = {})
  return yield if Tr8n.session.inline_mode?
  return yield unless Tr8n.cache.read_only?

  data = self.class.cache[key]

  if data
    info("Cache hit: #{key}")
    return data
  end

  info("Cache miss: #{key}")

  return nil unless block_given?

  data = yield

  store(key, data)

  data
end

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



69
70
71
# File 'lib/tr8n/cache_adapters/memory.rb', line 69

def store(key, data, opts = {})
  self.class.cache[key] = data
end