Skip to content

Added entity instances caching. #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Nov 26, 2019
Prev Previous commit
Next Next commit
Moved the "cache" property to the Entity class so that it gets added …
…to the wiki.
  • Loading branch information
jordanbriere committed Nov 25, 2019
commit d9f87dc30356ab5fda3ecc9a863ddd6d90e41411
18 changes: 11 additions & 7 deletions addons/source-python/packages/source-python/entities/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __call__(cls, index, caching=True):
"""
# Let's first lookup for a cached instance
if caching:
obj = cls.cache.get(index, None)
obj = cls._cache.get(index, None)
if obj is not None:
return obj

Expand All @@ -112,22 +112,18 @@ def __call__(cls, index, caching=True):

# Let's cache the new instance we just created
if caching:
cls.cache[index] = obj
cls._cache[index] = obj

# We are done, let's return the instance
return obj

@property
def cache(self):
return self._cache

def _on_entity_deleted(cls, base_entity):
"""Called when an entity is deleted."""
if not base_entity.is_networked():
return

# Cleanup the cache
cls.cache.pop(base_entity.index, None)
cls._cache.pop(base_entity.index, None)


class Entity(BaseEntity, metaclass=_EntityCaching):
Expand Down Expand Up @@ -289,6 +285,14 @@ def _obj(cls, ptr):
"""Return an entity instance of the given pointer."""
return cls(index_from_pointer(ptr))

@property
def cache(self):
"""Returns the cached instances of this class.

:rtype: dict
"""
return self._cache

@property
def index(self):
"""Return the entity's index.
Expand Down