Module: Pseudocephalopod::Caching::ClassMethods
- Defined in:
- lib/pseudocephalopod/caching.rb
Instance Method Summary collapse
-
#cache_slug_lookup!(slug, record) ⇒ Object
Modify the cache for a given slug.
-
#find_using_slug(slug, options = {}) ⇒ Object
Wraps find_using_slug to look in the cache.
- #has_cache_for_slug?(slug) ⇒ Boolean
-
#slug_cache_key(slug) ⇒ Object
Returns a slug cache key for a given slug.
Instance Method Details
#cache_slug_lookup!(slug, record) ⇒ Object
Modify the cache for a given slug. If record is nil, it will delete the item from the slug cache, otherwise it will store the records id.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/pseudocephalopod/caching.rb', line 66 def cache_slug_lookup!(slug, record) return if Pseudocephalopod.cache.blank? cache = Pseudocephalopod.cache key = slug_cache_key(slug) # Set an expires in option for caching. = Hash.new.tap do |hash| expiry = Pseudocephalopod::Caching.cache_expires_in hash[:expires_in] = expiry.to_i if expiry.present? end record.nil? ? cache.delete(key) : cache.write(key, record.id, ) end |
#find_using_slug(slug, options = {}) ⇒ Object
Wraps find_using_slug to look in the cache.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pseudocephalopod/caching.rb', line 43 def find_using_slug(slug, = {}) # First, attempt to load an id and then record from the cache. if (cached_id = lookup_cached_id_from_slug(slug)).present? return find(cached_id, ).tap { |r| r.found_via_slug = slug } end # Otherwise, fallback to the normal approach. super.tap do |record| cache_slug_lookup!(slug, record) if record.present? end end |
#has_cache_for_slug?(slug) ⇒ Boolean
59 60 61 |
# File 'lib/pseudocephalopod/caching.rb', line 59 def has_cache_for_slug?(slug) lookup_cached_id_from_slug(slug).present? end |
#slug_cache_key(slug) ⇒ Object
Returns a slug cache key for a given slug.
55 56 57 |
# File 'lib/pseudocephalopod/caching.rb', line 55 def slug_cache_key(slug) [Pseudocephalopod.cache_key_prefix, slug_scope_key(Digest::SHA256.hexdigest(slug.to_s.strip))].compact.join("/") end |