0.01
No commit activity in last 3 years
No release in over 3 years
How to design a perfect cache key? This project's goal is to provide a bulletproof solution for most use cases.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 0.8.13
~> 2.0.0
~> 0.10.3
~> 3.3.0
~> 0.11.2
~> 1.3
 Project Readme

CacheKeyFor Gem Version Build Status Coverage Status Code Climate

Comprehensive cache key generator (complements Rails cache helper)

How to design a perfect cache key? This project's goal is to provide the bulletproof solution for most use cases.

Install

cache_key_for is a Railtie. We support the versions of Ruby and Rails listed in .travis.yml.

It should be run as a gem and included in your Gemfile:

gem "cache_key_for"

#cache_key_for

Features:

  • ORM agnostic

  • works with arrays and Plain Old Ruby Objects (POROs) (just provide: #id, #updated_at)

  • supports locale

  • recognizes subdomains

  • deletion from and addition to collections sorted in ascending order (via embedded count in the key)

  • accepts cache_owner_cache_key for personalized cache, eg. current_company.cache_key, current_user.cache_key etc.

  • filters params with proper non-utf8 data handling for key generation

  • supports paginated and not paginated collections or arrays of objects

  • recognizes pagination via params (performs well for less than 100 objects per page)

  • allows to set default page and per_page or sort order or any param in default_params to avoid multiple different default caches

  • includes all params, not only GET's query params, which enables submitting of complex forms via POST, which - otherwise - would have query string longer than 2048 characters (Microsoft Internet Explorer)

  • optional whitelist of first level parameters to prevent accidentally generating duplicated cache

  • strips utm_* params

  • optional suffix for:

    • expiry tags (unlike :expires_in cache option, this offers full :file_store storage compatibility)
    • any edge cases
  • file differentiation and file changes are handled separately by Rails' cache helper (md5 is added at the end of cache path)

  • one key instead of many, but also unobtrusive (example in haml):

    - cache [cache_key_for(@articles, 'articles'), @some_active_model_object, 'box-type-view'] do
    (...)
  • core concept of Russian Doll Caching is touching: belongs_to :some_model, touch: true, but if you do not use touch, you can still cache safely like this (example in haml):

    - cache [cache_key_for(@articles, 'articles'), @some_model_instance] do
    (...)

Example (haml):

- cache cache_key_for(@service.datacenters, 'datacenters', current_company.cache_key, Time.now.utc.to_date.to_s) do
(...)

Rails will generate cache at:

app_name:views/en/datacenters/5bd92bd352e7726d02175752913014711f5d412e/companies/1-20150619101645935901000/2015-06-26/7a6f89a738006a69c1d1e0214e147bab

#cache_key_for_view

Features:

  • all features of cache_key_for method
  • better debugging (accepts view/partial file path for visible part of cache key)

Example (haml):

- cache cache_key_for_view(__FILE__, @service.datacenters, 'datacenters', current_company.cache_key, Time.now.utc.to_date.to_s) do
(...)

Rails will generate cache at:

app_name:views/en/datacenters/5bd92bd352e7726d02175752913014711f5d412e/companies/1-20150619101645935901000/2015-06-
26/app/views/services/datacenters.html.haml/4324f99340fd2b2bc7e26b8e9b79d8f5

Contributing

./bin/setup
bundle exec rake

Testing new versions

rm gemfiles/*.lock
bundle exec appraisal install

Todo