-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmodule.py
71 lines (60 loc) · 1.49 KB
/
module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from cachetools import Cache
from enum import Enum
from pydantic import BaseModel
from threading import RLock
from app.schemas.types import MediaType
class MediaDigest(BaseModel):
"""
媒体摘要
"""
title: str = None
year: str = None
type: MediaType
tmdb_id: str
imdb_id: str = None
tvdb_id: str = None
class MediaDataSource(Enum):
"""
影视数据来源
"""
MEDIA_LIBRARY = "媒体库"
SUBSCRIBE = "订阅"
SUBSCRIBE_HISTORY = "订阅历史"
class AtomicCache():
"""
原子缓存操作(线程安全)
"""
# 锁
__lock: RLock = None
# 真实缓存
__cache: Cache = None
def __init__(self, cache: Cache):
"""
"""
if cache is None:
raise Exception("Param 'cache' cannot be None.")
self.__lock: RLock = RLock()
self.__cache: Cache = cache
def get_and_set(self, key: any, value: any) -> any:
"""
获取并设置缓存值
:return: 设置前的缓存值
"""
if not key:
raise Exception("Param 'key' cannot be None.")
self.__lock.acquire()
try:
old_value = self.__cache.get(key)
self.__cache[key] = value
return old_value
finally:
self.__lock.release()
def clear(self):
"""
清空缓存
"""
self.__lock.acquire()
try:
self.__cache.clear()
finally:
self.__lock.release()