Git Product home page Git Product logo

cached_model's Introduction

= CachedModel

Rubyforge Project:

http://rubyforge.org/projects/rctools/

Documentation:

http://dev.robotcoop.com/Libraries/cached_model/

== About

CachedModel stores Rails ActiveRecord objects in memcache allowing for very
fast retrievals.  CachedModel uses the ActiveRecord::Locking to ensure that
you don't perform multiple updates.

== CachedModel Doesn't...

CachedModel is not magic.

CachedModel only accelerates simple finds for single rows.

CachedModel won't cache every query you run.

CachedModel isn't smart enough to determine the dependencies between your
queries so that it can accelerate more complicated queries.  Without these
smarts you'll only end up with a broken application.

If you want to cache more complicated queries you need do it by hand.

== Using CachedModel

First, install the cached_model gem:

  $ sudo gem install cached_model

Then set up memcache-client for CachedModel by setting CACHE in your
config/environment files:

  CACHE = MemCache.new 'localhost:11211', :namespace => 'my_rails_app'

You will need separate namespaces for production and development, if
you are using memcache on the same machine.

Note that using memcache with tests will cause test failures, so set your
memcache to be readonly for the test environment.

Then make Rails load the gem:

  $ tail -n 4 config/environment.rb 
  # Include your application configuration below
  
  require 'cached_model'

Then edit your ActiveRecord model to inherit from CachedModel instead of
ActiveRecord::Base:

  $ head -n 8 app/models/photo.rb
  ##
  # A Photo from Flickr.
  
  class Photo < CachedModel
  
    belongs_to :point
    belongs_to :route

== Extra Features

=== Local Cache

CachedModel also incorporates an in-process cache, but it is disabled by
default.  In order to enable the in-process cache enable it in your
environment.rb:

  CachedModel.use_local_cache = true

And add a after_filter that flushes the local cache:

  class ApplicationController < ActionController::Base
  
    after_filter { CachedModel.cache_reset }
  
  end

IF YOU DO NOT ADD THE AFTER FILTER YOU WILL EXPERIENCE EXTREME PROCESS GROWTH.

=== Memcache

Memcache may be disabled and the TTL for objects stored in memcache may be
changed:

  CachedModel.use_memcache = false
  CachedModel.ttl = 86400

The default TTL for memcache objects is 900 seconds.

cached_model's People

Contributors

drbrain avatar zenspider avatar

Watchers

 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.