memcached에 생성 시간 후 24 시간 만료되는 항목이 여러 개 있습니다. 만료 시간을 변경하지 않고 해당 항목을 업데이트해야합니다.만료 시간을 변경하지 않고 memcached의 항목 바꾸기
어떻게하면됩니까? 명백히 replace 함수는 만료 매개 변수가 필요합니다.
memcached에 생성 시간 후 24 시간 만료되는 항목이 여러 개 있습니다. 만료 시간을 변경하지 않고 해당 항목을 업데이트해야합니다.만료 시간을 변경하지 않고 memcached의 항목 바꾸기
어떻게하면됩니까? 명백히 replace 함수는 만료 매개 변수가 필요합니다.
만료에 대해 이러한 유형의 제어가 필요한 경우 항목 자체와 함께 저장된 항목의 만료 시간을 지켜야합니다.
일반적인 방법은 직렬화 레이어를 사용하는 것입니다. memcached에 쓰여진 항목은 항목 자체와 타임 스탬프로 저장됩니다. 항목이 직렬화 해제 될 때, 시간 소인이 점검되고, 만기 된 경우, 읽기 항목은 버려지고 캐시 미스로 처리됩니다.
[ entry, expiresAt ]
원시 memcached를 항목의 TTL
은 일반적으로 무한대로 설정하고 항목은 수동으로 만 캐시에서 삭제된다, 또는 LRU 정책을 통해.memcached FAQ에는 preventing stampeding requests에이 기술을 적용하는 섹션이 있습니다.
값이 캐시 된 시간과 원래 시간 초과 값을 저장해야합니다. 다음은 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)