如何使用Dalli更新MemCached中的到期时间? [英] How to update expiration time in MemCached using Dalli?

查看:110
本文介绍了如何使用Dalli更新MemCached中的到期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails(v3.2.13),Dalli(v2.6.4)和MemCached(v1.4.13).

I'm using Ruby on Rails (v3.2.13), Dalli (v2.6.4) and MemCached (v1.4.13).

我这样缓存:

    result = Rails.cache.fetch("test_key", :expires_in => 1.week) do
        get_data()    # slow call, result of which should be cached
    end

我想根据数据更新缓存过期日期,因为我的某些数据可以保存更长的时间.

I want to update cache expiration date based on the data, since some of my data can be kept longer.

现在,以下代码可以完成这项工作:

Right now the following code does the job:

    if keep_longer(result)
        Rails.cache.write("test_key", result, :expires_in => 6.months)
    end

我知道MemCached支持"touch"命令,该命令允许在不发送值的情况下更新到期日期.而且我看不到如何通过Dalli gem使用它.有没有一种方法可以在不重新发送结果的情况下更新到期日期?

I know that MemCached supports "touch" command that allows to update expiration date without sending the value. And I don't see how to use it through the Dalli gem. Is there a way to update expiration date without resending the result?

更新:

    Rails.cache.dalli.touch('some_key', 24.hours)

这应该有效,但对我而言却无效.对您有用吗?

This should work, but for me it doesn't. Does it work for you?

这是一个可以尝试的小例子.在IRB中执行以下代码后

Here is small example you can try. After execution of the following code in the IRB

      dc = Dalli::Client.new("localhost:11211")
      dc.set("test_key", "test_value", 5.minutes)
      dc.set(     "key",      "value", 5.minutes)
      dc.touch(   "key",   10.minutes)

我正在使用telnet检查到期日期:

I'm checking the expiration dates using telnet:

telnet localhost 11211

然后给定正确的slab_id并使用"stats cachedump"命令获取以秒为单位的到期时间:

Then given the correct slab_id and using "stats cachedump" command I obtain expiration times in seconds:

stats cachedump 1 0

ITEM key [9 b; 1375733492 s]
ITEM test_key [14 b; 1375905957 s]

请注意,键"key"的到期时间指向过去.当我期望它比"test_key"到期时间晚300秒时.我还注意到,密钥"过期时间大约是在MemCached服务器启动之前的1秒.这可能表明该密钥没有到期时间.实际上,密钥"不会在不久的将来删除.

Note that the expiration time of the key "key" points to the past. When I expect it to be 300 seconds later than "test_key" expiration time. Also I noticed that "key" expiration time is approximately 1 second before the MemCached server has started. Which probably indicates that this key has no expiration time. And in fact "key" doesn't get deleted in the near future.

我是在做错什么还是Dalli/MemCached的错误?

Am I doing something wrong or it is a bug of Dalli/MemCached?

推荐答案

Dalli确实支持这一点-Dalli::Client上有一个touch方法,其功能与锡盒上所说的完全相同. Rails.cache返回一个缓存存储区,而不是基础Dalli对象,因此您需要这样做

Dalli does support this - there's a touch method on Dalli::Client that does exactly what it says on the tin. Rails.cache returns a cache store rather than the underlying Dalli object so you need to do

Rails.cache.dalli.touch('some_key', 24.hours)

要将缓存条目的有效期延长24小时(当然,内存缓存可能仍会决定删除该条目)

To bump the cache entry's expiry time by 24 hours (and of course memcache may decide to drop the entry anyway)

这篇关于如何使用Dalli更新MemCached中的到期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆