Use Rails.cache.fetch directly on objects
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
Gemfile
LICENSE
README.md
Rakefile
ocache.gemspec

README.md

ocache

Build Status Coverage Status Gem Version Gem Downloads

Use Rails.cache.fetch directly on objects:

 # Pull from database and populate cache
 > Vote.cache_fetch.count
   (1.3s)  SELECT COUNT(*) FROM `votes`
=> 2_000_000

 # Pull from cache
 > Vote.cache_fetch.count
=> 2_000_000

Install

Add to Gemfile

gem 'ocache'

No further configuration necessary if your app already has a caching method configured.

Options

ocache supports all of the same options that Rails.cache.fetch does:

force, expires_in, race_condition_ttl, raw, compress, compress_threshold, namespace

You can pass them like:

Vote.cache_fetch(force: true, expires_in: 2.days).count

Automatic cache key generation

ocache follows Rails' cache key naming and generation conventions:

# Generates and uses the key: 'votes/count'
Vote.cache_fetch.count

# Generates and uses the key: 'votes/1-20141229214304289304000/expensive_instance_method'
Vote.find(1).cache_fetch.expensive_instance_method

Custom cache keys

Defining one:

Vote.cache_fetch('custom_key').count

With options:

Vote.cache_fetch('custom_key', force: true, expires_in: 2.days).count

Invalidation

It facilitates the Rails preferred key-based cache expiration method:

# Pull from database and populate cache
> Vote.find(1).cache_fetch.expensive_instance_method
=> 2_000_000

# Pull from cache
> Vote.find(1).cache_fetch.expensive_instance_method
=> 2_000_000

# Trigger key-based cache expiration
Vote.find(1).touch

# Pull from database and populate cache
> Vote.find(1).cache_fetch.expensive_instance_method
=> 2_000_000

Raw cache keys

Since ocache complies with Rails' cache key naming conventions, you can find raw key names using Rails' built in cache_key method. These keys can then be used on any of the Rails.cache methods to retrieve the values stored by ocache:

# Pull from database and populate cache
> Vote.find(1).cache_fetch.expensive_instance_method
=> 2_000_000

# Get the cache key
Vote.find(1).cache_key
=> 'votes/1-20141228091556086877000'

# Include method name in key
Rails.cache.read('votes/1-20141228091556086877000/expensive_instance_method')
=> 2_000_000

# Invalidate via key-based cache expiration
Vote.find(1).touch

# Pull from database and populate cache
Rails.cache.read('votes/1-20141228091556086877000/expensive_instance_method')
=> nil

Give it a test run

Start by running some of these examples on a new ocache console:

git clone https://github.com/lukeheuer/ocache.git
cd ocache
bundle
cd test/dummy
rake db:create db:migrate
rails c
> Vote.create # Generate an object for the instance method examples

Note: this starts you off with a blank database. Don't expect the same results as above (2_000_000).

Support

  • Ruby on Rails 4.2 and above
  • Ruby 2.2.2 and above