This commit is contained in:
恍兮惚兮 2024-08-10 04:00:03 +08:00
parent f2e25875c8
commit 3ccd80a1e2
2 changed files with 33 additions and 15 deletions

View File

@ -685,24 +685,30 @@ def get_time_stamp(ct=None, ms=True):
class LRUCache: class LRUCache:
def __init__(self, capacity: int): def __init__(self, capacity: int):
self.cache = {} self.cache = {}
self.Lock = threading.Lock()
self.capacity = capacity self.capacity = capacity
self.order = [] self.order = []
def setcap(self, cap): def setcap(self, cap):
with self.Lock:
if cap == -1: if cap == -1:
cap = 9999999999 cap = 9999999999
self.capacity = cap self.capacity = cap
while len(self.cache) > self.capacity: while len(self.cache) > self.capacity:
self.cache.popitem(last=False) self.cache.popitem(last=False)
def get(self, key: int) -> bool: def __get(self, key):
if key in self.cache: if key in self.cache:
self.order.remove(key) self.order.remove(key)
self.order.append(key) self.order.append(key)
return True return self.cache[key]
return False return None
def put(self, key: int) -> None: def get(self, key):
with self.Lock:
return self.__get(key)
def __put(self, key, value=True) -> None:
if not self.capacity: if not self.capacity:
return return
if key in self.cache: if key in self.cache:
@ -710,13 +716,18 @@ class LRUCache:
elif len(self.order) == self.capacity: elif len(self.order) == self.capacity:
old_key = self.order.pop(0) old_key = self.order.pop(0)
del self.cache[old_key] del self.cache[old_key]
self.cache[key] = None self.cache[key] = value
self.order.append(key) self.order.append(key)
def put(self, key, value=True) -> None:
with self.Lock:
self.__put(key, value)
def test(self, key): def test(self, key):
_ = self.get(key) with self.Lock:
_ = self.__get(key)
if not _: if not _:
self.put(key) self.__put(key)
return _ return _

View File

@ -3,6 +3,7 @@ import functools, threading
from myutils.wrapper import threader from myutils.wrapper import threader
from traceback import print_exc from traceback import print_exc
from myutils.proxy import getproxy from myutils.proxy import getproxy
from myutils.utils import LRUCache
class TTSbase: class TTSbase:
@ -53,6 +54,7 @@ class TTSbase:
self.typename = typename self.typename = typename
self.playaudiofunction = playaudiofunction self.playaudiofunction = playaudiofunction
self.uid = uid self.uid = uid
self.LRUCache = LRUCache(3)
if privateconfig is None: if privateconfig is None:
self.privateconfig = globalconfig["reader"][self.typename] self.privateconfig = globalconfig["reader"][self.typename]
else: else:
@ -80,9 +82,14 @@ class TTSbase:
if len(self.voicelist) == 0: if len(self.voicelist) == 0:
return return
try: try:
key = content, self.rate, self.voice
data = self.LRUCache.get(key)
if data:
return callback(data)
data = self.speak(content, self.rate, self.voice) data = self.speak(content, self.rate, self.voice)
if data and len(data): if data:
callback(data) callback(data)
self.LRUCache.put(key, data)
except: except:
print_exc() print_exc()
return return