Coder Social home page Coder Social logo

redrecord's Introduction

redrecord
---------

This gem pre-caches your ActiveRecord model's calculated attributes (computed
fields) in Redis.


Example
-------

 class User < ActiveRecord::Base

   redis_cache do
     def fullname
       firstname + ' ' + lastname
     end
   end

 end


Methods defined inside the "cache" block are redefined to get the answer
from redis first.  The cached attributes are saved whenever the record is
saved, in an after_commit callback.


Cache invalidation for associations
-----------------------------------

Redrecord can be used to cache attributes that use assocations:

 class User < ActiveRecord::Base
   has_many :preferences

   redis_cache do
     def preferences_list
       preferences.map(&:name).join(', ')
     end
   end

 end

 class Preference < ActiveRecord::Base
   belongs_to :user

   invalidate_cache_on :user

 end


In this example, whenever a preference is saved, the associated user record
will be recalculated and saved in redis.  If it is an array (eg. has_many)
then all of the associated records will be re-cached.

Other instance methods of interest:

 obj.remove_from_cache! # Remove redis cache for an object.
 obj.add_to_cache!      # Recalculate fields and store in redis.
 obj.cached_fields      # hash of the cached fields and their values
 obj.attribs_with_cached_fields # cached_fields merged with AR attributes
 obj.verify_cache!      # raise an error if cache doesn't match calculated


If redis is down?
-----------------

To enable talking to a redis server, set Redrecord.enabled=true

If Redrecord can't access redis within a timeout (Redrecord.timeout=15 by
default) then it sets enabled=false, disabling all redis access so your
application can keep working without the cache.

For testing purposes, you can set Redrecord.write_only = true
This keeps the cache contents up to date but does not read back any cached
content.


Setup
-----

in your Gemfile:

   gem 'redrecord'

in your rails app config/initializers/redrecord.rb:

   $redis = Redis.new(:host => 'localhost', :port => 6379, :db => 1)
   Redrecord.redis = $redis
   Redrecord.enabled = true



Contact the author
------------------

Andrew Snow <[email protected]>
Andys^ on irc.freenode.net

redrecord's People

Contributors

andys avatar

Stargazers

Lennard Timm avatar  avatar Dmitry Vasilets avatar Maksim Pechnikov avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.