class Px::Service::Client::Caching::CacheEntry
Attributes
cache_client[R]
data[RW]
expires_at[RW]
policy_group[RW]
url[RW]
Public Class Methods
fetch(cache_client, url, policy_group)
click to toggle source
Fetch an entry from the cache. Returns the entry if it's present, otherwise returns nil
# File lib/px/service/client/caching/cache_entry.rb, line 37 def self.fetch(cache_client, url, policy_group) raise ArgumentError.new('Cache client has not been set.') unless cache_client.present? key_values = nil data_key = cache_key(url, policy_group, :data) meta_key = cache_key(url, policy_group, :meta) ActiveSupport::Notifications.instrument("get.caching", { url: url, policy_group: policy_group } ) do key_values = cache_client.get_multi(data_key, meta_key) end data_json = key_values[data_key] meta_json = key_values[meta_key] if data_json && meta_json data = JSON.parse(data_json) meta = JSON.parse(meta_json) CacheEntry.new(cache_client, meta['url'], meta['pg'], data, meta['expires_at']) else nil end end
new(cache_client, url, policy_group, data, expires_at = nil)
click to toggle source
# File lib/px/service/client/caching/cache_entry.rb, line 6 def initialize(cache_client, url, policy_group, data, expires_at = nil) @cache_client = cache_client self.url = url self.data = data self.expires_at = expires_at self.policy_group = policy_group end
Private Class Methods
cache_key(url, policy_group, type)
click to toggle source
# File lib/px/service/client/caching/cache_entry.rb, line 87 def self.cache_key(url, policy_group, type) "#{policy_group}_#{cache_key_base(url)}_#{type}" end
cache_key_base(url)
click to toggle source
Get the cache key for the given query string
# File lib/px/service/client/caching/cache_entry.rb, line 93 def self.cache_key_base(url) md5 = Digest::MD5.hexdigest(url.to_s) "#{self.class.name.parameterize}_#{md5}" end
Public Instance Methods
expired?()
click to toggle source
# File lib/px/service/client/caching/cache_entry.rb, line 14 def expired? expires_at < DateTime.now end
store(expires_in, refresh_window: 5.minutes)
click to toggle source
Store this entry in the cache with the given expiry.
# File lib/px/service/client/caching/cache_entry.rb, line 20 def store(expires_in, refresh_window: 5.minutes) raise ArgumentError.new('Cache client has not been set.') unless cache_client.present? self.expires_at = DateTime.now + expires_in ActiveSupport::Notifications.instrument("store.caching", { url: url, policy_group: policy_group, expires_in: expires_in} ) do real_expiry = real_cache_expiry(expires_in, refresh_window: refresh_window) cache_client.multi do data_json = data.is_a?(Hash) ? data.to_json : data cache_client.set(cache_key(:data), data_json, real_expiry) cache_client.set(cache_key(:meta), metadata, real_expiry) end end end
touch(expires_in, refresh_window: 5.minutes)
click to toggle source
Touch this entry in the cache, updating its expiry time but not its data
# File lib/px/service/client/caching/cache_entry.rb, line 60 def touch(expires_in, refresh_window: 5.minutes) raise ArgumentError.new('Cache client has not been set.') unless cache_client.present? self.expires_at = DateTime.now + expires_in ActiveSupport::Notifications.instrument("touch.caching", { url: url, policy_group: policy_group, expires_in: expires_in} ) do real_expiry = real_cache_expiry(expires_in, refresh_window: refresh_window) cache_client.touch(cache_key(:data), real_expiry) cache_client.set(cache_key(:meta), metadata, real_expiry) end end
Private Instance Methods
cache_key(type)
click to toggle source
# File lib/px/service/client/caching/cache_entry.rb, line 83 def cache_key(type) self.class.cache_key(url, policy_group, type) end
metadata()
click to toggle source
# File lib/px/service/client/caching/cache_entry.rb, line 75 def metadata { "url" => url, "pg" => policy_group, "expires_at" => expires_at, }.to_json end
real_cache_expiry(expires_in, refresh_window: nil)
click to toggle source
# File lib/px/service/client/caching/cache_entry.rb, line 98 def real_cache_expiry(expires_in, refresh_window: nil) (expires_in + refresh_window).to_i end