- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到了一个简单的问题:我的抽佣任务是:
namespace :common do
desc 'check rails cache'
task easy_task: :environment do
l = -> (test_param) do
puts "cache value isn't found, reload..."
'this is result'
end
result = Rails.cache.fetch('test_task', expires_in: 1.hour) do
l.call(1)
end
end
end
我的期望 - 输出短语“找不到缓存值,重新加载...”将仅输出 rake common:easy_task
的第一次运行,但在每次运行时都会输出。
此外,我正在尝试:
rake common:easy_task
rails common:easy_task
bundle exec spring rake common:easy_task
bundle exec spring rails common:easy_task
也没有这些帮助。
Rails 缓存在 rake 任务中不起作用,但在控制台和应用程序中起作用。为什么?
最佳答案
如果事情在控制台中工作但在 rake 任务中不工作,您可能正在使用 ActiveSupport::Cache::MemoryStore (这是 config/environments/development.rb 中的默认设置,如果你有启用缓存的临时文件,否则它使用 ActiveSupport::Cache::NullStore ,这在任何地方都不起作用,原因很明显),它将东西存储在进程内存中:
A cache store implementation which stores everything into memory in the same process.
Rails.configuration.cache_store # => :memory_store
l = -> (test_param) do
puts "cache value isn't found, reload..."
'this is result'
end
Rails.cache.fetch('test_task', expires_in: 1.hour) do
l.call(1)
end
# cache value isn't found, reload...
# => "this is result"
Rails.cache.fetch('test_task', expires_in: 1.hour) do
l.call(1)
end
# => "this is result"
但在每次使用单独进程的 rake 任务中效果不佳
$ rake common:easy_task
# [:memory_store, "pid = 26230"]
# cache value isn't found, reload...
$ rake common:easy_task
# [:memory_store, "pid = 26253"]
# cache value isn't found, reload...
即使使用 spring,每次运行的过程也是不同的
$ bin/rake common:easy_task
# Running via Spring preloader in process 26283
# [:memory_store, "pid = 26283"]
# cache value isn't found, reload...
$ bin/rake common:easy_task
# Running via Spring preloader in process 26297
# [:memory_store, "pid = 26297"]
# cache value isn't found, reload...
为了解决这个问题,我们需要使用不同的内存存储(我将使用 ActiveSupport::Cache::FileStore 用于此示例,但可以使用您喜欢的其他任何一个)
config.cache_store = :file_store, Rails.root.join('tmp/cache_store')
然后 rake 任务按预期工作:
$ bin/rake common:easy_task
# Running via Spring preloader in process 26400
# [:file_store, #<Pathname:.../tmp/cache_store>, "pid = 26400"]
# cache value isn't found, reload...
$ bin/rake common:easy_task
# Running via Spring preloader in process 26414
# [:file_store, #<Pathname:.../tmp/cache_store>, "pid = 26414"]
$ bin/rails c
# Running via Spring preloader in process 26430
result = Rails.cache.fetch('test_task', expires_in: 1.hour) do
l.call(1)
end
# => "this is result"
关于ruby-on-rails - Rails.cache 在每次 rake 任务运行后丢弃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45277992/
我已经为精炼厂安装了一个博客引擎,它运行良好。 现在我生成了一些表字段更改的迁移(当然不是精炼厂或博客表),但我收到一个错误: == CreateBlogStructure: migrating ==
设置如下: 新建 Rails 应用程序,然后将此 test_rake.rake 放入 lib/tasks 中: task :testclass do HelloClass.hello end` 将
我觉得调用 bundle exec rake 或 bundle exec make 很奇怪:为什么不呢 bundle exec bundle exec rake 然后呢? 虽然我应该能够从 Rakef
我试图更好地了解 rake作品。我在 rake 网站上查看过它是如何工作的,但没有明确解释 rake搜索 Rakefiles 以及它在解决依赖项时所经历的步骤。有人能解释一下如何rake作品? 最佳答
我有一个构建系统,它由几个带有项目的子目录组成,其中每个子目录都有一个单独的 rakiefile(或几个 rakefiles)。没有顶级目录有一个 rakefile,它遍历所有子目录并通过以下方式调用
我想运行一个要求用户输入的 Rake 任务。 我知道我可以在命令行上提供输入,但我想询问用户是否确定他们想要继续执行特定操作,以防他们错误输入所提供的值之一到 Rake 任务。 最佳答案 像这样的东西
我在 this question 中遇到了相同的 heroku rake 问题(据我所知已解决) . 当我尝试修复(包括 require 'rake/dsl_definition' 上面的 requi
如何编写将捆绑安装然后 rake db:migrate 然后 rake db:seed 的 rake 任务。 namespace 'install' do 'bundle install' '
这可能是一种“插上电源了吗?”问题,但是在获得 Ubuntu 11.04 的默认 Rackspace 镜像之后。运行命令(以 root 身份) apt-get 安装 rake 因错误而失败 Readi
花了一些时间没有在 Rails 上编程,现在我“回来了”,事情出了问题。 我安装了 ruby 1.9.2-p0 并坚持使用 rails 3.0.1.Updated 所有 gems 和 bundle
我在让 dotCover 在 Albacore 中工作时遇到一些问题 exec使用相对路径的任务。 @xUnitRunnerPath = Pathname.new('../../Tools/xUnit
我正在尝试执行“rake db:migrate”,它给了我这个错误。 Andy:AcademyAir Andy$ rake db:migrate /Users/Andy/.rvm/gems/ruby-
我的 .bashrc 中有以下命令: alias mfigpdf='for FIG in *.fig; do fig2dev -L pdftex "$FIG" "${FIG%.*}.pdftex";
我希望能够发出命令 rake test:qunit并运行我们的 qunit 测试。这可能吗?可以在不打开浏览器窗口的情况下完成吗? 最佳答案 您可以将 PhantomJS 集成到 rake 中。 Ph
我不是一个 ruby 人,我正在使用 Ember.js 开发一个项目,我正在使用 Rake 管道来编译我的脚本。 # AssetFile $: true concat 'templates.
嗨,我正在做一些编码并试图 rake routes 这个错误信息出来了,我不知道该怎么办 rake aborted! Gem::LoadError: You have already activate
你好,我正在开发一个 rails 应用程序,每次我尝试运行任何 rake 命令时,我都会收到此错误 /usr/local/bin/rake:22:in load': cannot load such
我打电话 bundle exec cap staging demo:foo . demo:foo Cap 任务调用 Rake 任务,打印出 Rails.env . 但是...... Rails 任务正
相关的 travis-ci 构建位于: http://travis-ci.org/#!/NZOI/nztrain/builds/2046207 此时 db:migrate 或 db:test:prep
为什么我收到这个 Rake 错误: $ rake --version /Volumes/Data/sampablokuper/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/
我是一名优秀的程序员,十分优秀!