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'
And then execute:
$ bundle install
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
- Fork it ( https://github.com/zarqman/smart_cache_store/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
License
The gem is available as open source under the terms of the MIT License.