Class: Tml::CacheAdapters::Cdn

Inherits:
Tml::Cache show all
Defined in:
lib/tml/cache_adapters/cdn.rb

Overview

– Copyright © 2015 Translation Exchange, Inc

_______                  _       _   _             ______          _

|__ __| | | | | (_) | __| | |

| |_ __ __ _ _ __  ___| | __ _| |_ _  ___  _ __ | |__  __  _____| |__   __ _ _ __   __ _  ___
| | '__/ _` | '_ \/ __| |/ _` | __| |/ _ \| '_ \|  __| \ \/ / __| '_ \ / _` | '_ \ / _` |/ _ \
| | | | (_| | | | \__ \ | (_| | |_| | (_) | | | | |____ >  < (__| | | | (_| | | | | (_| |  __/
|_|_|  \__,_|_| |_|___/_|\__,_|\__|_|\___/|_| |_|______/_/\_\___|_| |_|\__,_|_| |_|\__, |\___|
                                                                                    __/ |
                                                                                   |___/

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 Tml::Cache

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

Class Method Details

.cacheObject



35
36
37
# File 'lib/tml/cache_adapters/cdn.rb', line 35

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

.cache_pathObject



39
40
41
# File 'lib/tml/cache_adapters/cdn.rb', line 39

def self.cache_path
  "#{Tml.config.cache[:base_url]}/#{Tml.config.cache[:version]}"
end

.cdn_path(key) ⇒ Object



43
44
45
# File 'lib/tml/cache_adapters/cdn.rb', line 43

def self.cdn_path(key)
  File.join(cache_path, "#{key}.json")
end

Instance Method Details

#cache_nameObject



47
48
49
# File 'lib/tml/cache_adapters/cdn.rb', line 47

def cache_name
  'cdn'
end

#clear(opts = {}) ⇒ Object



93
94
95
# File 'lib/tml/cache_adapters/cdn.rb', line 93

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

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



85
86
87
# File 'lib/tml/cache_adapters/cdn.rb', line 85

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

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

Returns:

  • (Boolean)


89
90
91
# File 'lib/tml/cache_adapters/cdn.rb', line 89

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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tml/cache_adapters/cdn.rb', line 56

def fetch(key, opts = {})
  if self.class.cache[key]
    info("Memory hit: #{key}")
    return self.class.cache[key]
  end

  path = self.class.cdn_path(key)


  # load data from cdn


  if File.exists?(path)
    info("Cache hit: #{key}")
    self.class.cache[key] = JSON.parse(File.read(path))
    return self.class.cache[key]
  end

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

  return nil unless block_given?

  yield
end

#read_only?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/tml/cache_adapters/cdn.rb', line 97

def read_only?
  true
end

#segmented?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/tml/cache_adapters/cdn.rb', line 51

def segmented?
  return true if Tml.config.cache[:segmented].nil?
  Tml.config.cache[:segmented]
end

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



81
82
83
# File 'lib/tml/cache_adapters/cdn.rb', line 81

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