Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails缓存正在存储值但不工作?_Ruby On Rails_Caching - Fatal编程技术网

Ruby on rails Rails缓存正在存储值但不工作?

Ruby on rails Rails缓存正在存储值但不工作?,ruby-on-rails,caching,Ruby On Rails,Caching,我正在将外部API响应缓存到Rails中。我从缓存中读取数据,它按预期返回nil。我将其存储到缓存中,它返回true,我再次读取,它返回我的值,但是如果我使用相同的参数从客户机发出另一个请求,它将返回nil,就像没有存储值一样 def创建 cache=ActiveSupport::cache::MemoryStore.new url=”http://api.openweathermap.org/data/2.5/weather?zip=“+params[:zip]+”&units=英制&APP

我正在将外部API响应缓存到Rails中。我从缓存中读取数据,它按预期返回nil。我将其存储到缓存中,它返回true,我再次读取,它返回我的值,但是如果我使用相同的参数从客户机发出另一个请求,它将返回nil,就像没有存储值一样

def创建
cache=ActiveSupport::cache::MemoryStore.new
url=”http://api.openweathermap.org/data/2.5/weather?zip=“+params[:zip]+”&units=英制&APPID=4e66533961b500086bf6bd7c37d4b847”
@天气=获取天气(url)
p cache.read(参数[:zip])
weather=cache.read(参数[:zip])
p天气
p.零?
如果天气是零?
cache.write(参数[:zip],@weather,:expires\u in=>30.min)
结束
p cache.read(参数[:zip])
呈现json:@weather

结束
因为每次请求此操作时都会创建缓存存储:

cache = ActiveSupport::Cache::MemoryStore.new
您应该将此行放入配置文件中:

config.cache_store = :memory_store, { size: 64.megabytes }

然后使用Rails.cache获取或设置缓存。

您到底是如何运行代码的?有一个前端客户端可以触发控制器中的创建操作。因此,前端将zipcode作为参数传递给后端,并在表单提交时触发控制器操作