Skip to content
This repository has been archived by the owner on Jun 25, 2018. It is now read-only.

humanzz/method_cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MethodCache
===========

A Rails (v2.1.0) plugin that tries to solve the problem of having to call a method repetitively. That method is expensive to be recalculated everytime.
The solution is to cache the method results and we've always done that by caching a method's result in instance variables.

method_cache plugin frees you from the trouble of worrying about writing caching code and introduces a couple of utility methods
in ActiveRecord objects that make your life easier.

In its current form, the plugin provides the following extensions to ActiveRecord Objects

caches_method :method_name
caches_class_method :method_name
and for expiring the cache
instance.expire_method :method_name
Class.expire_instance_method :method_name, id
Class.expire_class_method :method_name


Introducing the plugin blog entry
=================================
http://humanzz.spaces.live.com/blog/cns!82322F9506CB0449!713.entry

Setup
=====
	
	ruby script/plugin install git://github.com/humanzz/method_cache.git
	
	
Examples
========
For caching instance methods

class User < ActiveRecord::Base
  def costy_method
    #some real heavy calculation that takes alot of resources
    #to complete
    #this method is called regularly and its results rarely change
  end
  caches_method :costy_method
end

class User < ActiveRecord::Base
  def costy_method
    #some real heavy calculation that takes alot of resources
    #to complete
    #this method is called regularly and its results rarely change
  end
  caches_method :costy_method, :for => 20.minutes
end

class User < ActiveRecord::Base
  def costy_method
    #some real heavy calculation that takes alot of resources
    #to complete
    #this method is called regularly and its results rarely change
  end
  caches_method :costy_method, :until => :midnight
end

:until also accepts Time and date objects but I'm considering removing until or have it 
support things like :midnight because otherwise it would cause errors after the specified
date or time have passed.

To expire cached instance methods
@user.expire_method :costy_method
User.expire_instance_method :costy_method, user_id

For caching class methods

class User < ActiveRecord::Base
  def self.costy_class_method
    #some real heavy calculation that takes alot of resources
    #to complete
    #this method is called regularly and its results rarely change
  end
  caches_class_method :costy_class_method
end

caches_class_method also supports the :for and :until options

To expire class methods
User.expire_class_method :costy_class_method

Test
====
rake test:plugins DB=sqlite3
Copyright (c) 2008 humanzz (Ahmed Sobhi), released under the MIT license

About

A Ruby on Rails plugin for caching the output of parameterless methods

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages