Skip to content

Commit

Permalink
memcache is working on local GAE dev server, and on vagrant
Browse files Browse the repository at this point in the history
  • Loading branch information
richardimaoka committed Aug 14, 2016
1 parent d5b0d47 commit abcc5cd
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/config.json
*.pyc
.idea
lib
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,55 @@
$ vagrant ssh
$ cd ~/vagrant
$ redis-server &
$ python3 app.wsgi
$ python app.py
```
4. access <http://127.0.0.1:3000/>
## How to run on GAE local dev server (work in progress, as we did not migrate to GAE yet)
GAE doesn't support Redis. So you need to use Memcache instead.
1. setup
- 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
do not forget the last dot (.) in the command
```
$ dev_appserver.py .
```
4. access <http://127.0.0.1:8080/>
## i18n/l10n
### Extract translatable strings from templates
Expand Down
15 changes: 11 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def __init__(self, file):
with open(file, 'r') as f:
self.cfg = json.load(f)

for section in ['OCTAV', 'REDIS_INFO', 'GITHUB', 'GOOGLE_MAP']:
for section in ['OCTAV', 'GITHUB', '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"}}.'
)
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):
return self.cfg.get(name)
Expand All @@ -68,7 +70,12 @@ def googlemap_api_key(self):

octav = Octav(**cfg.section('OCTAV'))

cache = cache.Redis(**cfg.section('REDIS_INFO'))
if cfg.section('REDIS_INFO'):
cache = cache.Redis(**cfg.section('REDIS_INFO'))
elif cfg.section('MEMCACHE'):
cache = cache.Memcache(**cfg.section('MEMCACHE'))
else:
raise Exception( 'config.json must specify either of "REDIS_INFO" or "MEMCACHE"' )

twitter = oauth.Init('twitter',
base_url='https://api.twitter.com/1.1/',
Expand Down
13 changes: 13 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
application: confweb
version: 1
runtime: python27
threadsafe: yes
api_version: 1

handlers:
- url: .*
script: app.app

libraries:
- name: jinja2
version: latest
14 changes: 12 additions & 2 deletions appengine_config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
import vendor
vendor.add('lib')
import os
import sys

from google.appengine.ext import vendor

vendor.add('lib')

# Fix for msvcrt import error https://github.com/gae-init/gae-init/pull/527
# Otherwise, GAE local dev server fails at "import msvcrt" in "click" package
if os.name == 'nt':
os.name = None
sys.platform = ''
39 changes: 39 additions & 0 deletions cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import pickle
import redis
import os
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/') or os.getenv('SERVER_SOFTWARE', '').startswith('Development/'):
from google.appengine.api import memcache
else:
import memcache

class Redis(object):
def __init__(self, host, port, db):
Expand All @@ -16,3 +21,37 @@ def get(self, key):
return None

return pickle.loads(thing)

class Memcache:
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"
self.client = memcache
else:
def server_str(server):
host = server.get('host')
port = server.get('port')
if not host:
raise Exception("host missing in memcache servers settings" )
elif not port:
raise Exception("port missing in memcache servers settings" )
else:
return str( host + ':' + port )

if not servers:
raise Exception("servers missing in memcache settings")
else:
server_settings = map( server_str, servers )
print "Normal memcache used"
self.client = memcache.Client(server_settings, debug)

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

4 changes: 4 additions & 0 deletions provision.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ubuntu-all:
ubuntu-root: \
ubuntu-apt \
ubuntu-redis \
ubuntu-memcached \
ubuntu-pip

ubuntu-user: \
Expand All @@ -75,6 +76,9 @@ ubuntu-apt:
ubuntu-redis:
apt-get install -y redis-server

ubuntu-memcached:
apt-get install -y memcached

ubuntu-pip:
apt-get install -y python-pip
pip install -r /vagrant/requirements.txt
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ flask_oauth
jinja2
py-gfm
redis
python-memcached
urllib3
wsgi-request-logger
werkzeug
71 changes: 0 additions & 71 deletions vendor.py

This file was deleted.

0 comments on commit abcc5cd

Please sign in to comment.