From 8654d8cc486ca3769c279dfc975335eba2f018ef Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 8 Nov 2012 19:29:57 +0100 Subject: [PATCH] Further migrated script caches to the central cache location, removing old cruft. --- src/scripts/scripts.py | 9 ++++----- src/server/caches.py | 17 +++++++++++++---- src/typeclasses/models.py | 32 -------------------------------- 3 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/scripts/scripts.py b/src/scripts/scripts.py index 9b494446ec..334f43bdc0 100644 --- a/src/scripts/scripts.py +++ b/src/scripts/scripts.py @@ -11,9 +11,9 @@ from collections import defaultdict from twisted.internet.defer import maybeDeferred from twisted.internet.task import LoopingCall from django.conf import settings +from src.server import caches from src.server.sessionhandler import SESSIONS from src.typeclasses.typeclass import TypeClass -from src.typeclasses.models import _ATTRIBUTE_CACHE from src.scripts.models import ScriptDB from src.comms import channelhandler from src.utils import logger @@ -449,7 +449,6 @@ class ClearAttributeCache(Script): self.persistent = True def at_repeat(self): "called every 2 hours. Sets a max attr-cache limit to 100 MB." # enough for normal usage? - global _ATTRIBUTE_CACHE - size = sum([sum([getsizeof(obj) for obj in dic.values()]) for dic in _ATTRIBUTE_CACHE.values()]) - if size / 1024.0 > _ATTRIBUTE_CACHE_MAXSIZE: - _ATTRIBUTE_CACHE = defaultdict(dict) + attr_cache_size, _, _ = caches.get_cache_sizes() + if attr_cache_size > _ATTRIBUTE_CACHE_MAXSIZE: + caches.flush_attr_cache() diff --git a/src/server/caches.py b/src/server/caches.py index fdbc4ab405..769939f1a2 100644 --- a/src/server/caches.py +++ b/src/server/caches.py @@ -89,9 +89,12 @@ def del_field_cache(obj, name): def flush_field_cache(obj): "On-model cache resetter" hid = hashid(obj) + global _FIELD_CACHE if hid: - global _FIELD_CACHE del _FIELD_CACHE[hashid(obj)] + else: + # clean cache completely + _FIELD_CACHE = defaultdict(dict) # on-object property cache (unrelated to database) # Note that the get/set_prop_cache handler do not actually @@ -128,10 +131,12 @@ def del_prop_cache(obj, name): def flush_field_cache(obj): "On-model cache resetter" hid = hashid(obj) + global _PROP_CACHE if hid: - global _PROP_CACHE del _PROP_CACHE[hashid(obj)] - + else: + # clean cache completely + _PROP_CACHE = defaultdict(dict) # attribute cache @@ -163,4 +168,8 @@ def flush_attr_cache(obj): Flush the attribute cache for this object. """ global _ATTR_CACHE - del _ATTR_CACHE[hashid(obj)] + if obj: + del _ATTR_CACHE[hashid(obj)] + else: + # clean cache completely + _ATTR_CACHE = defaultdict(dict) diff --git a/src/typeclasses/models.py b/src/typeclasses/models.py index df21c339b9..70c6e9b2e1 100644 --- a/src/typeclasses/models.py +++ b/src/typeclasses/models.py @@ -59,38 +59,6 @@ _DA = object.__delattr__ _PLOADS = pickle.loads _PDUMPS = pickle.dumps - -# Property Cache mechanism. - -#def _get_cache(obj, name): -# "On-model Cache handler." -# try: -# return _GA(obj, "_cached_db_%s" % name) -# except AttributeError: -# val = _GA(obj, "db_%s" % name) -# _SA(obj, "_cached_db_%s" % name, val) -# return val -#def set_prop_cache(obj, name, val): -# "On-model Cache setter. Also updates database." -# _SA(obj, "db_%s" % name, val) -# _GA(obj, "save")() -# _SA(obj, "_cached_db_%s" % name, val) -#def del_prop_cache(obj, name): -# "On-model cache deleter" -# try: -# _DA(obj, "_cached_db_%s" % name) -# except AttributeError: -# pass -#def _clean_cache(obj): -# "On-model cache resetter" -# [_DA(obj, cname) for cname in obj.__dict__.keys() if cname.startswith("_cached_db_")] - - -# this cache holds the attributes loaded on objects, one dictionary -# of attributes per object. -_ATTRIBUTE_CACHE = defaultdict(dict) - - #------------------------------------------------------------ # # Attributes