What is a good place to store configuration in Google AppEngine (python)

前端 未结 3 1871
死守一世寂寞
死守一世寂寞 2020-12-15 08:00

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

Should I make a single bigtable enti

3条回答
  •  北海茫月
    2020-12-15 08:54

    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
    

提交回复
热议问题