Class: Tr8n::CacheAdapters::File

Inherits:
Tr8n::Cache show all
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

Methods inherited from Tr8n::Cache

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

Class Method Details

.cache_pathObject



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

def self.cache_path
  "#{Tr8n.config.cache[:path]}/files/current"
end

.file_name(key) ⇒ Object



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

def self.file_name(key)
  "#{key.gsub(/[\.\/]/, '-')}.json"
end

.file_path(key) ⇒ Object



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

def self.file_path(key)
  "#{cache_path}/#{file_name(key)}"
end

Instance Method Details

#cache_nameObject



47
48
49
# File 'lib/tr8n/cache_adapters/file.rb', line 47

def cache_name
  "file"
end

#clear(opts = {}) ⇒ Object



78
79
80
# File 'lib/tr8n/cache_adapters/file.rb', line 78

def clear(opts = {})
  warn("This is a readonly cache")
end

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



70
71
72
# File 'lib/tr8n/cache_adapters/file.rb', line 70

def delete(key, opts = {})
  warn("This is a readonly cache")
end

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

Returns:

  • (Boolean)


74
75
76
# File 'lib/tr8n/cache_adapters/file.rb', line 74

def exist?(key, opts = {})
  File.exists?(self.class.file_path(key))
end

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



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

def fetch(key, opts = {})
  path = self.class.file_path(key)

  if File.exists?(path)
    info("Cache hit: #{key}")
    return File.read(path)
  end

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

  return nil unless block_given?

  yield
end

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



66
67
68
# File 'lib/tr8n/cache_adapters/file.rb', line 66

def store(key, data, opts = {})
  warn("This is a readonly cache")
end