RailsSimpleCaching

A simple wrapper around Rails built-in caching. Provides Rails-like macros to generate cached_ versions of your methods. The original goal was to reduce DB queries, but it works with any methods you define (as long as they don't take any arguments, though it's doable to make it work for methods with arguments, too). How Rails caching works: https://guides.rubyonrails.org/caching_with_rails.html#low-level-caching https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html

https://github.com/igorkasyanchuk/rails_cached_method is the only gem of a similar caliber that does kind of the same thing. That one's functionality is wider, but it does a little bit too much for my taste.

Example of usage:

class Author < ApplicationRecord
  has_many :books

  cache_expires_in 96.hours
  caches :books
  caches :expensive_method, expires_in: 2.hours

  def expensive_method
    SomeExternalAPIService.call(self)
    do_something_else
  end
end

The above is equivalent to:

class Author < ApplicationRecord
  has_many :books

  def expensive_method
    SomeExternalAPIService.call(self)
    do_something_else
  end

  def cached_books
    Rails.cache.fetch("#{cache_key_with_version}/books", expires_in: 96.hours) do
      books
    end
  end

  def cached_expensive_method
    Rails.cache.fetch("#{cache_key_with_version}/expensive_method", expires_in: 2.hours) do
      expensive_method
    end
  end
end

Rails caching won't work in development by default; run rails dev:cache in terminal.

Cache expiration time

Use these methods for setting up expiration time (how long until the data is deleted from the cache store). All of them expect a ActiveSupport::Duration argument (24.hours, 10.minutes etc.) The precedence goes:

  1. :expires_in keyword argument.
  2. cache_expires_in class method. Sets default expiration time for all caches macros in the model class. Will only work on macros that are declared below it, for example:
caches :something # Cache will still expire after default time
cache_expires_in 2.minutes
caches :something_else # Will expire in 2 minutes
cache_expires_in 3.hours
caches :another_something # Will expire in 3 hours
  1. Rails.configuration.default_expire_time: global default for all models. Set to 24.hours by default. Set it to your liking in config/application.rb or in config/environments/

Cache invalidation

The cache is invalidated whenever an object with a cached_ method has its updated_at value changed (because this method is used to form the key by which the data is fetched). The data is not deleted from the store, it just won't be fetched the next time you call the method.

# TODO: write some more about cache invalidation

Installation

Add this line to your application's Gemfile:

gem 'rails_simple_caching'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rails_simple_caching

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_simple_caching.

License

The gem is available as open source under the terms of the MIT License.