- 浏览: 154297 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
mysky1984:
:cry::arrow::idea:
Filter chain skipping --- 跳过过滤器链 -
shaka:
受用,考虑过这个问题没有?如何给过滤器传参数,或者在子类过滤器 ...
Filter chain skipping --- 跳过过滤器链 -
xlth2006@sina.com:
[img][/img][flash=200,200][/fla ...
rails2.0被废弃的用法
expir_xxx 方法会工作的很好,但是在你的“控制器”中它们也要与缓存功能的代码合
作。每次你修改数据库内的一些东西时,你也必须对它可能影响到的被缓存页做些工作。在
应用程序较小时这很容易,但应用程序快速增长时这就会变得很困难。在一个“控制器”内
的修改可能会影响到另一个“控制器”中被缓存的页。“帮助方法”内的商业逻辑,其实不
应该知道有关HTML 页的信息,现在需要担心的是失效被缓存的页。
幸运地,Rails 可以使用sweeper 来简化一些这样的耦合。Sweeper 是你的“模型”对象
上的一个指定“观察者”。当在“模型”内有重要事情发生时,sweeper 会失效依赖于此“模型”数据的被缓存的页。你的应用程序可以根据需要许多sweeper。典型地你会创建一个单独的sweeper 来管理每个“控制器”的缓存。在app/models 目录内放置你的sweeper 代码。
class ArticleSweeper < ActionController::Caching::Sweeper
observe Article
# If we create a new article, the public list
# of articles must be regenerated
def after_create(article)
expire_public_page
end
# If we update an existing article, the cached version
# of that particular article becomes stale
def after_update(article)
expire_article_page(article.id)
end
# Deleting a page means we update the public list
# and blow away the cached article
def after_destroy(article)
expire_public_page
expire_article_page(article.id)
end
private
def expire_public_page
expire_page(:controller => "content", :action => 'public_content')
end
def expire_article_page(article_id)
expire_action(:controller => "content",
:action => "premium_content",:id => article_id)
end
end
sweeper 的工作流程有点费解。
1、sweeper 在一个或多个“活动记录”类上被定义为一个“观察者”。在这个例子中,
它观察Article“模型”(我们在270 页谈论它)。sweeper 使用“钩子”方法来适当地失效被缓存的页。
2、sweeper 也被在一个“控制器”中像下面一样被声明为活跃的。
cache_sweeper directive.
class ContentController < ApplicationController
before_filter :verify_premium_user, :except => :public_content
caches_page :public_content
caches_action :premium_content
cache_sweeper :article_sweeper,
:only => [ :create_article,
:update_article,
:delete_article ]
# ...
3、如果请求来自sweeper 过滤的“动作”中的一个的调用,则sweeper 被激活。如果任
何一个“活动记录”观察者方法被激活,则页和“动作”失效方法将被调用。如果“活动记
录”观察者得到调用,但是当前“动作”没有被选择为一个缓存的sweeper,则sweeper 内
的失效调用被忽略。否则失效有效。
实例
sweeper插件用于自动生成清扫缓存的sweeper类,监视特定的model,根据相应的事件触发其行动,下面是一个example:
1. 首先生成项目和初始数据:
$ rails test_sweeper
$ cd test_sweeper
$ script/generate scaffold post title:string body:text
[/color]
#db/migrate/001_create_posts.rb
(1..50).each do |num|
Post.create(:title => "test#{num}", :body => "Test#{num}")
end
$ rake db:migrate
2. 安装插件:
$ script/plugin install http://topfunky.net/svn/plugins/sweeper
3. 生成Sweeper类,该类在model增删改之后将缓存页面清除:
$ script/generate sweeper post after_save before_destroy
[color=red]
#app/models/post_sweeper.rb
class PostSweeper < ActionController::Caching::Sweeper
observe Post
def after_save(record)
expire_page :controller => 'posts', :action => 'index'
end
def before_destroy(record)
expire_page :controller => 'posts', :action => 'index'
end
end
4. 在controller中指定缓存页面和sweeper类:
[color=red][/color]
#app/controller/posts_controller.rb
caches_page :index
cache_sweeper :post_sweeper
5. 察看效果:
当第一次访问/posts时,server会查询数据库,render页面,并生成cache page:
Cached page: /posts.html (0.00737)
之后再访问/posts时,server就不会再去查询数据库,render页面,而直接去读cache page
直到我们对post做了增删改中任意一个操作,server会删除被cache的page:
Expired page: /posts.html (0.00014)
然后再访问/posts时,server又会去查询数据库,render页面,并生成cache page
作。每次你修改数据库内的一些东西时,你也必须对它可能影响到的被缓存页做些工作。在
应用程序较小时这很容易,但应用程序快速增长时这就会变得很困难。在一个“控制器”内
的修改可能会影响到另一个“控制器”中被缓存的页。“帮助方法”内的商业逻辑,其实不
应该知道有关HTML 页的信息,现在需要担心的是失效被缓存的页。
幸运地,Rails 可以使用sweeper 来简化一些这样的耦合。Sweeper 是你的“模型”对象
上的一个指定“观察者”。当在“模型”内有重要事情发生时,sweeper 会失效依赖于此“模型”数据的被缓存的页。你的应用程序可以根据需要许多sweeper。典型地你会创建一个单独的sweeper 来管理每个“控制器”的缓存。在app/models 目录内放置你的sweeper 代码。
class ArticleSweeper < ActionController::Caching::Sweeper
observe Article
# If we create a new article, the public list
# of articles must be regenerated
def after_create(article)
expire_public_page
end
# If we update an existing article, the cached version
# of that particular article becomes stale
def after_update(article)
expire_article_page(article.id)
end
# Deleting a page means we update the public list
# and blow away the cached article
def after_destroy(article)
expire_public_page
expire_article_page(article.id)
end
private
def expire_public_page
expire_page(:controller => "content", :action => 'public_content')
end
def expire_article_page(article_id)
expire_action(:controller => "content",
:action => "premium_content",:id => article_id)
end
end
sweeper 的工作流程有点费解。
1、sweeper 在一个或多个“活动记录”类上被定义为一个“观察者”。在这个例子中,
它观察Article“模型”(我们在270 页谈论它)。sweeper 使用“钩子”方法来适当地失效被缓存的页。
2、sweeper 也被在一个“控制器”中像下面一样被声明为活跃的。
cache_sweeper directive.
class ContentController < ApplicationController
before_filter :verify_premium_user, :except => :public_content
caches_page :public_content
caches_action :premium_content
cache_sweeper :article_sweeper,
:only => [ :create_article,
:update_article,
:delete_article ]
# ...
3、如果请求来自sweeper 过滤的“动作”中的一个的调用,则sweeper 被激活。如果任
何一个“活动记录”观察者方法被激活,则页和“动作”失效方法将被调用。如果“活动记
录”观察者得到调用,但是当前“动作”没有被选择为一个缓存的sweeper,则sweeper 内
的失效调用被忽略。否则失效有效。
实例
sweeper插件用于自动生成清扫缓存的sweeper类,监视特定的model,根据相应的事件触发其行动,下面是一个example:
1. 首先生成项目和初始数据:
$ rails test_sweeper
$ cd test_sweeper
$ script/generate scaffold post title:string body:text
[/color]
#db/migrate/001_create_posts.rb
(1..50).each do |num|
Post.create(:title => "test#{num}", :body => "Test#{num}")
end
$ rake db:migrate
2. 安装插件:
$ script/plugin install http://topfunky.net/svn/plugins/sweeper
3. 生成Sweeper类,该类在model增删改之后将缓存页面清除:
$ script/generate sweeper post after_save before_destroy
[color=red]
#app/models/post_sweeper.rb
class PostSweeper < ActionController::Caching::Sweeper
observe Post
def after_save(record)
expire_page :controller => 'posts', :action => 'index'
end
def before_destroy(record)
expire_page :controller => 'posts', :action => 'index'
end
end
4. 在controller中指定缓存页面和sweeper类:
[color=red][/color]
#app/controller/posts_controller.rb
caches_page :index
cache_sweeper :post_sweeper
5. 察看效果:
当第一次访问/posts时,server会查询数据库,render页面,并生成cache page:
Cached page: /posts.html (0.00737)
之后再访问/posts时,server就不会再去查询数据库,render页面,而直接去读cache page
直到我们对post做了增删改中任意一个操作,server会删除被cache的page:
Expired page: /posts.html (0.00014)
然后再访问/posts时,server又会去查询数据库,render页面,并生成cache page
发表评论
-
Magic Column Names(自动的列名)
2008-08-10 16:09 2857在课程的最后两章我们会提到很多列名字,它们对“活动记录”有重大 ... -
Callback Objects
2008-08-10 16:04 2923可在“模型”类内直接 ... -
Timestamping Records
2008-08-10 15:57 2788before_create 和before_update 回调 ... -
Callbacks
2008-08-10 15:46 2845“活动记录”控制“模 ... -
“聚合”(Aggregation)
2008-08-07 00:20 4282数据库列有个带限制的类型集: integers, string ... -
acts as list/acts as tree
2008-08-07 00:03 3974Acts As List 在子表中使 ... -
基于时间缓存
2008-08-06 00:18 2788def index when_fragment_expir ... -
Caching(缓存)“缓存”什么
2008-08-02 17:24 3326相关文献(http://www.ibm.com/develop ... -
verify(验证)
2008-08-01 14:18 3006before“过滤器”的通常 ... -
“过滤器”(before,after,around)
2008-08-01 14:18 4039“过滤器”可以让你在你的“控制器”内写代码来包装由“动作”完成 ... -
Flash—“动作”间的通信
2008-08-01 13:37 2971Flash—“动作”间的通信 当我们使用redirect_to ... -
“会话”存储比较和失效与清理
2008-07-31 22:58 3179比较“会话”存储选项 ... -
“会话”存储
2008-07-31 22:56 2971在存储你的“会话”数据时,Rails 有很多选项。每个选项即好 ... -
“会话”session
2008-07-31 22:54 3599Rails 的“会话”是个类 ... -
cookies
2008-07-31 22:40 3277Cookie 允许web 应用程序 ... -
对用户应答(1,2,3)
2008-07-30 00:36 1093“控制器”的一部分工作是响应用户。有三个基本方式来完成个工作: ... -
应答3-重定向(redirect_to)
2008-07-30 00:34 2778一个HTTP redirect 被从服 ... -
应答2-发送文件和其它数据(send_data)
2008-07-30 00:25 1940我们已经看了在“控制器”内提交模板(render)和发送字符串 ... -
应答1-提交模板(render)方法
2008-07-29 23:52 2166模板是个文件,它定义 ... -
开发环境中的预设对象(request、params)
2008-07-29 23:33 2230“控制器”为“动作”设置环境。环境被建立在实例变量内,但你应该 ...
相关推荐
这个“mine_sweeper.zip”压缩包包含了构建Android版扫雷游戏的所有源代码,为开发者提供了一个深入理解Android应用开发和游戏逻辑实现的实例。 首先,让我们探讨一下Android开发的基础。Android是一个基于Linux的...
2. **单例模式**:用于确保游戏中只有一个实例,如游戏板对象,避免多实例冲突。 六、调试与测试 1. **单元测试**:编写测试用例验证各个功能模块的正确性,如雷的生成、单元格状态的转换等。 2. **集成测试**:...
有两个版本,第一个版本( Mine_sweeper )只完成了基本的 布局, 不能自动打开空白区, 不过 布局的算法 是我想出来的, 有兴趣的朋友可以看看. 第二个版本 , (Mine_mine)参考了 书上的布局方式, 递归打开空白区. 可以...
【标题】"WPF漂亮扫雷游戏源码.zip" 是一个包含使用Windows Presentation Foundation(WPF)技术开发的扫雷游戏源代码的...同时,游戏的逻辑设计,如生成雷区、检查点击、标记雷等,也是编程思维和算法运用的良好实例。
2. "jQuery-Powered-Mine-Sweeper-Game-1737.zip" - 这看起来是一个基于jQuery的扫雷游戏,可能是一个独立的项目或者作为示例中的一个互动元素,用于演示如何在网页中集成广告。jQuery是一种广泛使用的JavaScript库...
Sweeper商店为例,该商店通过实施MIS,实现了库存控制、销售管理、订单处理等多方面的自动化,显著减少了库存成本,提高了工作效率,并提供了更好的客户服务。 **MIS的组成元素** MIS的三大支柱是: 1. **计算机...
以**Mine Sweeper**为例,设计一个逻辑智能体来玩扫雷游戏,需要考虑**PEAS分析(Performance measure, Environment, Actuators, Sensors)**: - **性能度量(Performance measure)**:正确标记雷得1分,错误标记扣1分...