Skip to content

Commit

Permalink
Switch between Redis and Memcached via an environment variable #37
Browse files Browse the repository at this point in the history
  • Loading branch information
richardimaoka committed Aug 14, 2016
1 parent 8d1c848 commit a6e6c5c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 44 deletions.
33 changes: 4 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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',
Expand Down
5 changes: 4 additions & 1 deletion app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ handlers:

libraries:
- name: jinja2
version: latest
version: latest

env_variables:
CACHE_BACKEND: 'Memcached'
8 changes: 2 additions & 6 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

9 changes: 9 additions & 0 deletions config.json.default
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
"port": 6379,
"db": 0
},
"MEMCACHED": {
"servers" : [
{
"host": "localhost",
"port": "11211"
}
],
"debug": 0
},
"OCTAV": {
"endpoint": "",
"key": "",
Expand Down

0 comments on commit a6e6c5c

Please sign in to comment.