我有一些API,我想在其中保存一些可以在每种API方法之间访问的值,因此我尝试使用ActiveSupport :: Cache :: MemoryStore。我可能没有正确使用MemoryStore(从未使用过,无法找到特定的教程)
我正确使用MemoryStore吗?似乎应该超级简单,但似乎无法保存任何价值。
class MyController
# 1st API handler /post
def first
@cache = ActiveSupport::Cache::MemoryStore.new() if @cache.nil?
@cache.write('shared_val', params['user_key'])
end
# 2nd API handler /post
def second
@cache = ActiveSupport::Cache::MemoryStore.new() if @cache.nil?
saved_val = @cache.read('shared_val')
puts "#{saved_val}" # nil?????
end
我还尝试了一些我在SO答案中看到的示例,但似乎仍然无法保存该值。
# In config file
config.cache_store = :memory_store
# 1st API handler /post
def first
Rails.cache.write("ABC", "abc")
check_val = Rails.cache.read('ABC')
puts "VALUE: #{check_val}" # shows correct 'abc'
end
# 2nd API handler /post
def second
Rails.cache.fetch("ABC") # gets nil, why???
Rails.cache.read("ABC) # also nil
end
答案 0 :(得分:1)
config.action_controller.perform_caching
默认为假。您需要在其他环境中将其打开。