From 130bbd2c6b3f8133d13151da935cc22e0ced38a7 Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Wed, 9 May 2012 15:07:33 -0600 Subject: Added LIVESETTINGS_CACHE_TIMEOUT settings to set a different timeout for livesettings values. --- askbot/deps/livesettings/models.py | 50 ++++++++++++++++++----------- askbot/setup_templates/settings.py | 3 ++ 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 -- cgit v1.2.3-1-g7c22 From c5d8de6ed81b519b998faeb0c17d6ed3cdae96b1 Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Wed, 9 May 2012 17:49:27 -0600 Subject: added management command to preload livesettings cache --- .../management/commands/preload_livesettings_cache.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 askbot/management/commands/preload_livesettings_cache.py diff --git a/askbot/management/commands/preload_livesettings_cache.py b/askbot/management/commands/preload_livesettings_cache.py new file mode 100644 index 00000000..431572c4 --- /dev/null +++ b/askbot/management/commands/preload_livesettings_cache.py @@ -0,0 +1,17 @@ +from django.core.management.base import NoArgsCommand +from askbot.conf import settings + +class Command(NoArgsCommand): + '''Loads livesettings values to cache helping speed up + initial load time for the users''' + + def handle_noargs(self, **options): + empty1 = emtpy2 = None + print 'loading cache' + #Just loads all the settings that way they will be in the cache + for key, value in settings._ConfigSettings__instance.items(): + empty1 = key + empty2 = value + print 'cache pre-loaded' + + -- cgit v1.2.3-1-g7c22 From 5747fa192aecc826a118c8be89c512d96076b42c Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Wed, 9 May 2012 17:56:49 -0600 Subject: fixed error --- askbot/deps/livesettings/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/askbot/deps/livesettings/models.py b/askbot/deps/livesettings/models.py index 1b49cf88..15392cb8 100644 --- a/askbot/deps/livesettings/models.py +++ b/askbot/deps/livesettings/models.py @@ -130,7 +130,7 @@ class Setting(models.Model, CachedObjectMixin): 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) + cache_set(key, value=val, length=length) class Meta: unique_together = ('site', 'group', 'key') @@ -177,7 +177,7 @@ class LongSetting(models.Model, CachedObjectMixin): 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) + cache_set(key, value=val, length=length) class Meta: unique_together = ('site', 'group', 'key') -- cgit v1.2.3-1-g7c22 From 32a4a5ddf93f2bc605706ecc3eafbc63dc923acf Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Thu, 10 May 2012 09:53:48 -0600 Subject: fixed preload_cache command --- askbot/management/commands/preload_livesettings_cache.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/askbot/management/commands/preload_livesettings_cache.py b/askbot/management/commands/preload_livesettings_cache.py index 431572c4..1898fc6a 100644 --- a/askbot/management/commands/preload_livesettings_cache.py +++ b/askbot/management/commands/preload_livesettings_cache.py @@ -1,17 +1,12 @@ from django.core.management.base import NoArgsCommand -from askbot.conf import settings class Command(NoArgsCommand): '''Loads livesettings values to cache helping speed up initial load time for the users''' def handle_noargs(self, **options): - empty1 = emtpy2 = None - print 'loading cache' + 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 = key - empty2 = value + empty1 = getattr(settings, key) print 'cache pre-loaded' - - -- cgit v1.2.3-1-g7c22 From e6f41d2beff84f403542bf3038d322a3e3eb544a Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Tue, 15 May 2012 15:26:13 -0600 Subject: added fix for livesettings override --- askbot/deps/livesettings/models.py | 14 +++++++++++++- askbot/deps/livesettings/overrides.py | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/askbot/deps/livesettings/models.py b/askbot/deps/livesettings/models.py index 15392cb8..13c2323b 100644 --- a/askbot/deps/livesettings/models.py +++ b/askbot/deps/livesettings/models.py @@ -32,7 +32,13 @@ def find_setting(group, key, site=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) @@ -125,6 +131,7 @@ class Setting(models.Model, CachedObjectMixin): self.cache_set() +<<<<<<< Updated upstream def cache_set(self, *args, **kwargs): val = kwargs.pop('value', self) key = self.cache_key(*args, **kwargs) @@ -132,6 +139,8 @@ class Setting(models.Model, CachedObjectMixin): length = getattr(settings, 'LIVESETTINGS_CACHE_TIMEOUT', settings.CACHE_TIMEOUT) cache_set(key, value=val, length=length) +======= +>>>>>>> Stashed changes class Meta: unique_together = ('site', 'group', 'key') @@ -172,6 +181,7 @@ class LongSetting(models.Model, CachedObjectMixin): super(LongSetting, self).save(force_insert=force_insert, force_update=force_update) self.cache_set() +<<<<<<< Updated upstream def cache_set(self, *args, **kwargs): val = kwargs.pop('value', self) key = self.cache_key(*args, **kwargs) @@ -179,6 +189,8 @@ class LongSetting(models.Model, CachedObjectMixin): length = getattr(settings, 'LIVESETTINGS_CACHE_TIMEOUT', settings.CACHE_TIMEOUT) cache_set(key, value=val, length=length) +======= +>>>>>>> Stashed changes 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] -- cgit v1.2.3-1-g7c22 From d466f674ad3878495ec0c395b47c2386d2e5f0f6 Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Tue, 15 May 2012 15:34:29 -0600 Subject: fixed merge :-$ --- askbot/deps/livesettings/models.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/askbot/deps/livesettings/models.py b/askbot/deps/livesettings/models.py index 13c2323b..71db8acf 100644 --- a/askbot/deps/livesettings/models.py +++ b/askbot/deps/livesettings/models.py @@ -131,7 +131,6 @@ class Setting(models.Model, CachedObjectMixin): self.cache_set() -<<<<<<< Updated upstream def cache_set(self, *args, **kwargs): val = kwargs.pop('value', self) key = self.cache_key(*args, **kwargs) @@ -139,8 +138,6 @@ class Setting(models.Model, CachedObjectMixin): length = getattr(settings, 'LIVESETTINGS_CACHE_TIMEOUT', settings.CACHE_TIMEOUT) cache_set(key, value=val, length=length) -======= ->>>>>>> Stashed changes class Meta: unique_together = ('site', 'group', 'key') @@ -181,7 +178,6 @@ class LongSetting(models.Model, CachedObjectMixin): super(LongSetting, self).save(force_insert=force_insert, force_update=force_update) self.cache_set() -<<<<<<< Updated upstream def cache_set(self, *args, **kwargs): val = kwargs.pop('value', self) key = self.cache_key(*args, **kwargs) @@ -189,8 +185,6 @@ class LongSetting(models.Model, CachedObjectMixin): length = getattr(settings, 'LIVESETTINGS_CACHE_TIMEOUT', settings.CACHE_TIMEOUT) cache_set(key, value=val, length=length) -======= ->>>>>>> Stashed changes class Meta: unique_together = ('site', 'group', 'key') -- cgit v1.2.3-1-g7c22