Module: Slugged::Caching::ClassMethods
- Defined in:
- lib/slugged/caching.rb
Instance Method Summary collapse
-
#cache_slug_lookup!(slug, record) ⇒ Object
Modify the cache for a given slug.
-
#find_using_slug(slug) ⇒ 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.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/slugged/caching.rb', line 63 def cache_slug_lookup!(slug, record) return if Slugged.cache.blank? cache = Slugged.cache key = slug_cache_key(slug) # Set an expires in option for caching. = Hash.new.tap do |hash| expiry = Slugged::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) ⇒ Object
Wraps find_using_slug to look in the cache.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/slugged/caching.rb', line 40 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
56 57 58 |
# File 'lib/slugged/caching.rb', line 56 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.
52 53 54 |
# File 'lib/slugged/caching.rb', line 52 def slug_cache_key(slug) [Slugged.cache_key_prefix, slug_scope_key(Digest::SHA256.hexdigest(slug.to_s.strip))].compact.join("/") end |