0

我正在使用活动资源将查询发送到外部 API。

为避免滥用,结果将缓存一小时。这是我的代码,允许我缓存我的查询:

    def find_with_read_through_cache(*arguments)
      logger.debug "----------------"
      logger.debug "arguments : #{arguments.join(' ')}"
      key = cache_key(arguments)
      logger.debug "key : #{key}"
      result = Rails.cache.read(key).try(:dup)
      logger.debug "result #{result}"
      logger.debug "#{key} #{result} #{self.cache_expires_in}"
      unless result
        result = find_without_read_through_cache(*arguments)
        logger.debug "write : #{Rails.cache.write(key, result, :expires_in => self.cache_expires_in)}"
      end
      result
    end

在第一次查询时,一切都很好,我对每个查询都有 200 个请求,并且 Rails.cache 返回 true :

----------------
arguments : players/ersykzer/
key : player/players/ersykzer/
result 
player/players/ersykzer/  3600
GET http://ws.maniaplanet.com:80/players/ersykzer/
--> 200 OK 109 (5376.3ms)
write : true
----------------
arguments : player/ersykzer/
key : canyonsolo/player/ersykzer/
result 
canyonsolo/player/ersykzer/  3600
GET http://ws.maniaplanet.com:80/canyon/rankings/solo/player/ersykzer/
--> 200 OK 115 (404.8ms)
write : true
----------------
arguments : player/ersykzer/
key : canyonmulti/player/ersykzer/
result 
canyonmulti/player/ersykzer/  3600
GET http://ws.maniaplanet.com:80/canyon/rankings/multiplayer/player/ersykzer/
--> 200 OK 169 (389.7ms)
write : true
----------------
arguments : zone/ {:params=>{:offset=>113, :length=>10}}
key : canyonsolo/zone//{:params=>{:offset=>113, :length=>10}}
result 
canyonsolo/zone//{:params=>{:offset=>113, :length=>10}}  3600
GET http://ws.maniaplanet.com:80/canyon/rankings/solo/zone/?length=10&offset=113
--> 200 OK 2012 (157.2ms)
write : true
----------------
arguments : zone/ {:params=>{:offset=>"2", :length=>10}}
key : canyonmulti/zone//{:params=>{:offset=>"2", :length=>10}}
result 
canyonmulti/zone//{:params=>{:offset=>"2", :length=>10}}  3600
GET http://ws.maniaplanet.com:80/canyon/rankings/multiplayer/zone/?length=10&offset=2
--> 200 OK 1849 (172.1ms)
write : true

但是,当页面重新加载时,缓存中似乎有两个元素不可用:

----------------
arguments : players/ersykzer/
key : player/players/ersykzer/
result #<Player:0xaf25378>
player/players/ersykzer/ #<Player:0xaf25378> 3600
----------------
arguments : player/ersykzer/
key : canyonsolo/player/ersykzer/
result 
canyonsolo/player/ersykzer/  3600
GET http://ws.maniaplanet.com:80/canyon/rankings/solo/player/ersykzer/
--> 200 OK 115 (127.7ms)
write : true
----------------
arguments : player/ersykzer/
key : canyonmulti/player/ersykzer/
result 
canyonmulti/player/ersykzer/  3600
GET http://ws.maniaplanet.com:80/canyon/rankings/multiplayer/player/ersykzer/
--> 200 OK 169 (118.0ms)
write : true
----------------
arguments : zone/ {:params=>{:offset=>113, :length=>10}}
key : canyonsolo/zone//{:params=>{:offset=>113, :length=>10}}
result #<CanyonSolo:0xaee3180>
canyonsolo/zone//{:params=>{:offset=>113, :length=>10}} #<CanyonSolo:0xaee3180> 3600
----------------
arguments : zone/ {:params=>{:offset=>"2", :length=>10}}
key : canyonmulti/zone//{:params=>{:offset=>"2", :length=>10}}
result #<CanyonMulti:0xaeded10>
canyonmulti/zone//{:params=>{:offset=>"2", :length=>10}} #<CanyonMulti:0xaeded10> 3600

如您所见,尽管使用相同的键,但两个元素canyonsolo/player/ersykzer/canyonmulti/player/ersykzer/不会由缓存返回。为什么它不起作用?(轨道 3.1)

4

1 回答 1

0

从 rails 3.1 升级到 3.2.0 后,它可以工作了。我的代码没有任何变化。这似乎是一个rails错误。

于 2012-07-05T12:21:47.200 回答