Looksy – Updates On My ActiveRecord Caching Gem

I just updated Looksy, https://github.com/aboghosian/looksy, a gem I developed to add a caching layer between your ActiveRecord models that represents look up tables in your database. Originally the gem added the following methods:

  1. #fetch_all – retrieves all records from cache
  2. #fetch_by_id(id) – retrieves record by id
  3. #fetch_by_attribute(‘value’) – retrieves first record matching attribute (can supply multiple attributes by separating with _and_; #fetch_by_this_and_that(‘this’, ‘that’))
  4. #fetch_all_by_attribute(‘value’) – retrieves all matching records (can supply multiple attributes as above

A few new methods have been added and here they are:

  1. #fetch_first – retrieves the first record
  2. #fetch_last – retrieves the last record
  3. #fetch_last_by_attributes(‘value’) – retrieves the last matching record (the opposite of using #fetch_by_attribute and supports multiple attributes separated by _and_

To use the gem simple include the Looksy::Cacheable module in your ActiveRecord class (or any class that responds to .all).

class JobType < ActiveRecord::Base
  include Looksy::Cacheable

And just like that these methods are added to your class. By default the gem will attempt to use the caching store of Rails.cache if you are using Rails and if not it will default to Looksy::NullCache which essentially does not caching (lol). You can substitute your own cache and set other options the following way:

class JobType < ActiveRecord::Base
  include Looksy::Cacheable

  self.cache_store = MyCacheStore.new
  self.cache_key = 'MyKey'
  self.cache_options = { expires_in: 30.minutes }

The gem was a by product of some coding I did at work to cache our look up tables so we didn’t have to hit the database each time to get the data (which changed infrequently). Hope you enjoy and I appreciate feedback (this is my very first gem I’ve published).