webgame中的任务系统简单的描述就是这样的需求:
1.一个角色身上携带各种任务(任务获得渠道很多,系统分配获得,主动接取获得等)
2.角色的任何操作都有可能完成身上携带的某个任务
一个简单的种植小麦任务:
这个任务过程是这样的:如果一个角色身上携带一个种植小麦的任务,当他在执行种植小麦的操作时,把种植小麦这个任务标记成完成状态。
常规方式实现:
class PlantController < ApplicationController
def wheat
#此处省略种植操作....
if 种植小麦成功
task = current_role.tasks.find_by_tag("plant_wheat")
task.update_attributes :status => "completed" if task
render "plant ok"
else
render "plant fail"
end
end
end
这种方式简单,直观。
但是随着任务的增多,操作的增加,本来就已经很复杂的业务逻辑里面再掺杂着这样的任务判定与更新逻辑在里面就很难维护了。
幸好,rails有cache sweeper这东东。。!
cache sweeper本来的用途是集中处理缓存失效逻辑的,就像上面说的,缓存失效逻辑如果分散在各个action不便于管理和维护。。
任务与缓存失效都有着这样一个共通点:缓存失效与任务触发都是由action的执行与model的更改造成的。
监视action的执行rails里有各种filter
监视model的改变rails里有observer
但是这两个任意一个也解决不了上面的问题~
而cache sweeper正是把两者接合起来的一个东西:
引用
Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change.
They do this by being half-observers, half-filters and implementing callbacks for both roles.
利用cache sweeper的falf-ovserver half-filter的特性,完成上面需求:
#app/task_monitors/plant_contract_monitor.rb
class PlantContractMonitor < ActionController::Caching::Sweeper
observe PlantContract
def after_save(record)
if current_role
if record.name == "小麦"
task = current_role.tasks.find_by_tag("plant_wheat")
task.update_attributes :status => "completed" if task
end
end
end
end
种植小麦action只需开头加一句:
cache_sweeper :plant_contract_monitor, :only => [:wheat]
分享到:
相关推荐
Rails Cache 博文链接:https://mmm.iteye.com/blog/134696
rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...
安装将此行添加到应用程序的Gemfile中: gem 'rails-cache-inspector' , group : :development用法配置突出显示 # config/initializers/rails_cache_inspector.rbRailsCacheInspector . configuration . highlight_...
一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码
Bootstrap 3 和 Rails 4(样例用的是Ruby 2.1.1,Rails 4.1.4) Table of Contents Preface 1 Chapter 1: Introducing Web Application Development in Rails 7 Why Bootstrap with Rails? 8 Setting up a Todo ...
[Pragmatic Bookshelf] Crafting Rails Applications Expert Practices for Everyday Rails Development (E-Book) ☆ 图书概要:☆ Rails 3 is a huge step forward. You can now easily extend the framework, ...
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
《Ruby on Rails Tutorial》中文版(原书第2版,涵盖 Rails 4) Ruby 是一门很美的计算机语言,其设计原则就是“让编程人员快乐”。David Heinemeier Hansson 就是看重了这一点,才在开发 Rails 框架时选择了 Ruby...
《Rails之道》按照Rails的各个子系统进行组织编排,分别介绍了Rails的环境、初始过程、配置和日志记录,Rails的分配器、控制器、页面生成和路由,REST、资源和Rails,ActiveRecord的基础、关联、验证和高级技巧,...
本资源是参照rails敏捷开发第四版书中的例子,rails的版本是rails3.2.6
adminlte-rails, AdminLTE Rails gem 将AdminLTE主题与 Rails 资产管道集成 AdminLTE Rails gem AdminLTE 是后端的高级 Bootstrap 主题。英镑 AdminLTE Rails gem 与 Rails 资产管道集成了英镑AdminLTE主题。安装将...
中文世界唯一一本Rails 4.0.0 + Ruby 2.0.0 的自學書籍
Rails::Cache::Extended 这允许为记录集合生成自动过期的缓存键 安装 将此行添加到应用程序的 Gemfile 中: gem 'rails-cache-extended' 然后执行: $ bundle 或者自己安装: $ gem install rails-cache-...
Ruby on Rails helps you produce high-quality, beautiful-looking web applications quickly. You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of ...
使用Aptana+Rails开发Rails Web应用 有Aptana的安装配置等等,中文
rails文档 rails api 英文