forked from myreader-io/myGPTReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add auto apply whitelist in channel
- Loading branch information
1 parent
ba5b85d
commit 268ae35
Showing
3 changed files
with
167 additions
and
2 deletions.
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,34 @@ | ||
from threading import Thread, Lock | ||
import time | ||
|
||
class TtlSet: | ||
def __init__(self): | ||
self.ttl = {} | ||
self.lock = Lock() | ||
# Start a background thread to periodically remove expired items | ||
t = Thread(target=self._cleaner) | ||
t.daemon = True | ||
t.start() | ||
|
||
def __len__(self): | ||
return len(self.ttl) | ||
|
||
def __contains__(self, value): | ||
return value in self.ttl | ||
|
||
def add(self, value, ttl_seconds): | ||
with self.lock: | ||
self.ttl[value] = time.time() + ttl_seconds | ||
|
||
def discard(self, value): | ||
with self.lock: | ||
self.ttl.pop(value, None) | ||
|
||
def _cleaner(self): | ||
while True: | ||
with self.lock: | ||
now = time.time() | ||
for k, v in list(self.ttl.items()): | ||
if v < now: | ||
self.ttl.pop(k, None) | ||
time.sleep(1) |
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