diff options
-rw-r--r-- | askbot/deps/livesettings/models.py | 58 | ||||
-rw-r--r-- | askbot/deps/livesettings/overrides.py | 4 | ||||
-rw-r--r-- | askbot/management/commands/preload_livesettings_cache.py | 12 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py | 3 | ||||
-rw-r--r-- | askbot/setup_templates/settings.py.mustache | 2 |
5 files changed, 58 insertions, 21 deletions
diff --git a/askbot/deps/livesettings/models.py b/askbot/deps/livesettings/models.py index 1a57dfc5..71db8acf 100644 --- a/askbot/deps/livesettings/models.py +++ b/askbot/deps/livesettings/models.py @@ -25,14 +25,20 @@ 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: + + grp = overrides.get(group, None) + + if grp and key in grp: + val = grp[key] + setting = ImmutableSetting(key=key, group=group, value=val) + log.debug('Returning overridden: %s', setting) + elif use_db: try: setting = cache_get(ck) @@ -45,10 +51,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 +63,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 +83,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 +126,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) + cache_set(key, value=val, length=length) + class Meta: unique_together = ('site', 'group', 'key') @@ -149,7 +162,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 +177,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) + cache_set(key, value=val, length=length) + class Meta: unique_together = ('site', 'group', 'key') - + diff --git a/askbot/deps/livesettings/overrides.py b/askbot/deps/livesettings/overrides.py index f3dc3355..c2fc09df 100644 --- a/askbot/deps/livesettings/overrides.py +++ b/askbot/deps/livesettings/overrides.py @@ -35,7 +35,7 @@ def get_overrides(siteid=-1): } } - In the settings dict above, the "val" entries must exactly match the format + In the settings dict above, the "val" entries must exactly match the format stored in the database for a setting. Do not use a literal True or an integer, it needs to be the string representation of them. @@ -45,7 +45,7 @@ def get_overrides(siteid=-1): if hasattr(djangosettings, 'LIVESETTINGS_OPTIONS'): if siteid == -1: siteid = _safe_get_siteid(None) - + opts = djangosettings.LIVESETTINGS_OPTIONS if opts.has_key(siteid): opts = opts[siteid] diff --git a/askbot/management/commands/preload_livesettings_cache.py b/askbot/management/commands/preload_livesettings_cache.py new file mode 100644 index 00000000..1898fc6a --- /dev/null +++ b/askbot/management/commands/preload_livesettings_cache.py @@ -0,0 +1,12 @@ +from django.core.management.base import NoArgsCommand + +class Command(NoArgsCommand): + '''Loads livesettings values to cache helping speed up + initial load time for the users''' + + def handle_noargs(self, **options): + from askbot.conf import settings + #Just loads all the settings that way they will be in the cache + for key, value in settings._ConfigSettings__instance.items(): + empty1 = getattr(settings, key) + print 'cache pre-loaded' diff --git a/askbot/setup_templates/settings.py b/askbot/setup_templates/settings.py index 32af9920..632c4e70 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 |