is Rails.cache purged between tests?

前端 未结 2 1092
半阙折子戏
半阙折子戏 2020-12-24 00:57

We cache id/path mapping using Rails.cache in a Rails 3.2 app. On some machines it works OK, but on the others values are wrong. The cause is hard to track so I

2条回答
  •  温柔的废话
    2020-12-24 01:36

    A more efficient (and easier) method is to set the test environment's cache to use NullStore:

    # config/environments/test.rb:
    config.cache_store = :null_store
    

    The NullStore ensures that nothing will ever be cached.

    For instance in the code below, it will always fall through to the block and return the current time:

    Rails.cache.fetch('time') { Time.now }
    

    Also see the Rails Caching guide: http://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-nullstore

提交回复
热议问题