mirror of
https://github.com/evennia/evennia.git
synced 2026-03-21 23:36:30 +01:00
Implemented and tested set_recache_protection() on all idmapped objects, to avoid them getting recached. Added successfully to nattribute handler.
This commit is contained in:
parent
a617924fb0
commit
5e4af3f851
2 changed files with 10 additions and 6 deletions
|
|
@ -29,6 +29,7 @@ these to create custom managers.
|
|||
import sys
|
||||
import re
|
||||
import traceback
|
||||
import weakref
|
||||
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
|
|
@ -453,6 +454,7 @@ class NAttributeHandler(object):
|
|||
def __init__(self, obj):
|
||||
"initialized on the object"
|
||||
self._store = {}
|
||||
self.obj = weakref.proxy(obj)
|
||||
|
||||
def has(self, key):
|
||||
"Check if object has this attribute or not"
|
||||
|
|
@ -465,11 +467,13 @@ class NAttributeHandler(object):
|
|||
def add(self, key, value):
|
||||
"Add new key and value"
|
||||
self._store[key] = value
|
||||
self.obj.set_recache_protection()
|
||||
|
||||
def remove(self, key):
|
||||
"Remove key from storage"
|
||||
if key in self._store:
|
||||
del self._store[key]
|
||||
self.obj.set_recache_protection(self._store)
|
||||
|
||||
def all(self, return_tuples=False):
|
||||
"List all keys or (keys, values) stored, except _keys"
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ class SharedMemoryModelBase(ModelBase):
|
|||
|
||||
def _prepare(cls):
|
||||
cls.__instance_cache__ = {}
|
||||
cls._idmapper_cache_flush_safe = True
|
||||
cls._idmapper_recache_protection = False
|
||||
super(SharedMemoryModelBase, cls)._prepare()
|
||||
|
||||
def __new__(cls, classname, bases, classdict, *args, **kwargs):
|
||||
|
|
@ -232,16 +232,16 @@ class SharedMemoryModel(Model):
|
|||
def _flush_cached_by_key(cls, key, force=True):
|
||||
"Remove the cached reference."
|
||||
try:
|
||||
if force or cls._idmapper_cache_flush_safe:
|
||||
if force or not cls._idmapper_recache_protection:
|
||||
del cls.__instance_cache__[key]
|
||||
except KeyError:
|
||||
pass
|
||||
_flush_cached_by_key = classmethod(_flush_cached_by_key)
|
||||
|
||||
def _set_recache(cls, mode=True):
|
||||
def set_recache_protection(cls, mode=True):
|
||||
"set if this instance should be allowed to be recached."
|
||||
cls._idmapper_cache_flush_safe = bool(mode)
|
||||
_set_recache = classmethod(_set_recache)
|
||||
cls._idmapper_recache_protection = bool(mode)
|
||||
set_recache_protection = classmethod(set_recache_protection)
|
||||
|
||||
def flush_cached_instance(cls, instance, force=True):
|
||||
"""
|
||||
|
|
@ -263,7 +263,7 @@ class SharedMemoryModel(Model):
|
|||
cls.__instance_cache__ = {}
|
||||
else:
|
||||
cls.__instance_cache__ = dict((key, obj) for key, obj in cls.__instance_cache__.items()
|
||||
if not obj._idmapper_cache_flush_safe)
|
||||
if obj._idmapper_recache_protection)
|
||||
flush_instance_cache = classmethod(flush_instance_cache)
|
||||
|
||||
def save(cls, *args, **kwargs):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue