Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wiseplat
GitHub Repository: wiseplat/python-code
Path: blob/master/ invest-robot-contest_TinkoffBotTwitch-main/venv/lib/python3.8/site-packages/twitchio/cache.py
7771 views
1
"""
2
The MIT License (MIT)
3
4
Copyright (c) 2017-2021 TwitchIO
5
6
Permission is hereby granted, free of charge, to any person obtaining a
7
copy of this software and associated documentation files (the "Software"),
8
to deal in the Software without restriction, including without limitation
9
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
and/or sell copies of the Software, and to permit persons to whom the
11
Software is furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
DEALINGS IN THE SOFTWARE.
23
"""
24
25
import time
26
import functools
27
from typing import Hashable
28
29
30
__all__ = ("TimedCache", "user_cache", "id_cache")
31
32
33
class TimedCache(dict):
34
def __init__(self, seconds: int):
35
self.__timeout = seconds
36
super().__init__()
37
38
def _verify_cache(self):
39
now = time.monotonic()
40
to_remove = [key for (key, (_, exp)) in self.items() if now > (exp + self.__timeout)]
41
for k in to_remove:
42
del self[k]
43
44
def __getitem__(self, key):
45
self._verify_cache()
46
return super().__getitem__(key)[0]
47
48
def __setitem__(self, key, value):
49
super().__setitem__(key, (value, time.monotonic()))
50
51
def __contains__(self, key):
52
self._verify_cache()
53
return {a: b[0] for a, b in self.items()}.__contains__(key)
54
55
56
def user_cache(timer=300):
57
cache = TimedCache(timer)
58
59
def wraps(func):
60
@functools.wraps(func)
61
async def _wraps(cls, names: list = None, ids: list = None, force=False, token=None):
62
if not force:
63
existing = []
64
if names:
65
existing.extend([cache[x] for x in names if x in cache])
66
67
if ids:
68
existing.extend([cache[x] for x in ids if x in cache])
69
70
if len(existing) == (len(names) if names else 0) + (len(ids) if ids else 0):
71
return existing
72
73
values = await func(cls, names=names, ids=ids, token=token)
74
for v in values:
75
cache[v.id] = v
76
cache[v.name] = v
77
78
return values
79
80
return _wraps
81
82
return wraps
83
84
85
def id_cache(timer=300):
86
cache = TimedCache(timer)
87
88
def wraps(func):
89
@functools.wraps(func)
90
def _wraps(cls, id: Hashable):
91
if id in cache:
92
return cache[id]
93
94
value = func(cls, id)
95
if value is not None:
96
cache[id] = value
97
98
return value
99
100
return _wraps
101
102
return wraps
103
104