在 Google AppEngine (python) 中存储配置的好地方是什么 [英] What is a good place to store configuration in Google AppEngine (python)

查看:20
本文介绍了在 Google AppEngine (python) 中存储配置的好地方是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Google AppEngine 应用程序,但我怀疑我是否应该存储(敏感)配置数据,如凭据.

I am making a Google AppEngine application and am doubting were I should store (sensitive) configuration data like credentials.

我应该为配置创建一个单一的 bigtable 实体,还是有另一种建议的存储方式.

Should I make a single bigtable entity for configuration, or is there another advised way to store it.

推荐答案

如果您愿意将它们嵌入到您的源代码中,那么您可以这样做,但是如果您需要动态配置它,那么数据存储是一种方式去.您可以通过将它们缓存在本地内存中来避免获取每个请求的设置.这是一个帮助类:

If you're okay with embedding them in your source, you can do that, but if you need it to be dynamically configurable, the datastore is the way to go. You can avoid fetching the settings on every request by caching them in local memory. Here's a helper class for that:

class Configuration(db.Model):
  _INSTANCE = None

  @classmethod
  def get_instance(cls):
    if not cls._INSTANCE:
      cls._INSTANCE = cls.get_or_insert('config')
    return cls._INSTANCE

使用您需要的任何配置值简单地将其子类化(或修改类本身).由于加载的代码在请求之间持续存在,因此您只需为每个应用实例执行一次提取 - 但如果您希望能够动态更新配置,您可能需要建立超时.

Simply subclass this with whatever configuration values you need (or modify the class itself). Because loaded code persists between requests, you'll only have to do a single fetch per app instance - though if you want to be able to update the configuration dynamically, you may want to build in a timeout.

如果您想在有限的时间内缓存内容,最好的选择就是在获取时存储时间戳:

If you want to cache stuff for a limited time, your best option is simply storing the timestamp when you fetched it:

class Configuration(db.Model):
  CACHE_TIME = datetime.timedelta(minutes=5)

  _INSTANCE = None
  _INSTANCE_AGE = None

  @classmethod
  def get_instance(cls):
    now = datetime.datetime.now()
    if not cls._INSTANCE or cls._INSTANCE_AGE + cls.CACHE_TIME < now:
      cls._INSTANCE = cls.get_or_insert('config')
      cls._INSTANCE_AGE = now
    return cls._INSTANCE

这篇关于在 Google AppEngine (python) 中存储配置的好地方是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆