From 9bfb82927447a15a9486bf4a5df433dbe151497d Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 11 May 2014 15:23:11 +0200 Subject: [PATCH] Implemented WeakSharedMemoryModel for Attributes. --- src/commands/cmdhandler.py | 12 ++++++++++-- src/typeclasses/models.py | 6 +++--- src/utils/idmapper/base.py | 21 +++++++++++++++++++++ src/utils/idmapper/models.py | 2 +- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/commands/cmdhandler.py b/src/commands/cmdhandler.py index 0511ad7c92..33116d915e 100644 --- a/src/commands/cmdhandler.py +++ b/src/commands/cmdhandler.py @@ -35,6 +35,7 @@ command line. The process is as follows: """ +from weakref import WeakValueDictionary from copy import copy from traceback import format_exc from twisted.internet.defer import inlineCallbacks, returnValue @@ -42,13 +43,13 @@ from django.conf import settings from src.comms.channelhandler import CHANNELHANDLER from src.utils import logger, utils from src.commands.cmdparser import at_multimatch_cmd -from src.utils.utils import string_suggestions, make_iter, to_unicode +from src.utils.utils import string_suggestions, to_unicode from django.utils.translation import ugettext as _ __all__ = ("cmdhandler",) _GA = object.__getattribute__ -_CMDSET_MERGE_CACHE = {} +_CMDSET_MERGE_CACHE = WeakValueDictionary() # This decides which command parser is to be used. # You have to restart the server for changes to take effect. @@ -408,6 +409,13 @@ def cmdhandler(called_by, raw_string, testing=False, callertype="session", sessi caller.ndb.last_cmd = yield copy(cmd) else: caller.ndb.last_cmd = None + + # cleanup + del cmd.caller + del cmd.player + del cmd.session + del cmd.cmdset + # Done! This returns a deferred. By default, Evennia does # not use this at all. returnValue(ret) diff --git a/src/typeclasses/models.py b/src/typeclasses/models.py index 00bb2a745d..2d36e16609 100644 --- a/src/typeclasses/models.py +++ b/src/typeclasses/models.py @@ -35,7 +35,7 @@ from django.conf import settings from django.utils.encoding import smart_str from django.contrib.contenttypes.models import ContentType -from src.utils.idmapper.models import SharedMemoryModel +from src.utils.idmapper.models import SharedMemoryModel, WeakSharedMemoryModel from src.server.caches import get_prop_cache, set_prop_cache #from src.server.caches import set_attr_cache @@ -44,7 +44,7 @@ from src.server.models import ServerConfig from src.typeclasses import managers from src.locks.lockhandler import LockHandler from src.utils import logger -from src.utils.utils import make_iter, is_iter, to_str, inherits_from, LazyLoadHandler, NonWeakLazyLoadHandler +from src.utils.utils import make_iter, is_iter, to_str, inherits_from, LazyLoadHandler from src.utils.dbserialize import to_pickle, from_pickle from src.utils.picklefield import PickledObjectField @@ -66,7 +66,7 @@ _DA = object.__delattr__ #------------------------------------------------------------ #class Attribute(SharedMemoryModel): -class Attribute(models.Model): +class Attribute(WeakSharedMemoryModel): """ Abstract django model. diff --git a/src/utils/idmapper/base.py b/src/utils/idmapper/base.py index 832b5d1929..cbf17b4063 100755 --- a/src/utils/idmapper/base.py +++ b/src/utils/idmapper/base.py @@ -271,6 +271,27 @@ class SharedMemoryModel(Model): #blockingCallFromThread(reactor, _save_callback, cls, *args, **kwargs) callFromThread(_save_callback, cls, *args, **kwargs) + +class WeakSharedMemoryModelBase(SharedMemoryModelBase): + """ + Uses a WeakValue dictionary for caching instead of a regular one + """ + def _prepare(cls): + cls.__instance_cache__ = WeakValueDictionary() + super(WeakSharedMemoryModelBase, cls)._prepare() + +class WeakSharedMemoryModel(SharedMemoryModel): + """ + Uses a WeakValue dictionary for caching instead of a regular one + """ + __metaclass__ = WeakSharedMemoryModelBase + class Meta: + abstract = True + def flush_instance_cache(cls): + cls.__instance_cache__ = WeakValueDictionary() + flush_instance_cache = classmethod(flush_instance_cache) + + # Use a signal so we make sure to catch cascades. def flush_cache(**kwargs): def class_hierarchy(root): diff --git a/src/utils/idmapper/models.py b/src/utils/idmapper/models.py index 7f67eb5660..c70fd19092 100755 --- a/src/utils/idmapper/models.py +++ b/src/utils/idmapper/models.py @@ -1,2 +1,2 @@ from django.db.models import * -from base import SharedMemoryModel \ No newline at end of file +from base import SharedMemoryModel, WeakSharedMemoryModel