class Tml::CacheVersion

Attributes

cache[RW]
version[RW]

Public Class Methods

new(cache) click to toggle source

Init cache version with cache adapter

# File lib/tml/cache_version.rb, line 42
def initialize(cache)
  self.cache = cache
end

Public Instance Methods

cache_timestamp() click to toggle source

generates cache timestamp based on an interval

# File lib/tml/cache_version.rb, line 107
def cache_timestamp
  Tml::Utils.interval_timestamp(version_check_interval)
end
defined?() click to toggle source

checks if version is defined

# File lib/tml/cache_version.rb, line 123
def defined?
  not undefined?
end
fetch() click to toggle source

fetches the version from the cache

# File lib/tml/cache_version.rb, line 92
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?() click to toggle source

checks if the version is invalid

# File lib/tml/cache_version.rb, line 133
def invalid?
  %w(undefined 0).include?(version.to_s)
end
reset() click to toggle source

reset cache version

# File lib/tml/cache_version.rb, line 47
def reset
  self.version = nil
end
set(new_version) click to toggle source

updates the current cache version

# File lib/tml/cache_version.rb, line 52
def set(new_version)
  self.version = new_version
end
store(new_version) click to toggle source

stores the current version back in cache

# File lib/tml/cache_version.rb, line 112
def store(new_version)
  self.version = new_version
  cache.store(CACHE_VERSION_KEY, {'version' => new_version, 't' => cache_timestamp})
end
to_s() click to toggle source

returns string representation of the version

# File lib/tml/cache_version.rb, line 143
def to_s
  self.version.to_s
end
undefined?() click to toggle source

checks if the version is undefined

# File lib/tml/cache_version.rb, line 118
def undefined?
  version.nil? or version == 'undefined'
end
upgrade() click to toggle source

upgrade current version

# File lib/tml/cache_version.rb, line 57
def upgrade
  cache.store(CACHE_VERSION_KEY, {'version' => 'undefined', 't' => cache_timestamp})
  reset
end
valid?() click to toggle source

checks if the version is valid

# File lib/tml/cache_version.rb, line 128
def valid?
  not invalid?
end
validate_cache_version(version) click to toggle source

validate that current cache version hasn't expired

# File lib/tml/cache_version.rb, line 63
def validate_cache_version(version)
  # if cache version is hardcoded, use it
  if Tml.config.cache[:version]
    return Tml.config.cache[:version]
  end

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

  # if version check interval is disabled, don't try to check for the new
  # cache version on the CDN
  if version_check_interval == -1
    Tml.logger.debug('Cache version check is disabled')
    return version['version']
  end

  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_interval() click to toggle source

how often should the cache be checked for

# File lib/tml/cache_version.rb, line 102
def version_check_interval
  Tml.config.cache[:version_check_interval] || 3600
end
versioned_key(key, namespace = '') click to toggle source

returns versioned key with prefix

# File lib/tml/cache_version.rb, line 138
def versioned_key(key, namespace = '')
  "tml_#{namespace}#{CACHE_VERSION_KEY == key ? '' : "_v#{version}"}_#{key}"
end