Class: Tml::Cache
- Inherits:
-
Object
- Object
- Tml::Cache
- Defined in:
- lib/tml/cache.rb
Direct Known Subclasses
Tml::CacheAdapters::File, Tml::CacheAdapters::Memcache, Tml::CacheAdapters::Redis
Instance Method Summary collapse
-
#cache_name ⇒ Object
name of the cache adapter.
-
#clear(opts = {}) ⇒ Object
clears cache.
-
#delete(key, opts = {}) ⇒ Object
deletes key from cache.
-
#enabled? ⇒ Boolean
checks if Tml is enabled.
-
#exist?(key, opts = {}) ⇒ Boolean
checks if the key exists.
-
#fetch(key, opts = {}) ⇒ Object
fetches key from cache.
-
#fetch_version ⇒ Object
fetches the version from the cache.
-
#info(msg) ⇒ Object
logs information messages.
-
#namespace ⇒ Object
namespace of each cache key.
-
#read_only? ⇒ Boolean
by default all cache is read/write cache like files based should be set to read only.
-
#reset_version ⇒ Object
resets current version.
-
#store(key, data, opts = {}) ⇒ Object
stores key in cache.
-
#store_version(new_version) ⇒ Object
stores the current version back in cache.
- #strip_extensions(data) ⇒ Object
-
#upgrade_version ⇒ Object
upgrade current version.
-
#version ⇒ Object
Returns current cache version.
-
#version=(new_version) ⇒ Object
sets the current version.
-
#versioned_key(key, opts = {}) ⇒ Object
versioned name of cache key.
-
#warn(msg) ⇒ Object
logs a warning.
Instance Method Details
#cache_name ⇒ Object
name of the cache adapter
100 101 102 |
# File 'lib/tml/cache.rb', line 100 def cache_name self.class.name.split('::').last end |
#clear(opts = {}) ⇒ Object
clears cache
147 148 149 |
# File 'lib/tml/cache.rb', line 147 def clear(opts = {}) # do nothing end |
#delete(key, opts = {}) ⇒ Object
deletes key from cache
137 138 139 |
# File 'lib/tml/cache.rb', line 137 def delete(key, opts = {}) # do nothing end |
#enabled? ⇒ Boolean
checks if Tml is enabled
89 90 91 |
# File 'lib/tml/cache.rb', line 89 def enabled? Tml.config.cache_enabled? end |
#exist?(key, opts = {}) ⇒ Boolean
checks if the key exists
142 143 144 |
# File 'lib/tml/cache.rb', line 142 def exist?(key, opts = {}) false end |
#fetch(key, opts = {}) ⇒ Object
fetches key from cache
126 127 128 129 |
# File 'lib/tml/cache.rb', line 126 def fetch(key, opts = {}) return nil unless block_given? yield end |
#fetch_version ⇒ Object
fetches the version from the cache
73 74 75 76 77 78 79 80 |
# File 'lib/tml/cache.rb', line 73 def fetch_version @version ||= begin v = fetch(CACHE_VERSION_KEY) do {'version' => Tml.config.cache[:version] || 'undefined'} end v.is_a?(Hash) ? v['version'] : v end end |
#info(msg) ⇒ Object
logs information messages
105 106 107 |
# File 'lib/tml/cache.rb', line 105 def info(msg) Tml.logger.info("#{cache_name} - #{msg}") end |
#namespace ⇒ Object
namespace of each cache key
115 116 117 118 |
# File 'lib/tml/cache.rb', line 115 def namespace return '#' if Tml.config.disabled? Tml.config.cache[:namespace] || Tml.config.access_token[0..5] end |
#read_only? ⇒ Boolean
by default all cache is read/write cache like files based should be set to read only
95 96 97 |
# File 'lib/tml/cache.rb', line 95 def read_only? false end |
#reset_version ⇒ Object
resets current version
62 63 64 |
# File 'lib/tml/cache.rb', line 62 def reset_version @version = nil end |
#store(key, data, opts = {}) ⇒ Object
stores key in cache
132 133 134 |
# File 'lib/tml/cache.rb', line 132 def store(key, data, opts = {}) # do nothing end |
#store_version(new_version) ⇒ Object
stores the current version back in cache
83 84 85 86 |
# File 'lib/tml/cache.rb', line 83 def store_version(new_version) @version = new_version store(CACHE_VERSION_KEY, {'version' => new_version}) end |
#strip_extensions(data) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/tml/cache.rb', line 151 def strip_extensions(data) if data.is_a?(Hash) data = data.dup data.delete('extensions') return data end if data.is_a?(String) and data.match(/^\{/) data = JSON.parse(data) data.delete('extensions') data = data.to_json end data end |
#upgrade_version ⇒ Object
upgrade current version
67 68 69 70 |
# File 'lib/tml/cache.rb', line 67 def upgrade_version store(CACHE_VERSION_KEY, {'version' => 'undefined'}) reset_version end |
#version ⇒ Object
Returns current cache version
52 53 54 |
# File 'lib/tml/cache.rb', line 52 def version @version end |
#version=(new_version) ⇒ Object
sets the current version
57 58 59 |
# File 'lib/tml/cache.rb', line 57 def version=(new_version) @version = new_version end |
#versioned_key(key, opts = {}) ⇒ Object
versioned name of cache key
121 122 123 |
# File 'lib/tml/cache.rb', line 121 def versioned_key(key, opts = {}) "tml_#{namespace}#{CACHE_VERSION_KEY == key ? '' : "_v#{version}"}_#{key}" end |