`
Hooopo
  • 浏览: 335275 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Caching With Instance Variables

阅读更多
class ApplicationController < ActionController::Base  
    def current_user  
        User.find(session[:user_id])  
    end  
end  

上面代码每次请求都会查询一次数据库。下面用实例变量解决这个问题。
@current_user ||= User.find(session[:user_id])  


很简单,不多说了...

class ApplicationController < ActionController::Base  
    def current_user  
        @current_user ||= User.find(session[:user_id])  
    end  
end  


0
0
分享到:
评论

相关推荐

    Python Cookbook, 2nd Edition

    • Table of Contents • Index ... Automatically Initializing Instance Variables from _ _init_ _ Arguments Recipe 6.19. Calling a Superclass _ _init_ _ Method If It Exists Recipe 6.20. ...

    11g_plsql_user_guide_and_reference.pdf

    In previous versions of Oracle, result caches were private to each instance in a Real Application Clusters (RAC) environment. However, in 11g Release 2, result caches are now shared across all ...

    Bloodshed Dev-C++

    * When running a source file in explorer, don't spawn new instance. Instead open the file in an already launched Dev-C++. * Class-parser speed-up (50% to 85% improvement timed!!!) * Many code-...

    微软内部资料-SQL性能优化2

    The user address space is where application code, global variables, per-thread stacks, and DLL code would reside. The system address space is where the kernel, executive, HAL, boot drivers, page ...

    ZendFramework中文文档

    11.1. 输出变量的值 (Dumping Variables) 12. Zend_Exception 12.1. 使用“异常” 13. Zend_Feed 13.1. 介绍 13.2. 导入Feeds 13.2.1. 定制 feeds 13.2.1.1. 导入定制的数组 13.2.1.2. 导入定制的数据源 ...

    php.ini-development

    compatibility with older or less security conscience applications. We ; recommending using the production ini in production and testing environments. ; ...

    spring-framework-reference4.1.4

    Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................

    spring-framework-reference-4.1.2

    Instantiation using an instance factory method ........................................... 30 4.4. Dependencies ...........................................................................................

    MySQL 5.6 Reference Manual

    Table of Contents Preface, Notes, Licenses . . . . . . . . ....1. Licenses for Third-Party Components ....1.1. FindGTest.cmake License ....1.2. LPeg Library License ....1.3. LuaFileSystem Library License ....

Global site tag (gtag.js) - Google Analytics