Added full caching to all attributes on a per-object level. This speeds up attribute loading considerably.

This commit is contained in:
Griatch 2012-04-26 21:39:16 +02:00
parent 9f9dadd4eb
commit 9d970ea7c5

View file

@ -1213,14 +1213,21 @@ class TypedObject(SharedMemoryModel):
# Helper methods for persistent attributes # Helper methods for persistent attributes
_attribute_cache = {}
def has_attribute(self, attribute_name): def has_attribute(self, attribute_name):
""" """
See if we have an attribute set on the object. See if we have an attribute set on the object.
attribute_name: (str) The attribute's name. attribute_name: (str) The attribute's name.
""" """
return _GA(self, "_attribute_class").objects.filter(db_obj=self).filter( if attribute_name not in _GA(self, "_attribute_cache"):
db_key__iexact=attribute_name).count() attrib_obj = _GA(self, "_attribute_class").objects.filter(db_obj=self).filter(
db_key__iexact=attribute_name)
if attrib_obj:
_GA(self, "_attribute_cache")[attribute_name] = attrib_obj[0]
else:
return False
return True
def set_attribute(self, attribute_name, new_value=None): def set_attribute(self, attribute_name, new_value=None):
""" """
@ -1231,17 +1238,21 @@ class TypedObject(SharedMemoryModel):
new_value: (python obj) The value to set the attribute to. If this is not new_value: (python obj) The value to set the attribute to. If this is not
a str, the object will be stored as a pickle. a str, the object will be stored as a pickle.
""" """
attrib_obj = None attrib_obj = _GA(self, "_attribute_cache").get("attribute_name")
if not attrib_obj:
attrclass = _GA(self, "_attribute_class") attrclass = _GA(self, "_attribute_class")
try: # check if attribute already exists.
# use old attribute
attrib_obj = attrclass.objects.filter( attrib_obj = attrclass.objects.filter(
db_obj=self).filter(db_key__iexact=attribute_name)[0] db_obj=self).filter(db_key__iexact=attribute_name)
except IndexError: if attrib_obj:
# use old attribute
attrib_obj = attrib_obj[0]
else:
# no match; create new attribute # no match; create new attribute
attrib_obj = attrclass(db_key=attribute_name, db_obj=self) attrib_obj = attrclass(db_key=attribute_name, db_obj=self)
# re-set an old attribute value # re-set an old attribute value
attrib_obj.value = new_value attrib_obj.value = new_value
_GA(self,"_attribute_cache")[attribute_name] = attrib_obj
def get_attribute(self, attribute_name, default=None): def get_attribute(self, attribute_name, default=None):
""" """
@ -1252,12 +1263,14 @@ class TypedObject(SharedMemoryModel):
attribute_name: (str) The attribute's name. attribute_name: (str) The attribute's name.
default: What to return if no attribute is found default: What to return if no attribute is found
""" """
attrib_obj = default attrib_obj = _GA(self,"_attribute_cache").get(attribute_name)
try: if not attrib_obj:
attrib_obj = _GA(self, "_attribute_class").objects.filter( attrib_obj = _GA(self, "_attribute_class").objects.filter(
db_obj=self).filter(db_key__iexact=attribute_name)[0] db_obj=self).filter(db_key__iexact=attribute_name)
except IndexError: if not attrib_obj:
return default return default
_GA(self,"_attribute_cache")[attribute_name] = attrib_obj[0] #query is first evaluated here
return _GA(self, "_attribute_cache")[attribute_name].value
return attrib_obj.value return attrib_obj.value
def get_attribute_raise(self, attribute_name): def get_attribute_raise(self, attribute_name):
@ -1267,11 +1280,15 @@ class TypedObject(SharedMemoryModel):
attribute_name: (str) The attribute's name. attribute_name: (str) The attribute's name.
""" """
try: attrib_obj = _GA(self, "_attribute_cache.get")(attribute_name)
return _GA(self, "_attribute_class").objects.filter( if not attrib_obj:
db_obj=self).filter(db_key__iexact=attribute_name)[0].value attrib_obj = _GA(self, "_attribute_class").objects.filter(
except IndexError: db_obj=self).filter(db_key__iexact=attribute_name)
if not attrib_obj:
raise AttributeError raise AttributeError
_GA(self, "_attribute_cache")[attribute_name] = attrib_obj[0] #query is first evaluated here
return _GA(self, "_attribute_cache")[attribute_name].value
return attrib_obj.value
def del_attribute(self, attribute_name): def del_attribute(self, attribute_name):
""" """
@ -1279,6 +1296,11 @@ class TypedObject(SharedMemoryModel):
attribute_name: (str) The attribute's name. attribute_name: (str) The attribute's name.
""" """
attr_obj = _GA(self, "_attribute_cache").get(attribute_name)
if attr_obj:
del _GA(self, "_attribute_cache")[attribute_name]
attr_obj.delete()
else:
try: try:
_GA(self, "_attribute_class").objects.filter( _GA(self, "_attribute_class").objects.filter(
db_obj=self).filter(db_key__iexact=attribute_name)[0].delete() db_obj=self).filter(db_key__iexact=attribute_name)[0].delete()
@ -1292,10 +1314,16 @@ class TypedObject(SharedMemoryModel):
attribute_name: (str) The attribute's name. attribute_name: (str) The attribute's name.
""" """
attr_obj = _GA(self, "_attribute_cache").get(attribute_name)
if attr_obj:
del _GA(self, "_attribute_cache")[attribute_name]
attr_obj.delete()
else:
try: try:
_GA(self, "_attribute_class").objects.filter( _GA(self, "_attribute_class").objects.filter(
db_obj=self).filter(db_key__iexact=attribute_name)[0].delete() db_obj=self).filter(db_key__iexact=attribute_name)[0].delete()
except IndexError: except IndexError:
pass
raise AttributeError raise AttributeError
def get_all_attributes(self): def get_all_attributes(self):