No release in over a year
Production-ready enhancements for mem_cache_store and redis_cache_store. For Rails or ActiveSupport.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

 Project Readme

SmartCacheStore

SmartCacheStore provides variations on ActiveSupport's RedisCacheStore and MemCacheStore to make the default configurations more production ready. It does this by setting better defaults and using a few more ENVs.

Redis/Valkey -- SmartRedisCache

Set by default:

url:                ENV['REDIS_URL']
namespace:          ENV['REDIS_NAMESPACE']
expires_in:         1.day
race_condition_ttl: 5.seconds
pool:               { size: ENV['RAILS_MAX_THREADS'] || 5 }
connect_timeout:    0.5
read_timeout:       1
write_timeout:      1
reconnect_attempts: [0.2]

:url should be a uri, like: redis://localhost:6379/0. For two or more uris, combine with a comma: redis://host-a,redis://host-b.

Memcache -- SmartMemCache

Set by default:

url:                ENV['MEMCACHE_SERVERS']
namespace:          ENV['MEMCACHE_NAMESPACE']
expires_in:         1.day
race_condition_ttl: 5.seconds
failover:           true
pool:               { size: ENV['RAILS_MAX_THREADS'] || 5 }

MemCacheStore's legacy initializer with *addresses as the first parameter, and the RedisCacheStore compatible :url are supported. See examples below.

:url or *addresses should be a uri, like: memcached://localhost:11211. For two or more uris, combine with a comma: memcached://host-a,memcached://host-b.

Usage

In environment/[env].rb:

Redis/Valkey

# basic:
config.cache_store = :smart_redis_cache
# with parameters:
config.cache_store = :smart_redis_cache, {url: 'redis://localhost:1234/9'}
config.cache_store = :smart_redis_cache, {url: 'redis://localhost:1234/9', connect_timeout: 1}

Memcache

# basic:
config.cache_store = :smart_mem_cache
# with parameters:
config.cache_store = :smart_mem_cache, 'memcached://localhost:1234'
config.cache_store = :smart_mem_cache, 'memcached://localhost:1234', {expires_in: 1.hour}
config.cache_store = :smart_mem_cache, {url: 'memcached://localhost:1234', expires_in: 1.hour}

Installation

Add smart_cache_store plus one driver to your application's Gemfile:

gem 'redis-client' # for redis/valkey
# OR
gem 'dalli' # for memcache

gem 'smart_cache_store'

Gem versions

For Rails / ActiveSupport >= 8.2, use smart_cache_store 2.x. For Rails / ActiveSupport 6.1-8.1, use smart_cache_store 1.x.

Contributing

Contributions are welcomed. If unsure about the scope or reach of a potential PR, feel free to open a discussion Issue first (not required though).

Please submit human-written contributions only. No contributions containing output generated by LLMs or other non-deterministic systems are allowed.

  • Code, tests, docs, PRs: must be 100% human written.
  • Issues, security reports: must originally be 100% human written. If not originally in English, an LLM may be used to translate to English provided that this translation is disclosed.

We recognize that opinions about the use of LLMs and non-deterministic systems vary widely. Still, we respectfully ask that you honor this policy when interacting with this project.

Q: May I use LLMs apart from contributions?

All usage, training or otherwise, must respect copyrights. Beyond that, we've found that LLMs are prone to errors and misunderstandings--sometimes obvious, sometimes subtle. Please verify everything before trusting an LLM's output.

Running tests

bundle install
rake [test]

License

MIT