3
A
回答
3
如果您需要这种类型的过期控制,您需要将存储的项目的到期时间与项目本身一起进行。
这通常是通过序列化层完成的。写入memcached的条目存储为条目本身和时间戳。当条目反序列化时,检查时间戳,如果条目已过期,则读取条目将被丢弃并视为缓存未命中。
[ entry, expiresAt ]
对原始分布式缓存条目的TTL通常被设置为无穷大,并且条目只从缓存手动清除,则或者经由LRU政策。
在memcached常见问题解答中,有关于此技术的preventing stampeding requests部分。
0
您需要存储缓存值的时间以及原始超时值。这里有一个Python实现:
class ExpiringCache(BaseCache):
"""A cache that allows you to update values without changing when the
data will expire. We do this by storing when the value was
inserted in the cache and decrementing the timeout when we update.
"""
def get(self, key, *args, **kwargs):
raw_value = super(ExpiringCache, self).get(key, *args, **kwargs)
# we get None on a cache miss, but otherwise it's a 3-tuple
if raw_value is None:
return None
value, start_time, timeout = raw_value
return value
def set(self, key, value, *args, **kwargs):
timeout = kwargs.get('timeout')
raw_value = (value, now(), timeout)
super(ExpiringCache, self).set(key, raw_value, *args, **kwargs)
def update(self, key, value, timeout=None):
"""If this value is still in the cache, update it but reduce the
timeout. If it's not present, just set it.
"""
raw_value = super(ExpiringCache, self).get(key)
if raw_value is None:
self.set(key, value, timeout=timeout)
return
original_value, start_time, original_timeout = raw_value
if not original_timeout:
# we are caching without a timeout, so just set the new value
self.set(key, value, timeout=original_timeout)
return
elapsed_time = (now() - start_time).total_seconds()
remaining_timeout = timeout - elapsed_time
if remaining_timeout > 0:
self.set(key, value, timeout=remaining_timeout)
相关问题
- 1. C#日期时间到UTC时间而不更改时间
- 2. 替换Vim中的项目而不替换旧的替换项目
- 3. 何时以及如何更新Memcached中更改的项目?
- 4. 如何获取memcached中特定项目的过期时间
- 5. memcached过期时间
- 6. 是在memcached更新后更新的到期时间吗?
- 7. Jquery可排序 - 替换项目而不是交换项目
- 8. 替换HTML标记而不更改值
- 9. Memcached替换不工作
- 10. 更改JS日期上的时区而不更改指定的时间?
- 11. 更改日期对象的时区而不更改日期
- 12. PHP Memcached过期时间
- 13. 更改目录时间/日期
- 14. python集合中的替换/更改项目
- 15. 可更改的cookie到期时间
- 16. 替换HTML代码块中的单词而不更改HTML
- 17. 替换R中的文本而不更改其他词
- 18. 如何用Bootstrap 3中的FontAwesome替换Glyphicons而不更改HTML?
- 19. 替换Android项目中的图像,但不会在项目中更新
- 20. 替换observableArray中的项目
- 21. 如何扩展Memcached项目的生命周期而不知道它?
- 22. yii:在使用寻呼机时追加而不是替换CListView中的项目
- 23. 在VBScript中为日期对象设置日期和时间而不更改系统日期/时间
- 24. 重命名项目文件夹而不更改项目名称?
- 25. 更改罐子到期时间
- 26. 更改ActionBarTab项之间的项目
- 27. ObservableCollection中的项目替换不会更新WPF ItemsControl
- 28. 更改mySQL中的日期时间戳
- 29. 如何更换每个项目,而不更改阵列的结构?
- 30. 更改“时间”列“日期时间”