def set(self, key, value, expire=_DEFAULT_TTL, namespace=None): from kay.ext.live_settings.models import KayLiveSetting old_namespace = namespace_manager.get_namespace() try: if namespace is not None: namespace_manager.set_namespace(namespace) new_setting = KayLiveSetting( key_name=key, ttl=expire, value=value, ) new_setting.put() # Set the memcached key to never expire. It only expires # if it is evicted from memory. TTLs are handled by the # in-memory cache. memcache.set("kay:live:%s" % key, (value, expire)) self._set_local_cache(key, value, ttl=expire, namespace=namespace) finally: if namespace is not None: namespace_manager.set_namespace(old_namespace) return new_setting
def set_multi(self, data, expire=_DEFAULT_TTL, namespace=None): from kay.ext.live_settings.models import KayLiveSetting old_namespace = namespace_manager.get_namespace() try: if namespace is not None: namespace_manager.set_namespace(namespace) data_items = data.items() db.put( map( lambda x: KayLiveSetting( key_name=x[0], ttl=expire, value=x[1]), data_items)) memcache.set_multi( dict( map(lambda x: ("kay:live:%s" % x[0], (x[1], expire)), data_items))) for key, value in data_items: self._set_local_cache(key, value, ttl=expire, namespace=namespace) finally: if namespace is not None: namespace_manager.set_namespace(old_namespace)
def admin(request): nsform = None namespace = '' if 'namespace' in request.args: nsform = KayNamespaceForm() if nsform.validate(request.args): namespace = nsform['namespace'] or '' nsform = nsform.as_widget() object_list = list(KayLiveSetting.all(namespace=namespace)) forms = dict( map(lambda s: (s.key().name(), KayLiveSettingForm(instance=s, initial={ "key_name": s.key().name(), "namespace": namespace, })), object_list) ) if (request.method == "POST"): key_name = request.form.get('key_name') if key_name: form = forms.get(key_name, None) if not form: form = KayLiveSettingForm() if form.validate(request.form): if 'delete' in request.form: _set_flash_msg(request, _("Deleted the setting '%(key)s'" % { 'key': key_name, })) live_settings.delete(form['key_name'], namespace=namespace) if key_name in forms: del forms[key_name] else: _set_flash_msg(request, _("Updated the setting '%(key)s'" % { 'key': key_name, })) forms[key_name] = form form.instance = live_settings.set( form['key_name'], form['value'], namespace=namespace, ) return redirect( "%s?%s" % ( url_for('live_settings/admin'), url_encode(request.args), ), ) new_form = KayLiveSettingForm() return render_to_response('live_settings/admin.html', { 'flash_msg': _get_flash_msg(request), 'to_local_timezone': to_local_timezone, # ensure we have this function 'nsform': nsform, 'namespace': namespace, 'form_list': map(lambda f: (f.instance, f.as_widget()), forms.values()), 'new_form': new_form.as_widget(), })
def keys(self, namespace=None): from kay.ext.live_settings.models import KayLiveSetting old_namespace = namespace_manager.get_namespace() try: if namespace is not None: namespace_manager.set_namespace(namespace) return map(lambda e: e.key().name(), KayLiveSetting.all()) finally: if namespace is not None: namespace_manager.set_namespace(old_namespace)
def delete(self, key, namespace=None): from kay.ext.live_settings.models import KayLiveSetting old_namespace = namespace_manager.get_namespace() try: if namespace is not None: namespace_manager.set_namespace(namespace) setting = KayLiveSetting.get_by_key_name(key) if setting: setting.delete() memcache.delete("kay:live:%s" % key) self._del_local_cache(key, namespace=namespace) finally: if namespace is not None: namespace_manager.set_namespace(old_namespace)
def get(self, key, default=None, namespace=None): from kay.ext.live_settings.models import KayLiveSetting old_namespace = namespace_manager.get_namespace() try: if namespace is not None: namespace_manager.set_namespace(namespace) set_dictcache = False set_memcache = False value = self._get_local_cache(key, namespace=namespace) expire = _missing ttl = _missing if value is _missing: set_dictcache = True value = memcache.get("kay:live:%s" % key) if value: value, ttl = value else: value, ttl = (_missing, _missing) if value is _missing: set_memcache = True entity = KayLiveSetting.get_by_key_name(key) if entity: value = entity.value or _missing ttl = entity.ttl or _missing if value is _missing: return default else: if ttl is None or ttl is _missing: ttl = _DEFAULT_TTL if set_dictcache: self._set_local_cache(key, value, ttl=ttl, namespace=namespace) if set_memcache: memcache.set("kay:live:%s" % key, (value, ttl)) return value finally: if namespace is not None: namespace_manager.set_namespace(old_namespace)
def get(self, key, default=None, namespace=None): from kay.ext.live_settings.models import KayLiveSetting old_namespace = namespace_manager.get_namespace() try: if namespace is not None: namespace_manager.set_namespace(namespace) set_dictcache = False set_memcache = False value = self._get_local_cache(key, namespace=namespace) expire = _missing ttl = _missing if value is _missing: set_dictcache = True value = memcache.get("kay:live:%s" % key) if value: value,ttl = value else: value,ttl = (_missing, _missing) if value is _missing: set_memcache = True entity = KayLiveSetting.get_by_key_name(key) if entity: value = entity.value or _missing ttl = entity.ttl or _missing if value is _missing: return default else: if ttl is None or ttl is _missing: ttl = _DEFAULT_TTL if set_dictcache: self._set_local_cache(key, value, ttl=ttl, namespace=namespace) if set_memcache: memcache.set("kay:live:%s" % key, (value, ttl)) return value finally: if namespace is not None: namespace_manager.set_namespace(old_namespace)
def admin(request): nsform = None namespace = '' if 'namespace' in request.args: nsform = KayNamespaceForm() if nsform.validate(request.args): namespace = nsform['namespace'] or '' nsform = nsform.as_widget() object_list = list(KayLiveSetting.all(namespace=namespace)) forms = dict( map( lambda s: (s.key().name(), KayLiveSettingForm(instance=s, initial={ "key_name": s.key().name(), "namespace": namespace, })), object_list)) if (request.method == "POST"): key_name = request.form.get('key_name') if key_name: form = forms.get(key_name, None) if not form: form = KayLiveSettingForm() if form.validate(request.form): if 'delete' in request.form: _set_flash_msg( request, _("Deleted the setting '%(key)s'" % { 'key': key_name, })) live_settings.delete(form['key_name'], namespace=namespace) if key_name in forms: del forms[key_name] else: _set_flash_msg( request, _("Updated the setting '%(key)s'" % { 'key': key_name, })) forms[key_name] = form form.instance = live_settings.set( form['key_name'], form['value'], namespace=namespace, ) return redirect( "%s?%s" % ( url_for('live_settings/admin'), url_encode(request.args), ), ) new_form = KayLiveSettingForm() return render_to_response( 'live_settings/admin.html', { 'flash_msg': _get_flash_msg(request), 'to_local_timezone': to_local_timezone, # ensure we have this function 'nsform': nsform, 'namespace': namespace, 'form_list': map(lambda f: (f.instance, f.as_widget()), forms.values()), 'new_form': new_form.as_widget(), })