Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cache replyto chat so no need to replyto every msg * Mod modify cache replied chat logic * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/slave_message.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * Update efb_telegram_master/cache.py Co-Authored-By: Eana Hufwe <ilove@1a23.com> * fix code review problem
- Loading branch information
1 parent
d25c1f5
commit e438c7f
Showing
4 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# coding: utf-8 | ||
# modified from [messud4312]https://my.oschina.net/u/914655/blog/1799159 | ||
|
||
import collections | ||
import time | ||
import weakref | ||
|
||
class LocalCache(): | ||
|
||
# Wrapping dict as it requires subclassing for weak reference. | ||
class Dict(dict): | ||
def __del__(self): | ||
pass | ||
|
||
def __init__(self, maxlen=20): | ||
self.weak = weakref.WeakValueDictionary() | ||
self.strong = collections.deque(maxlen=maxlen) | ||
|
||
@staticmethod | ||
def now_time(): | ||
return int(time.time()) | ||
|
||
def get(self, key): | ||
val = self.weak.get(key, None) | ||
if val is not None: | ||
expire = val['expire'] | ||
value = val['value'] | ||
if self.now_time() > expire: | ||
return None | ||
else: | ||
return value | ||
else: | ||
return None | ||
|
||
def set(self, key, value, expire=3600): | ||
# strong_ref prevent object from being collected by gc. | ||
self.weak[key] = strong_ref = LocalCache.Dict({ | ||
'expire': self.now_time() + expire, | ||
'value': value | ||
}) | ||
# Enqueue the element and waiting to be collected by gc once popped. | ||
self.strong.append(strong_ref) | ||
|
||
def remove(self, key): | ||
return self.weak.pop(key, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters