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?, #deserialize_object, #enabled?, #info, #read_only?, #reset_version, #serialize_object, #upgrade_version, #version, #versioned_key, #warn

Class Method Details

.cache_pathObject



27
28
29
# File 'lib/tr8n/cache_adapters/file.rb', line 27

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

.file_name(key) ⇒ Object



31
32
33
# File 'lib/tr8n/cache_adapters/file.rb', line 31

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

.file_path(key) ⇒ Object



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

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

Instance Method Details

#cache_nameObject



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

def cache_name
  "file"
end

#clear(opts = {}) ⇒ Object



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

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

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



63
64
65
# File 'lib/tr8n/cache_adapters/file.rb', line 63

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

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

Returns:

  • (Boolean)


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

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

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tr8n/cache_adapters/file.rb', line 43

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

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

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

  return nil unless block_given?

  yield
end

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



59
60
61
# File 'lib/tr8n/cache_adapters/file.rb', line 59

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