From a6e6c5c7d46c41d578da5ed082269611b262917e Mon Sep 17 00:00:00 2001 From: "Richard S. Imaoka" Date: Sun, 14 Aug 2016 16:56:13 +0900 Subject: [PATCH] Switch between Redis and Memcached via an environment variable #37 --- README.md | 33 ++++----------------------------- app.py | 15 +++++++-------- app.yaml | 5 ++++- cache.py | 8 ++------ config.json.default | 9 +++++++++ 5 files changed, 26 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index ccf83ce..a0d9aae 100644 --- a/README.md +++ b/README.md @@ -29,36 +29,11 @@ GAE doesn't support Redis. So you need to use Memcache instead. -1. setup +1. setup GAE SDK + + - Install [GAE Python SDK](https://cloud.google.com/appengine/downloads) - - Install [GAE Python SDK](https://cloud.google.com/appengine/downloads) - - -2. In config.json, - - Remove the "REDIS_INFO" section : - - ``` - "REDIS_INFO": { - ... - }, - ``` - - and add the "MEMCACHE" section like below - - ``` - "MEMCACHE": { - "servers" : [ - { - "host": "localhost", - "port": "11211" - } - ], - "debug": 0 - }, - ``` - -3. start GAE local dev server +2. start GAE local dev server do not forget the last dot (.) in the command diff --git a/app.py b/app.py index 9a64c29..bd53bd4 100644 --- a/app.py +++ b/app.py @@ -38,15 +38,13 @@ def __init__(self, file): with open(file, 'r') as f: self.cfg = json.load(f) - for section in ['OCTAV', 'GITHUB', 'GOOGLE_MAP']: + for section in ['OCTAV', 'GITHUB', 'REDIS_INFO', 'GOOGLE_MAP']: if not self.cfg.get(section): raise Exception( "missing section '" + section + "' in config file '" + file + "'" ) if self.cfg.get('OCTAV').get('BASE_URI'): raise Exception( 'DEPRECATED: {"OCTAV":{"BASE_URI"}} in config.json is deprecated.\ Please use {"OCTAV":{"endpoint"}} instead and remove {"OCTAV":{"BASE_URI"}}.') - if self.cfg.get('REDIS_INFO') and self.cfg.get('MEMCACHE'): - raise Exception( 'In config.json, do not specify both "REDIS_INFO" and "MEMCACHE". Use only either of them.' ) def section(self, name): @@ -70,14 +68,15 @@ def googlemap_api_key(self): octav = Octav(**cfg.section('OCTAV')) -if cfg.section('REDIS_INFO'): +backend = os.getenv('CACHE_BACKEND', 'Redis') +if backend == 'Redis': cache = cache.Redis(**cfg.section('REDIS_INFO')) -elif cfg.section('MEMCACHE'): - cache = cache.Memcache(**cfg.section('MEMCACHE')) +elif backend == 'Memcached': + cache = cache.Memcached(**cfg.section('MEMCACHED')) else: - raise Exception( 'config.json must specify either of "REDIS_INFO" or "MEMCACHE"' ) + raise Exception('Unknown backend "%s"' % backend) -twitter = oauth.Init('twitter', +twitter = oauth.Init('twitter', base_url='https://api.twitter.com/1.1/', request_token_url='https://api.twitter.com/oauth/request_token', access_token_url='https://api.twitter.com/oauth/access_token', diff --git a/app.yaml b/app.yaml index 42ad914..50b122d 100644 --- a/app.yaml +++ b/app.yaml @@ -10,4 +10,7 @@ handlers: libraries: - name: jinja2 - version: latest \ No newline at end of file + version: latest + +env_variables: + CACHE_BACKEND: 'Memcached' \ No newline at end of file diff --git a/cache.py b/cache.py index ccfb393..f556ff8 100644 --- a/cache.py +++ b/cache.py @@ -22,7 +22,7 @@ def get(self, key): return pickle.loads(thing) -class Memcache: +class Memcached: def __init__(self, servers=[], debug=0): if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/') or os.getenv('SERVER_SOFTWARE', '').startswith('Development/'): print "GAE memcache used" @@ -49,9 +49,5 @@ def set(self, key, val, expires=0): self.client .set(key, val, expires) def get(self, key): - thing = self.client .get(key) - if not thing: - return None - - return thing + return self.client.get(key) diff --git a/config.json.default b/config.json.default index 4aea51c..1fde8ae 100644 --- a/config.json.default +++ b/config.json.default @@ -8,6 +8,15 @@ "port": 6379, "db": 0 }, + "MEMCACHED": { + "servers" : [ + { + "host": "localhost", + "port": "11211" + } + ], + "debug": 0 + }, "OCTAV": { "endpoint": "", "key": "",