我有系統發送併發得到查詢到couchbase。每當系統獲得密鑰時,它應該更新(延長)其生命週期。時間的長短並不是那麼重要,而是按天來衡量:主要想法是在一段時間內沒有人得到鑰匙(例如20天)後鑰匙應該被移除。更新密鑰在couchbase中的時間
我想應該使用touch
操作,但是我應該使用lock
鍵,這會讓事情變得更加困難?是否可以使用memcached包(它似乎沒有lock
API,但也許gets
應該這樣做)?
import pylibmc
class Cache(Singleton):
def init(self):
self.mc = pylibmc.Client(
# connection settings here
)
def get(self, key):
"""get key without locking it and update lifetime"""
result = self.mc.get(key)
if result:
# prolongate key for another 20 days
self.mc.touch(key, 60*60*24*20)
return result
def get_and_lock(self, key):
"""lock the key while getting it and update lifetime"""
# should use couchbase package as memcached does not have lock API
# or use 'gets' instead?