Class: Tml::CacheVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/cache_version.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache) ⇒ CacheVersion

Init cache version with cache adapter



42
43
44
# File 'lib/tml/cache_version.rb', line 42

def initialize(cache)
  self.cache = cache
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



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

def cache
  @cache
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#cache_timestampObject

generates cache timestamp based on an interval



95
96
97
# File 'lib/tml/cache_version.rb', line 95

def cache_timestamp
  Tml::Utils.interval_timestamp(version_check_interval)
end

#defined?Boolean

checks if version is defined



111
112
113
# File 'lib/tml/cache_version.rb', line 111

def defined?
  not undefined?
end

#fetchObject

fetches the version from the cache



80
81
82
83
84
85
86
87
# File 'lib/tml/cache_version.rb', line 80

def fetch
  self.version = begin
    ver = cache.fetch(CACHE_VERSION_KEY) do
      {'version' => Tml.config.cache[:version] || 'undefined', 't' => cache_timestamp}
    end
    validate_cache_version(ver)
  end
end

#invalid?Boolean

checks if the version is invalid



121
122
123
# File 'lib/tml/cache_version.rb', line 121

def invalid?
  %w(undefined 0).include?(version.to_s)
end

#resetObject

reset cache version



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

def reset
  self.version = nil
end

#set(new_version) ⇒ Object

updates the current cache version



52
53
54
# File 'lib/tml/cache_version.rb', line 52

def set(new_version)
  self.version = new_version
end

#store(new_version) ⇒ Object

stores the current version back in cache



100
101
102
103
# File 'lib/tml/cache_version.rb', line 100

def store(new_version)
  self.version = new_version
  cache.store(CACHE_VERSION_KEY, {'version' => new_version, 't' => cache_timestamp})
end

#to_sObject

returns string representation of the version



131
132
133
# File 'lib/tml/cache_version.rb', line 131

def to_s
  self.version.to_s
end

#undefined?Boolean

checks if the version is undefined



106
107
108
# File 'lib/tml/cache_version.rb', line 106

def undefined?
  version.nil? or version == 'undefined'
end

#upgradeObject

upgrade current version



57
58
59
60
# File 'lib/tml/cache_version.rb', line 57

def upgrade
  cache.store(CACHE_VERSION_KEY, {'version' => 'undefined', 't' => cache_timestamp})
  reset
end

#valid?Boolean

checks if the version is valid



116
117
118
# File 'lib/tml/cache_version.rb', line 116

def valid?
  not invalid?
end

#validate_cache_version(version) ⇒ Object

validate that current cache version hasn’t expired



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tml/cache_version.rb', line 63

def validate_cache_version(version)
  return version unless version.is_a?(Hash)
  return 'undefined' unless version['t'].is_a?(Numeric)
  return version['version'] if cache.read_only?

  expires_at = version['t'] + version_check_interval
  if expires_at < Time.now.to_i
    Tml.logger.debug('Cache version is outdated, needs refresh')
    return 'undefined'
  end

  delta = expires_at - Time.now.to_i
  Tml.logger.debug("Cache version is up to date, expires in #{delta}s")
  version['version']
end

#version_check_intervalObject

how often should the cache be checked for



90
91
92
# File 'lib/tml/cache_version.rb', line 90

def version_check_interval
  Tml.config.cache[:version_check_interval] || 3600
end

#versioned_key(key, namespace = '') ⇒ Object

returns versioned key with prefix



126
127
128
# File 'lib/tml/cache_version.rb', line 126

def versioned_key(key, namespace = '')
  "tml_#{namespace}#{CACHE_VERSION_KEY == key ? '' : "_v#{version}"}_#{key}"
end