示例#1
0
  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
示例#2
0
    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)
示例#3
0
文件: views.py 项目: dbordak/pyblog
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(),
  })
示例#4
0
 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)
示例#5
0
  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)
示例#6
0
    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)
示例#7
0
文件: __init__.py 项目: gmist/kay-ru
  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)
示例#8
0
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(),
        })