- 首先写了个cache的module Cache
def put(object) pre @@cache[object.id] = object end def delete(id) pre @@cache.delete(id) end def get(id) pre @@cache[id] end def load pre @@cache end def destroy @@cache = [] end def pre @@cache ||=find(:all) end def refresh @@cache = find(:all) end end
- ActiveRecord引用之require 'cache'
class Product < ActiveRecord::Base extend Cache belongs_to :company end
- 使用范例class IndexController < ApplicationController
def index @products = Product.load @products end end