Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] memcache for GAE migration #74

Merged
merged 5 commits into from
Aug 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,28 @@
$ 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 fully migrate to GAE yet)

1. setup GAE SDK

- Install [GAE Python SDK](https://cloud.google.com/appengine/downloads)

2. 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
10 changes: 8 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ def googlemap_api_key(self):

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

cache = cache.Redis(**cfg.section('REDIS_INFO'))
backend = os.getenv('CACHE_BACKEND', 'Redis')
if backend == 'Redis':
cache = cache.Redis(**cfg.section('REDIS_INFO'))
elif backend == 'Memcached':
cache = cache.Memcached(**cfg.section('MEMCACHED'))
else:
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
16 changes: 16 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
application: confweb
version: 1
runtime: python27
threadsafe: yes
api_version: 1

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

libraries:
- name: jinja2
version: latest

env_variables:
CACHE_BACKEND: 'Memcached'
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 = ''
33 changes: 33 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,31 @@ def get(self, key):
return None

return pickle.loads(thing)

class Memcached:
def __init__(self, servers=[], debug=0):
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/') or os.getenv('SERVER_SOFTWARE', '').startswith('Development/'):
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python で

Memcache(servers=nil)

Memcache(servers=[])

の両方とも

if not servers:

で捕まえられましたっけ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nilはNoneのことですかね?でしたらどちらでも捕まえられます。
Stack Overflow - Best way to check if a list is empty

実際に試したところ、全部

Exception: servers missing in memcache settings

を吐きました。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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

def set(self, key, val, expires=0):
self.client .set(key, val, expires)

def get(self, key):
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
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.