]> git.openstreetmap.org Git - osqa.git/commitdiff
Reduces the amount of connections to the cache server.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Fri, 4 Jun 2010 12:21:53 +0000 (12:21 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Fri, 4 Jun 2010 12:21:53 +0000 (12:21 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@374 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/settings/base.py

index 398ed11907d7b33c214d168eb5036ffa2900ef40..75687be8b7f662125a45f11f6d601c97da2fdde8 100644 (file)
@@ -1,5 +1,8 @@
 import django.dispatch
 from django.utils.encoding import force_unicode
+from datetime import datetime, timedelta
+
+TMP_MINICACHE_SECONDS = 5
 
 class SettingSet(list):
     def __init__(self, name, title, description, weight=1000, markdown=False):
@@ -8,7 +11,7 @@ class SettingSet(list):
         self.description = description
         self.weight = weight
         self.markdown = markdown
-        
+
 
 class BaseSetting(object):
     @classmethod
@@ -23,6 +26,8 @@ class BaseSetting(object):
         self.default = default
         self.field_context = field_context or {}
 
+        self._temp = None
+
         if set is not None:
             if not set.name in Setting.sets:
                 Setting.sets[set.name] = set
@@ -37,21 +42,28 @@ class BaseSetting(object):
 
     @property
     def value(self):
+        if self._temp:
+            v, exp = self._temp
+            if exp + timedelta(seconds=TMP_MINICACHE_SECONDS) > datetime.now():
+                return v
+
         from forum.models import KeyValue
 
         try:
             kv = KeyValue.objects.get(key=self.name)
-        except:
-            kv = KeyValue(key=self.name, value=self._parse(self.default))
-            kv.save()
-
-        return kv.value
+            v = kv.value
+            self._temp = (v, datetime.now() + timedelta(seconds=TMP_MINICACHE_SECONDS))
+            return v
+        except Exception, e:
+                return self.default
 
     def set_value(self, new_value):
         new_value = self._parse(new_value)
         self.save(new_value)
 
     def save(self, value):
+        self._temp = None
+        
         from forum.models import KeyValue
 
         try: