rails cache sweeper妙用

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正是把两者接合起来的一个东西:

[quote]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.[/quote]

利用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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值