Class: Tr8n::CacheAdapters::File
- Inherits:
-
Tr8n::Cache
- Object
- Tr8n::Cache
- Tr8n::CacheAdapters::File
- Defined in:
- lib/tr8n/cache_adapters/file.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
- #cache_name ⇒ Object
- #clear(opts = {}) ⇒ Object
- #delete(key, opts = {}) ⇒ Object
- #exist?(key, opts = {}) ⇒ Boolean
- #fetch(key, opts = {}) ⇒ Object
- #read_only? ⇒ Boolean
- #segmented? ⇒ Boolean
- #store(key, data, opts = {}) ⇒ Object
Methods inherited from Tr8n::Cache
#cached_by_source?, #enabled?, #info, #reset_version, #update_version, #upgrade_version, #version, #versioned_key, #warn
Class Method Details
.cache_path ⇒ Object
35 36 37 |
# File 'lib/tr8n/cache_adapters/file.rb', line 35 def self.cache_path "#{Tr8n.config.cache[:path]}/#{Tr8n.config.cache[:version]}" end |
.file_path(key) ⇒ Object
39 40 41 |
# File 'lib/tr8n/cache_adapters/file.rb', line 39 def self.file_path(key) File.join(cache_path, "#{key}.json") end |
Instance Method Details
#cache_name ⇒ Object
43 44 45 |
# File 'lib/tr8n/cache_adapters/file.rb', line 43 def cache_name 'file' end |
#clear(opts = {}) ⇒ Object
81 82 83 |
# File 'lib/tr8n/cache_adapters/file.rb', line 81 def clear(opts = {}) warn('This is a readonly cache') end |
#delete(key, opts = {}) ⇒ Object
73 74 75 |
# File 'lib/tr8n/cache_adapters/file.rb', line 73 def delete(key, opts = {}) warn('This is a readonly cache') end |
#exist?(key, opts = {}) ⇒ Boolean
77 78 79 |
# File 'lib/tr8n/cache_adapters/file.rb', line 77 def exist?(key, opts = {}) File.exists?(self.class.file_path(key)) end |
#fetch(key, opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/tr8n/cache_adapters/file.rb', line 52 def fetch(key, opts = {}) info("Fetching key: #{key}") path = self.class.file_path(key) if File.exists?(path) info("Cache hit: #{key}") return JSON.parse(File.read(path)) end info("Cache miss: #{key}") return nil unless block_given? yield end |
#read_only? ⇒ Boolean
85 86 87 |
# File 'lib/tr8n/cache_adapters/file.rb', line 85 def read_only? true end |
#segmented? ⇒ Boolean
47 48 49 50 |
# File 'lib/tr8n/cache_adapters/file.rb', line 47 def segmented? return true if Tr8n.config.cache[:segmented].nil? Tr8n.config.cache[:segmented] end |
#store(key, data, opts = {}) ⇒ Object
69 70 71 |
# File 'lib/tr8n/cache_adapters/file.rb', line 69 def store(key, data, opts = {}) warn('This is a readonly cache') end |