如何从Rails Observer/Model调用expire_fragment? [英] How to call expire_fragment from Rails Observer/Model?

查看:68
本文介绍了如何从Rails Observer/Model调用expire_fragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎已经尝试了所有方法,但似乎无法使用 来自模型的expire_fragment?我知道你不应该这样 非MVC,但肯定有很多方法可以做到.

I've pretty much tried everything, but it seems impossible to use expire_fragment from models? I know you're not supposed to and it's non-MVC, but surely there much be some way to do it.

我在lib/cache_helper.rb中创建了一个带有所有过期帮助器的模块, 每个内部只有一堆expire_fragment调用.我有我所有的 /app/sweepers下的缓存清扫程序设置,并有一个"include 我的应用程序控制器中的"CacheHelper" 通过控制器调用时,app运行正常.

I created a module in lib/cache_helper.rb with all my expire helpers, within each are just a bunch of expire_fragment calls. I have all my cache sweepers setup under /app/sweepers and have an "include CacheHelper" in my application controller so expiring cache within the app when called via controllers works fine.

然后是我有一些外部守护程序,尤其是一些 重复的cron任务,该任务调用一个调用特定任务的rake任务 方法.此方法进行一些处理并将条目输入到 模型,之后我需要使缓存过期.

Then things is I have some external daemons and especially some recurring cron tasks which call a rake task that calls a certain method. This method does some processing and inputs entries into the model, after which I need to expire cache.

执行此操作的最佳方法是什么,因为我无法在模型中指定缓存清除程序. 直率的观察者似乎是最好的解决方案,但是 抱怨expire_fragment未定义等,我什至 尝试将ActionController缓存类包含到观察器中 但这没用.我喜欢一些有关如何创建解决方案的想法 为了这.谢谢.

What's the best way to do this as I can't specify cache sweeper within the model. Straight up observers seem to be the best solution but then it complains about expire_fragment being undefined etc etc, I've even tried including the ActionController caching classes into the observer but that didn't work. I'd love some ideas of how to create a solution for this. Thanks.

推荐答案

免责声明:我的导轨有点生锈,但是这种方法或类似的方法应该可以工作

Disclaimer: My rails is a bit rusty, but this or something like it should work

ActionController::Base.new.expire_fragment(key, options = nil) 

这篇关于如何从Rails Observer/Model调用expire_fragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆