1- from collections import OrderedDict
2- from typing import Any , List , Optional
1+ from collections import defaultdict
2+ from typing import TYPE_CHECKING , Dict , Generic , List , Optional , Tuple , Type , TypeVar , Union
3+
4+ if TYPE_CHECKING :
5+ from .models import Snowflake
6+
7+ Key = TypeVar ("Key" , Snowflake , Tuple [Snowflake , Snowflake ])
38
49__all__ = (
5- "Item" ,
610 "Storage" ,
711 "Cache" ,
812)
913
14+ _T = TypeVar ("_T" )
15+ _P = TypeVar ("_P" )
1016
11- class Item :
12- """
13- A class representing the defined item in a stored dataset.
1417
15- :ivar str id: The ID of the item.
16- :ivar Any value: The item itself.
17- :ivar Type type: The ID type representation.
18- """
19-
20- __slots__ = ("id" , "value" , "type" )
21-
22- def __init__ (self , id : str , value : Any ) -> None :
23- """
24- :param id: The item's ID.
25- :type id: str
26- :param value: The item itself.
27- :type value: Any
28- """
29- self .id = id
30- self .value = value
31- self .type = type (value )
32-
33-
34- class Storage :
18+ class Storage (Generic [_T ]):
3519 """
3620 A class representing a set of items stored as a cache state.
3721
38- :ivar List[Item ] values: The list of items stored.
22+ :ivar Dict[Union[Snowflake, Tuple[Snowflake, Snowflake]], Any ] values: The list of items stored.
3923 """
4024
41- __slots__ = "values"
25+ __slots__ = ( "values" ,)
4226
4327 def __repr__ (self ) -> str :
4428 return f"<{ self .__class__ .__name__ } object containing { len (self .values )} items.>"
4529
4630 def __init__ (self ) -> None :
47- self .values = OrderedDict ()
31+ self .values : Dict [ "Key" , _T ] = {}
4832
49- def add (self , item : Item ) -> OrderedDict :
33+ def add (self , item : _T , id : Optional [ "Key" ] = None ) -> None :
5034 """
5135 Adds a new item to the storage.
5236
5337 :param item: The item to add.
54- :type item: Item
55- :return : The new storage .
56- :rtype: OrderedDict
38+ :type item: Any
39+ :param id : The unique id of the item .
40+ :type id: Optional[Union[Snowflake, Tuple[Snowflake, Snowflake]]]
5741 """
58- self .values .update ({item .id : item .value })
59- return self .values
42+ self .values [id or item .id ] = item
6043
61- def get (self , id : str ) -> Optional [ Item ]:
44+ def get (self , id : "Key" , default : Optional [ _P ] = None ) -> Union [ _T , _P ]:
6245 """
6346 Gets an item from the storage.
6447
6548 :param id: The ID of the item.
66- :type id: str
49+ :type id: Union[Snowflake, Tuple[Snowflake, Snowflake]]
50+ :param default: The default value to return if the item is not found.
51+ :type default: Optional[Any]
6752 :return: The item from the storage if any.
68- :rtype: Optional[Item ]
53+ :rtype: Optional[Any ]
6954 """
70- if id in self .values .keys ():
71- return self .values [id ]
55+ return self .values .get (id , default )
7256
73- def update (self , item : Item ) -> Optional [ Item ] :
57+ def update (self , data : Dict [ "Key" , _T ]) :
7458 """
75- Updates an item from the storage.
59+ Updates multiple items from the storage.
7660
77- :param item: The item to update.
78- :return: The updated item, if stored.
79- :rtype: Optional[Item]
61+ :param data: The data to update with.
62+ :type data: dict
8063 """
81- if item .id in self .values .keys ():
82- self .values [item .id ] = item .value
83- return self .values [
84- id
85- ] # fetches from cache to see if its saved properly, instead of returning input.
64+ self .values .update (data )
65+
66+ def pop (self , key : "Key" , default : Optional [_P ] = None ) -> Union [_T , _P ]:
67+ try :
68+ return self .values .pop (key )
69+ except KeyError :
70+ return default
8671
8772 @property
8873 def view (self ) -> List [dict ]:
@@ -93,45 +78,32 @@ def view(self) -> List[dict]:
9378 """
9479 return [v ._json for v in self .values .values ()]
9580
81+ def __getitem__ (self , item : "Key" ) -> _T :
82+ return self .values .__getitem__ (item )
83+
84+ def __setitem__ (self , key : "Key" , value : _T ) -> None :
85+ return self .values .__setitem__ (key , value )
86+
87+ def __delitem__ (self , key : "Key" ) -> None :
88+ return self .values .__delitem__ (key )
89+
9690
9791class Cache :
9892 """
9993 A class representing the cache.
10094 This cache collects all of the HTTP requests made for
10195 the represented instances of the class.
10296
103- :ivar Cache dms: The cached Direct Messages.
104- :ivar Cache self_guilds: The cached guilds upon gateway connection.
105- :ivar Cache guilds: The cached guilds after ready.
106- :ivar Cache channels: The cached channels of guilds.
107- :ivar Cache roles: The cached roles of guilds.
108- :ivar Cache members: The cached members of guilds and threads.
109- :ivar Cache messages: The cached messages of DMs and channels.
110- :ivar Cache interactions: The cached interactions upon interaction.
97+ :ivar defaultdict[Type, Storage] storages:
11198 """
11299
113- __slots__ = (
114- "dms" ,
115- "self_guilds" ,
116- "guilds" ,
117- "channels" ,
118- "roles" ,
119- "members" ,
120- "messages" ,
121- "users" ,
122- "interactions" ,
123- )
100+ __slots__ = "storages"
124101
125102 def __init__ (self ) -> None :
126- self .dms = Storage ()
127- self .self_guilds = Storage ()
128- self .guilds = Storage ()
129- self .channels = Storage ()
130- self .roles = Storage ()
131- self .members = Storage ()
132- self .messages = Storage ()
133- self .users = Storage ()
134- self .interactions = Storage ()
103+ self .storages : defaultdict [Type [_T ], Storage [_T ]] = defaultdict (Storage )
104+
105+ def __getitem__ (self , item : Type [_T ]) -> Storage [_T ]:
106+ return self .storages [item ]
135107
136108
137109ref_cache = Cache () # noqa
0 commit comments