cpuguy83 / pack_rat

Rails cache helper method
MIT License
11 stars 1 forks source link

PackRat

PackRat is a simple helper for Rails.cache
When included in your class it makes caching trivial.

class MyClass
  include PackRat::CacheHelper

  def some_method
    cache do
      #stuff
    end
  end

  def self.some_class_method
    cache do
      # stuff
    end
  end
end

Installation

To install:

gem install pack_rat

or include in your Gemfile

gem 'pack_rat'

PackRat::CacheHelper does a few things:

When included into your gemfile, PackRat is automatically included into ActiveRecord::Base

If you do not want an MD5 digest created, then set self.file_location = nil in your class.

Check it out. Use the debug: true option in your cache call to see what key is actually being generated for debugging

def some_method
  cache '', debug: true do
    # stuff
  end
end

An Example using an ActiveRecord class

class MyClass < ActiveRecord::Base

  def my_long_instance_method
    cache do
      puts 'stuff'
    end
  end

  def self.my_long_class_method
    cache do
      puts 'stuff'
    end
  end

  def method_with_debug
    cache [], debug: true do
      puts 'stuff'
    end
  end

  def self.class_method_with_debug
    cache [], debug: true do
      puts 'stuff'
    end
  end
end

If you are passing in options, you must specify a cache key, this can be an empty string, or an empty array, or even not empty, but it has to be there

Contributing to pack_rat

Copyright

Copyright (c) 2013 Brian Goff. See LICENSE.txt for further details.