Move global user cache to a WeakValueDictionary.
This commit is contained in:
		@@ -507,14 +507,26 @@ class Client:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    # helpers/getters
 | 
					    # helpers/getters
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @property
 | 
				
			||||||
 | 
					    def users(self):
 | 
				
			||||||
 | 
					        """Returns a list of all the :class:`User` the bot can see."""
 | 
				
			||||||
 | 
					        return list(self.connection._users.values())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_channel(self, id):
 | 
					    def get_channel(self, id):
 | 
				
			||||||
        """Returns a :class:`Channel` or :class:`PrivateChannel` with the following ID. If not found, returns None."""
 | 
					        """Returns a :class:`abc.GuildChannel` or :class:`abc.PrivateChannel` with the following ID.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        If not found, returns None.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        return self.connection.get_channel(id)
 | 
					        return self.connection.get_channel(id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_guild(self, id):
 | 
					    def get_guild(self, id):
 | 
				
			||||||
        """Returns a :class:`Guild` with the given ID. If not found, returns None."""
 | 
					        """Returns a :class:`Guild` with the given ID. If not found, returns None."""
 | 
				
			||||||
        return self.connection._get_guild(id)
 | 
					        return self.connection._get_guild(id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_user(self, id):
 | 
				
			||||||
 | 
					        """Returns a :class:`User` with the given ID. If not found, returns None."""
 | 
				
			||||||
 | 
					        return self.connection.get_user(id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def get_all_emojis(self):
 | 
					    def get_all_emojis(self):
 | 
				
			||||||
        """Returns a generator with every :class:`Emoji` the client can see."""
 | 
					        """Returns a generator with every :class:`Emoji` the client can see."""
 | 
				
			||||||
        for guild in self.guilds:
 | 
					        for guild in self.guilds:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,6 +42,7 @@ import copy, enum, math
 | 
				
			|||||||
import datetime
 | 
					import datetime
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					import weakref
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ListenerType(enum.Enum):
 | 
					class ListenerType(enum.Enum):
 | 
				
			||||||
    chunk = 0
 | 
					    chunk = 0
 | 
				
			||||||
@@ -66,8 +67,8 @@ class ConnectionState:
 | 
				
			|||||||
        self.user = None
 | 
					        self.user = None
 | 
				
			||||||
        self.sequence = None
 | 
					        self.sequence = None
 | 
				
			||||||
        self.session_id = None
 | 
					        self.session_id = None
 | 
				
			||||||
 | 
					        self._users = weakref.WeakValueDictionary()
 | 
				
			||||||
        self._calls = {}
 | 
					        self._calls = {}
 | 
				
			||||||
        self._users = {}
 | 
					 | 
				
			||||||
        self._emojis = {}
 | 
					        self._emojis = {}
 | 
				
			||||||
        self._guilds = {}
 | 
					        self._guilds = {}
 | 
				
			||||||
        self._voice_clients = {}
 | 
					        self._voice_clients = {}
 | 
				
			||||||
@@ -133,6 +134,9 @@ class ConnectionState:
 | 
				
			|||||||
            self._users[user_id] = user = User(state=self, data=data)
 | 
					            self._users[user_id] = user = User(state=self, data=data)
 | 
				
			||||||
            return user
 | 
					            return user
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_user(self, id):
 | 
				
			||||||
 | 
					        return self._users.get(id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def store_emoji(self, guild, data):
 | 
					    def store_emoji(self, guild, data):
 | 
				
			||||||
        emoji_id = int(data['id'])
 | 
					        emoji_id = int(data['id'])
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,7 +61,7 @@ class User(discord.abc.Messageable):
 | 
				
			|||||||
        Specifies if the user is a bot account.
 | 
					        Specifies if the user is a bot account.
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    __slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', '_state')
 | 
					    __slots__ = ('name', 'id', 'discriminator', 'avatar', 'bot', '_state', '__weakref__')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, *, state, data):
 | 
					    def __init__(self, *, state, data):
 | 
				
			||||||
        self._state = state
 | 
					        self._state = state
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user