Skip to content

Commit

Permalink
used memcached http request to reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
avelino committed Aug 17, 2014
1 parent eca13b5 commit ebe2cb2
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import json
import requests
from google.appengine.api import memcache
from webapp2 import WSGIApplication, RequestHandler, cached_property
from webapp2_extras import jinja2
from jinja2 import Template
Expand Down Expand Up @@ -34,11 +35,15 @@ def get(self):
def file_exist(file):
return "/assets/images/logo/{}.png".format(file)

url = "https://api.github.com/repos/avelino/vim-bootstrap/contents"
url += "/vim_template/langs"
langs = [g["name"] for g in json.loads(requests.get(url).text)]
langs = memcache.get('langs')
if not langs:
url = "https://api.github.com/repos/avelino/vim-bootstrap/contents"
url += "/vim_template/langs"
langs = [g["name"] for g in json.loads(requests.get(url).text)]
memcache.add('langs', langs, 3600)

context = {'langs': langs, 'file_exist': file_exist}

self.render_response('index.html', **context)


Expand All @@ -48,12 +53,19 @@ def post(self):
url = "https://raw.githubusercontent.com/avelino/vim-bootstrap/master/"

langs = {"bundle": {}, "vim": {}}
print self.request.POST.getall('langs')
for l in self.request.POST.getall('langs'):
langs["bundle"][l] = requests.get(
"{0}vim_template/langs/{1}/{1}.bundle".format(url, l)).text
langs["vim"][l] = requests.get(
"{0}vim_template/langs/{1}/{1}.vim".format(url, l)).text
data = memcache.get('vim-{}'.format(l))
if not data:
langs["bundle"][l] = requests.get(
"{0}vim_template/langs/{1}/{1}.bundle".format(url, l)).text
langs["vim"][l] = requests.get(
"{0}vim_template/langs/{1}/{1}.vim".format(url, l)).text
memcache.add('vim-{}'.format(l),
{'vim': langs['vim'][l],
'bundle': langs['bundle'][l]}, 3600)
else:
langs["bundle"][l] = data['bundle']
langs["vim"][l] = data['vim']

template = Template(
requests.get("{}vim_template/vimrc".format(url)).text)
Expand Down

0 comments on commit ebe2cb2

Please sign in to comment.