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
107 108 109 |
# File 'lib/tml/cache_version.rb', line 107 def Tml::Utils.(version_check_interval) end |
#defined? ⇒ Boolean
checks if version is defined
123 124 125 |
# File 'lib/tml/cache_version.rb', line 123 def defined? not undefined? end |
#fetch ⇒ Object
fetches the version from the cache
92 93 94 95 96 97 98 99 |
# 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' => } end validate_cache_version(ver) end end |
#invalid? ⇒ Boolean
checks if the version is invalid
133 134 135 |
# File 'lib/tml/cache_version.rb', line 133 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
112 113 114 115 |
# 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' => }) end |
#to_s ⇒ Object
returns string representation of the version
143 144 145 |
# File 'lib/tml/cache_version.rb', line 143 def to_s self.version.to_s end |
#undefined? ⇒ Boolean
checks if the version is undefined
118 119 120 |
# File 'lib/tml/cache_version.rb', line 118 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
128 129 130 |
# File 'lib/tml/cache_version.rb', line 128 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 78 79 80 81 82 83 84 85 86 87 88 89 |
# 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 ⇒ Object
how often should the cache be checked for
102 103 104 |
# File 'lib/tml/cache_version.rb', line 102 def version_check_interval Tml.config.cache[:version_check_interval] || 3600 end |
#versioned_key(key, namespace = '') ⇒ Object
returns versioned key with prefix
138 139 140 |
# File 'lib/tml/cache_version.rb', line 138 def versioned_key(key, namespace = '') "tml_#{namespace}#{CACHE_VERSION_KEY == key ? '' : "_v#{version}"}_#{key}" end |