Class: Tml::CacheVersion
- Inherits:
-
Object
- Object
- Tml::CacheVersion
- Defined in:
- lib/tml/cache_version.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#cache_timestamp ⇒ Object
generates cache timestamp based on an interval.
-
#defined? ⇒ Boolean
checks if version is defined.
-
#fetch ⇒ Object
fetches the version from the cache.
-
#initialize(cache) ⇒ CacheVersion
constructor
Init cache version with cache adapter.
-
#invalid? ⇒ Boolean
checks if the version is invalid.
-
#reset ⇒ Object
reset cache version.
-
#set(new_version) ⇒ Object
updates the current cache version.
-
#store(new_version) ⇒ Object
stores the current version back in cache.
-
#to_s ⇒ Object
returns string representation of the version.
-
#undefined? ⇒ Boolean
checks if the version is undefined.
-
#upgrade ⇒ Object
upgrade current version.
-
#valid? ⇒ Boolean
checks if the version is valid.
-
#validate_cache_version(version) ⇒ Object
validate that current cache version hasn’t expired.
-
#version_check_interval ⇒ Object
how often should the cache be checked for.
-
#versioned_key(key, namespace = '') ⇒ Object
returns versioned key with prefix.
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
#cache ⇒ Object
Returns the value of attribute cache.
39 40 41 |
# File 'lib/tml/cache_version.rb', line 39 def cache @cache end |
#version ⇒ Object
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_timestamp ⇒ Object
generates cache timestamp based on an interval
95 96 97 |
# File 'lib/tml/cache_version.rb', line 95 def Tml::Utils.(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 |
#fetch ⇒ Object
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' => } 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 |
#reset ⇒ Object
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' => }) end |
#to_s ⇒ Object
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 |
#upgrade ⇒ Object
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' => }) 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_interval ⇒ Object
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 |