/redcache

A gem for doing smart caching with Redis

Primary LanguageRubyMIT LicenseMIT

Redcache

A gem for caching data in Redis.

This gem caches data from slow services in Redis, but ensures that the cache is up to date.

Should the cache be cold, the result is cached. Should the cache be warm but considered stale, the cache is still used, but updated subsequently via a threaded request, thus not blocking the original request.

This gem is also able to encrypt the cached results with Fernet if required.

Installation

Add this line to your application's Gemfile:

gem 'redcache'

And then execute:

$ bundle

Or install it yourself as:

$ gem install redcache

Usage

Redcache.configure do |c|
  c.redis = $redis
  c.secret = <some_long_hash>
  c.encrypt = true
end

value = Redcache.cache "unique_cache_key" do
  call_to_slow_service
end

arguments can also be passed into Redcache is needed for when your stale refreshes are run:

value = Redcache.cache "unique_cache_key", api, id do |api, id|
  api.call_to_slow_service id
end

Configuration

Several configuration options are available to use that defined the behaviour of redcache.

:redis Connection to redis
:secret If encrypting, what secret should be used. See [Fernet](https://github.com/fernet/fernet-rb) README for more information
:encrypt Should cached data be encrypted (boolean)
:skip_cache Should the cache be skipped. Useful in test environments (boolean)
:logged Standard logger object to use for logging
:log_prefix String to prefix to l2met compatible log lines
:cache_time Time (in seconds) to cache data for before expiring
:stale_time Time (in seconds) before cached data should be considered stale

Testing

Specs to come ;)

Contributing

  1. Fork it ( https://github.com/[my-github-username]/redcache/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request