diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-05-09 15:07:33 -0600 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-05-09 15:07:33 -0600 |
commit | 130bbd2c6b3f8133d13151da935cc22e0ced38a7 (patch) | |
tree | 0440ff9b642c2e2f0b5453937bc546bebfca1d0b | |
parent | fe88a995f4b096d952df56026a04bb9b148e03ba (diff) | |
download | askbot-130bbd2c6b3f8133d13151da935cc22e0ced38a7.tar.gz askbot-130bbd2c6b3f8133d13151da935cc22e0ced38a7.tar.bz2 askbot-130bbd2c6b3f8133d13151da935cc22e0ced38a7.zip |
Added LIVESETTINGS_CACHE_TIMEOUT settings to set a different timeout for
livesettings values.
-rw-r--r-- | askbot/deps/livesettings/models.py | 50 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py | 3 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py.mustache | 2 |
3 files changed, 37 insertions, 18 deletions
diff --git a/askbot/deps/livesettings/models.py b/askbot/deps/livesettings/models.py index 1a57dfc5..1b49cf88 100644 --- a/askbot/deps/livesettings/models.py +++ b/askbot/deps/livesettings/models.py @@ -25,13 +25,13 @@ def _safe_get_siteid(site): def find_setting(group, key, site=None): """Get a setting or longsetting by group and key, cache and return it.""" - + siteid = _safe_get_siteid(site) setting = None - + use_db, overrides = get_overrides(siteid) ck = cache_key('Setting', siteid, group, key) - + if use_db: try: setting = cache_get(ck) @@ -45,10 +45,10 @@ def find_setting(group, key, site=None): # maybe it is a "long setting" try: setting = LongSetting.objects.get(site__id__exact=siteid, key__exact=key, group__exact=group) - + except LongSetting.DoesNotExist: pass - + cache_set(ck, value=setting) else: @@ -57,13 +57,13 @@ def find_setting(group, key, site=None): val = grp[key] setting = ImmutableSetting(key=key, group=group, value=val) log.debug('Returning overridden: %s', setting) - + if not setting: raise SettingNotSet(key, cachekey=ck) return setting -class SettingNotSet(Exception): +class SettingNotSet(Exception): def __init__(self, k, cachekey=None): self.key = k self.cachekey = cachekey @@ -77,22 +77,22 @@ class SettingManager(models.Manager): class ImmutableSetting(object): - + def __init__(self, group="", key="", value="", site=1): self.site = site self.group = group self.key = key self.value = value - + def cache_key(self, *args, **kwargs): return cache_key('OverrideSetting', self.site, self.group, self.key) - + def delete(self): pass - + def save(self, *args, **kwargs): pass - + def __repr__(self): return "ImmutableSetting: %s.%s=%s" % (self.group, self.key, self.value) @@ -120,11 +120,18 @@ class Setting(models.Model, CachedObjectMixin): site = self.site except Site.DoesNotExist: self.site = Site.objects.get_current() - + super(Setting, self).save(force_insert=force_insert, force_update=force_update) - + self.cache_set() - + + def cache_set(self, *args, **kwargs): + val = kwargs.pop('value', self) + key = self.cache_key(*args, **kwargs) + #TODO: fix this with Django's > 1.3 CACHE dict setting support + length = getattr(settings, 'LIVESETTINGS_CACHE_TIMEOUT', settings.CACHE_TIMEOUT) + keyedcache.cache_set(key, value=val, length=length) + class Meta: unique_together = ('site', 'group', 'key') @@ -149,7 +156,7 @@ class LongSetting(models.Model, CachedObjectMixin): def cache_key(self, *args, **kwargs): # note same cache pattern as Setting. This is so we can look up in one check. - # they can't overlap anyway, so this is moderately safe. At the worst, the + # they can't overlap anyway, so this is moderately safe. At the worst, the # Setting will override a LongSetting. return cache_key('Setting', self.site, self.group, self.key) @@ -164,7 +171,14 @@ class LongSetting(models.Model, CachedObjectMixin): self.site = Site.objects.get_current() super(LongSetting, self).save(force_insert=force_insert, force_update=force_update) self.cache_set() - + + def cache_set(self, *args, **kwargs): + val = kwargs.pop('value', self) + key = self.cache_key(*args, **kwargs) + #TODO: fix this with Django's > 1.3 CACHE dict setting support + length = getattr(settings, 'LIVESETTINGS_CACHE_TIMEOUT', settings.CACHE_TIMEOUT) + keyedcache.cache_set(key, value=val, length=length) + class Meta: unique_together = ('site', 'group', 'key') - + diff --git a/askbot/setup_templates/settings.py b/askbot/setup_templates/settings.py index b326ea85..104ea64b 100644 --- a/askbot/setup_templates/settings.py +++ b/askbot/setup_templates/settings.py @@ -181,6 +181,8 @@ INSTALLED_APPS = ( CACHE_BACKEND = 'locmem://' #needed for django-keyedcache CACHE_TIMEOUT = 6000 +#sets a special timeout for livesettings if you want to make them different +LIVESETTINGS_CACHE_TIMEOUT = CACHE_TIMEOUT CACHE_PREFIX = 'askbot' #make this unique CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True #If you use memcache you may want to uncomment the following line to enable memcached based sessions @@ -229,3 +231,4 @@ CSRF_COOKIE_NAME = 'askbot_csrf' STATICFILES_DIRS = ( os.path.join(ASKBOT_ROOT, 'skins'),) RECAPTCHA_USE_SSL = True + diff --git a/askbot/setup_templates/settings.py.mustache b/askbot/setup_templates/settings.py.mustache index eb1cb1c1..e29f1dca 100644 --- a/askbot/setup_templates/settings.py.mustache +++ b/askbot/setup_templates/settings.py.mustache @@ -180,6 +180,8 @@ INSTALLED_APPS = ( CACHE_BACKEND = 'locmem://' #needed for django-keyedcache CACHE_TIMEOUT = 6000 +#sets a special timeout for livesettings if you want to make them different +LIVESETTINGS_CACHE_TIMEOUT = CACHE_TIMEOUT CACHE_PREFIX = 'askbot' #make this unique CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True #If you use memcache you may want to uncomment the following line to enable memcached based sessions |