- 首先写了个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
本文介绍了一种在Rails应用中实现缓存的方法,通过定义一个名为ModuleCache的模块来简化对象的存储、检索和清除过程,并展示了如何在 ActiveRecord 的 Product 模型中引入此缓存模块,以及如何在控制器中调用缓存加载方法。
780

被折叠的 条评论
为什么被折叠?



