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
end

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 }
end

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).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s