Rails sidekiq 和 controller 里面,cache 好像写的是不同的地方

lukefan · August 31, 2015 · Last by lukefan replied at September 03, 2015 · 2177 hits

我想通过 Rails.cache 来存储一个变量,在 sidekiq 的后台任务上进行修改,在 controller 里面确认状态。 但是好像两边的 cache 读写的并不是统一个位置。

请问有什么办法处理这种问题?不想写数据库或 redis。 另外,如何判断 sidekiq 中的进程都跑完了?

Rails.cache 的 默认 CacheStore 是 MemoryStore,可以配置为 FileStore 或者 MemCacheStore (需要安装 memcached),默认的 MemoryStore 只存在于当前的 Ruby VM 进程中,因为 sidekiq 跟 Rails 运行在不同的 Ruby VM 上,所以不共享。

用 Rails.cache 去存储变量,然后通过 sidekiq 修改,这个做法明显不合理,cache 是不能确保数据持久化的。你的需求我建议最好还是写数据库。

关于查询运行状态,Sidekiq 提供了完整的 API,详细信息见文档 https://github.com/mperham/sidekiq/wiki/API

#1 楼 @lgn21st 感谢, Sidekiq 的 api,看了一下,Sidekiq::Stats::History.new(2) 应该是我需要的。 但是跑步起来,说是 sidekiq::stats 没有初始化过。

#2 楼 @lukefan 你是怎么运行的?应该在当前 Rails 的 console 环境下运行。

#3 楼 @lgn21st 非常感谢,不知道为什么那天就是不行。今天一切都 ok 了。灵异事件。

You need to Sign in before reply, if you don't have an account, please Sign up first.