Rails 用 Rails.cache 写入服务器的缓存失效了,请问是什么原因呢

lgor · June 10, 2017 · Last by lgor replied at July 13, 2017 · 1472 hits

第一个红框是生成的 key 写入缓存,第二个是用 key 读取测试,可以成功读取到,第三个是用户再次访问时用 key 就读取不到数据了。 写入缓存的方法如下


def gen(outid, partner)
  code = generate_code
  #code = "123456"
  key  = cache_key(partner.appid, code)

  write_cache(key, outid, partner)
  puts "read key test--------------------------------"
  puts key
  puts cache.read(key)

  puts "read key test--------------------------------"
  code
end

def cache
  Rails.cache
end

def write_cache(key, outid, partner)
  info = {
    outid: outid,
    appid: partner.appid,
    appsecret: partner.appsecret
  }.to_json
  puts "write key-----------------------"

  puts key

  puts "write key-----------------------"

  cache.write(key, info, expires_in: 30.minutes.to_i)
end

读取方法如下

搞了一个多小时没解决,缓存设置的有效时间是半小时,但是这里立马就失效了,求教

看上去没什么问题,redis-cli 进控制台看看 ttl

lgor closed this topic. 13 Jul 15:22
lgor reopened this topic. 13 Jul 15:22
Reply to IChou

后来发现是因为是启动的开发模式,缓存默认是关闭的,后来手动开启了缓存就可以了

lgor closed this topic. 13 Jul 15:23
You need to Sign in before reply, if you don't have an account, please Sign up first.