本文整理汇总了Python中cache.Cache.expire方法的典型用法代码示例。如果您正苦于以下问题:Python Cache.expire方法的具体用法?Python Cache.expire怎么用?Python Cache.expire使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cache.Cache
的用法示例。
在下文中一共展示了Cache.expire方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UTKhashmir
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import expire [as 别名]
class UTKhashmir(khashmir.KhashmirBase):
_Node = UTNode
def setup(self, host, port, data_dir, rlcount, checkpoint=True):
khashmir.KhashmirBase.setup(self, host, port,data_dir, rlcount, checkpoint)
self.cur_token = self.last_token = sha('')
self.tcache = Cache()
self.gen_token(loop=True)
self.expire_cached_tokens(loop=True)
def expire_cached_tokens(self, loop=False):
self.tcache.expire(time() - TOKEN_UPDATE_INTERVAL)
if loop:
self.rawserver.external_add_task(self.expire_cached_tokens, TOKEN_UPDATE_INTERVAL, (True,))
def gen_token(self, loop=False):
self.last_token = self.cur_token
self.cur_token = sha(newID())
if loop:
self.rawserver.external_add_task(self.gen_token, TOKEN_UPDATE_INTERVAL, (True,))
def get_token(self, host, port):
x = self.cur_token.copy()
x.update("%s%s" % (host, port))
h = x.digest()
return h
def val_token(self, token, host, port):
x = self.cur_token.copy()
x.update("%s%s" % (host, port))
a = x.digest()
if token == a:
return True
x = self.last_token.copy()
x.update("%s%s" % (host, port))
b = x.digest()
if token == b:
return True
return False
def addContact(self, host, port, callback=None):
# use dns on host, then call khashmir.addContact
Thread(target=self._get_host, args=[host, port, callback]).start()
def _get_host(self, host, port, callback):
# this exception catch can go away once we actually fix the bug
try:
ip = gethostbyname(host)
except TypeError, e:
raise TypeError(str(e) + (": host(%s) port(%s)" % (repr(host), repr(port))))
self.rawserver.external_add_task(self._got_host, 0, (ip, port, callback))
示例2: hostbroker
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import expire [as 别名]
class hostbroker(Handler):
def __init__(self, server, addr, transport, call_later, max_ul_rate, config, rlcount):
self.server = server
self.addr = addr
self.transport = transport
self.rltransport = KRateLimiter(transport, max_ul_rate, call_later, rlcount, config['max_rate_period'])
self.call_later = call_later
self.connections = Cache(touch_on_access=True)
self.hammerlock = Hammerlock(100, call_later)
self.expire_connections(loop=True)
self.config = config
if not self.config.has_key('pause'):
self.config['pause'] = False
def expire_connections(self, loop=False):
self.connections.expire(bttime() - KRPC_CONNECTION_CACHE_TIME)
if loop:
self.call_later(self.expire_connections, KRPC_CONNECTION_CACHE_TIME, (True,))
def data_came_in(self, addr, datagram):
#if addr != self.addr:
if not self.config['pause'] and self.hammerlock.check(addr):
c = self.connectionForAddr(addr)
c.datagramReceived(datagram, addr)
def connection_lost(self, socket):
## this is like, bad
print ">>> connection lost!", socket
def connectionForAddr(self, addr):
if addr == self.addr:
raise KRPCSelfNodeError()
if not self.connections.has_key(addr):
conn = KRPC(addr, self.server, self.transport, self.rltransport, self.call_later)
self.connections[addr] = conn
else:
conn = self.connections[addr]
return conn
示例3: UTKhashmir
# 需要导入模块: from cache import Cache [as 别名]
# 或者: from cache.Cache import expire [as 别名]
class UTKhashmir(khashmir.KhashmirBase):
_Node = UTNode
def setup(self, host, port, data_dir, rlcount, checkpoint=True):
khashmir.KhashmirBase.setup(self, host, port,data_dir, rlcount, checkpoint)
self.cur_token = self.last_token = sha('')
self.tcache = Cache()
self.gen_token(loop=True)
self.expire_cached_tokens(loop=True)
def expire_cached_tokens(self, loop=False):
self.tcache.expire(time() - TOKEN_UPDATE_INTERVAL)
if loop:
self.rawserver.external_add_task(self.expire_cached_tokens, TOKEN_UPDATE_INTERVAL, (True,))
def gen_token(self, loop=False):
self.last_token = self.cur_token
self.cur_token = sha(newID())
if loop:
self.rawserver.external_add_task(self.gen_token, TOKEN_UPDATE_INTERVAL, (True,))
def get_token(self, host, port):
x = self.cur_token.copy()
x.update("%s%s" % (host, port))
h = x.digest()
return h
def val_token(self, token, host, port):
x = self.cur_token.copy()
x.update("%s%s" % (host, port))
a = x.digest()
if token == a:
return True
x = self.last_token.copy()
x.update("%s%s" % (host, port))
b = x.digest()
if token == b:
return True
return False
def addContact(self, host, port, callback=None):
# use dns on host, then call khashmir.addContact
Thread(target=self._get_host, args=[host, port, callback]).start()
def _get_host(self, host, port, callback):
ip = gethostbyname(host)
self.rawserver.external_add_task(self._got_host, 0, (host, port, callback))
def _got_host(self, host, port, callback):
khashmir.KhashmirBase.addContact(self, host, port, callback)
def announcePeer(self, info_hash, port, callback=None):
""" stores the value for key in the global table, returns immediately, no status
in this implementation, peers respond but don't indicate status to storing values
a key can have many values
"""
def _storeValueForKey(nodes, key=info_hash, value=port, response=callback , table=self.table):
if not response:
# default callback
def _storedValueHandler(sender):
pass
response=_storedValueHandler
action = UTStoreValue(self, key, value, response, self.rawserver.add_task, "announcePeer")
self.rawserver.external_add_task(action.goWithNodes, 0, (nodes,))
# this call is asynch
self.findNode(info_hash, _storeValueForKey)
def krpc_announce_peer(self, info_hash, port, id, token, _krpc_sender):
sender = {'id' : id}
sender['host'] = _krpc_sender[0]
sender['port'] = _krpc_sender[1]
if not self.val_token(token, sender['host'], sender['port']):
raise KRPCProtocolError("Invalid Write Token")
value = compact_peer_info(_krpc_sender[0], port)
self.store[info_hash] = value
n = self.Node().initWithDict(sender)
self.insertNode(n, contacted=0)
return {"id" : self.node.id}
def retrieveValues(self, key):
try:
l = self.store.sample(key, NUM_PEERS)
except KeyError:
l = []
return l
def getPeers(self, info_hash, callback, searchlocal = 1):
""" returns the values found for key in global table
callback will be called with a list of values for each peer that returns unique values
final callback will be an empty list - probably should change to 'more coming' arg
"""
nodes = self.table.findNodes(info_hash, invalid=True)
l = [x for x in nodes if x.invalid]
if len(l) > 4:
nodes = sample(l , 4) + self.table.findNodes(info_hash, invalid=False)[:4]
#.........这里部分代码省略.........