Skip to content
Merged
23 changes: 15 additions & 8 deletions interactions/api/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
overload,
)

import interactions

if TYPE_CHECKING:
from .models import Snowflake

Expand All @@ -38,8 +40,12 @@ class Storage(Generic[_T]):
def __repr__(self) -> str:
return f"<{self.__class__.__name__} object containing {len(self.values)} items.>"

def __init__(self) -> None:
self.values: Dict["Key", _T] = {}
def __init__(self, limit: Optional[int] = float("inf")) -> None:
"""

:param Optional[int] limit: The maximum number of items to store
"""
self.values: interactions.LRUDict["Key", _T] = interactions.LRUDict(max_items=limit)

def merge(self, item: _T, id: Optional["Key"] = None) -> None:
"""
Expand Down Expand Up @@ -158,13 +164,14 @@ class Cache:
:ivar defaultdict[Type, Storage] storages: A dictionary denoting the Type and the objects that correspond to the Type.
"""

__slots__ = "storages"
__slots__ = ("storages", "config")

def __init__(self) -> None:
self.storages: defaultdict[Type[_T], Storage[_T]] = defaultdict(Storage)
def __init__(self, config: Dict[Type[_T], int] = None) -> None:
self.storages: Dict[Type[_T], Storage[_T]] = defaultdict(Storage)

if config is not None:
for type_, limit in config.items():
self.storages[type_] = Storage(limit)

def __getitem__(self, item: Type[_T]) -> Storage[_T]:
return self.storages[item]


ref_cache = Cache() # noqa
7 changes: 5 additions & 2 deletions interactions/api/gateway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

if TYPE_CHECKING:
from ...client.context import _Context
from ..cache import Storage
from ..cache import Cache, Storage
from ..models.gw import GuildMembers

log = get_logger("gateway")
Expand Down Expand Up @@ -93,6 +93,7 @@ class WebSocketClient:
"_ratelimiter",
"_http",
"_client",
"_cache",
"__closed", # placeholder to work with variables atm. its event variant of "_closed"
"_options",
"_intents",
Expand Down Expand Up @@ -120,6 +121,7 @@ def __init__(
self,
token: str,
intents: Intents,
cache: "Cache",
session_id: Optional[str] = MISSING,
sequence: Optional[int] = MISSING,
shards: Optional[List[Tuple[int]]] = MISSING,
Expand Down Expand Up @@ -152,6 +154,7 @@ def __init__(
loop=self._loop if version_info < (3, 10) else None
)
self._http: HTTPClient = token
self._cache: "Cache" = cache

self._client: Optional["ClientWebSocketResponse"] = None

Expand Down Expand Up @@ -243,7 +246,7 @@ async def run(self) -> None:
"""

if isinstance(self._http, str):
self._http = HTTPClient(self._http)
self._http = HTTPClient(self._http, self._cache)

url = await self._http.get_gateway()
self.ws_url = url
Expand Down
8 changes: 5 additions & 3 deletions interactions/api/http/channel.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from typing import Dict, List, Optional, Union
from typing import TYPE_CHECKING, Dict, List, Optional, Union

from ...api.cache import Cache
from ..error import LibraryException
from ..models.channel import Channel
from ..models.message import Message
from ..models.misc import Snowflake
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("ChannelRequest",)


class ChannelRequest:
_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
25 changes: 7 additions & 18 deletions interactions/api/http/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any, Optional, Tuple
from typing import TYPE_CHECKING, Any, Optional, Tuple

from ...api.cache import Cache, ref_cache
from .channel import ChannelRequest
from .emoji import EmojiRequest
from .guild import GuildRequest
Expand All @@ -17,6 +16,9 @@
from .user import UserRequest
from .webhook import WebhookRequest

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("HTTPClient",)


Expand Down Expand Up @@ -51,25 +53,12 @@ class HTTPClient(

token: str
_req: _Request
cache: Cache
cache: "Cache"

def __init__(self, token: str):
def __init__(self, token: str, cache: "Cache"): # noqa skip the no super imports
self.token = token
self._req = _Request(self.token)
self.cache = ref_cache
UserRequest.__init__(self)
MessageRequest.__init__(self)
GuildRequest.__init__(self)
ChannelRequest.__init__(self)
InviteRequest.__init__(self)
ThreadRequest.__init__(self)
ReactionRequest.__init__(self)
StickerRequest.__init__(self)
InteractionRequest.__init__(self)
WebhookRequest.__init__(self)
ScheduledEventRequest.__init__(self)
EmojiRequest.__init__(self)
MemberRequest.__init__(self)
self.cache = cache

# An ideology is that this client does every single HTTP call, which reduces multiple ClientSessions in theory
# because of how they are constructed/closed. This includes Gateway
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/emoji.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from typing import List, Optional
from typing import TYPE_CHECKING, List, Optional

from ...api.cache import Cache
from ..models.emoji import Emoji
from ..models.guild import Guild
from ..models.misc import Snowflake
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("EmojiRequest",)


class EmojiRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/guild.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from typing import Any, Dict, List, Optional
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from urllib.parse import quote

from ...api.cache import Cache
from ..models.channel import Channel
from ..models.guild import Guild
from ..models.role import Role
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("GuildRequest",)


class GuildRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/interaction.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import List, Optional, Union
from typing import TYPE_CHECKING, List, Optional, Union

from ...api.cache import Cache
from ..models import Snowflake
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("InteractionRequest",)


class InteractionRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
8 changes: 5 additions & 3 deletions interactions/api/http/invite.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from typing import Optional
from typing import TYPE_CHECKING, Optional

from ...api.cache import Cache
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("InviteRequest",)


class InviteRequest:
_req = _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/member.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from typing import List, Optional
from typing import TYPE_CHECKING, List, Optional

from ...api.cache import Cache
from ..models.guild import Guild
from ..models.member import Member
from ..models.misc import Snowflake
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("MemberRequest",)


class MemberRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
8 changes: 5 additions & 3 deletions interactions/api/http/message.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from typing import List, Optional, Union
from typing import TYPE_CHECKING, List, Optional, Union

from aiohttp import MultipartWriter

from ...api.cache import Cache
from ...utils.missing import MISSING
from ..models.message import Embed, Message, Sticker
from ..models.misc import AllowedMentions, File, Snowflake
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("MessageRequest",)


class MessageRequest:
_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/reaction.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from typing import List
from typing import TYPE_CHECKING, List

from ...api.cache import Cache
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("ReactionRequest",)


class ReactionRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/scheduledEvent.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from typing import List
from typing import TYPE_CHECKING, List

from ...api.cache import Cache
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("ScheduledEventRequest",)


class ScheduledEventRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
9 changes: 5 additions & 4 deletions interactions/api/http/sticker.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from typing import List, Optional
from typing import TYPE_CHECKING, List, Optional

from aiohttp import FormData

from ...api.cache import Cache
from ..models.misc import File
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("StickerRequest",)


class StickerRequest:

_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
8 changes: 5 additions & 3 deletions interactions/api/http/thread.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
from typing import Dict, List, Optional
from typing import TYPE_CHECKING, Dict, List, Optional

from aiohttp import MultipartWriter

from ...api.cache import Cache
from ...utils.missing import MISSING
from ..models.channel import Channel
from ..models.misc import File
from .request import _Request
from .route import Route

if TYPE_CHECKING:
from ...api.cache import Cache

__all__ = ("ThreadRequest",)


class ThreadRequest:
_req: _Request
cache: Cache
cache: "Cache"

def __init__(self) -> None:
pass
Expand Down
Loading