Skip to content

Commit

Permalink
Memcached channelcloud and made overall optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrik Berggren committed Dec 11, 2008
1 parent 157382d commit 0f6a352
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app.yaml
@@ -1,5 +1,5 @@
application: onelinr
version: 3
version: 4
runtime: python
api_version: 1

Expand Down
80 changes: 47 additions & 33 deletions onelinr.py
Expand Up @@ -9,12 +9,14 @@
import template

from google.appengine.api import users

import re
from google.appengine.ext.webapp.util import run_wsgi_app
from django.conf import settings
import textile

from google.appengine.api import memcache

from django.utils import simplejson

settings.INSTALLED_APPS = ('django.contrib.markup', 'onelinr')

SKIP_LIST = ["favicon.ico","robots.txt", "feed"]
Expand All @@ -41,11 +43,15 @@ class Post(db.Model):
class StartPage(webapp.RequestHandler):

def get(self):
channels = Channel.all()
channels.filter("post_count >", 0)
channels.order("-post_count")
channelCloud = renderChannelCloud(channels);
self.response.out.write(template.render('index.html', {'channels':channels,'channelCloud':channelCloud,'page_title':'Onelinr'}))
channelCloud = memcache.get("channelCloud")
if channelCloud is None:
channels = Channel.all()
channels.filter("post_count >", 0)
channels.order("-post_count")
channelCloud = renderChannelCloud(channels);
memcache.set("channelCloud", channelCloud, 86400)

self.response.out.write(template.render('index.html', {'channelCloud':channelCloud,'page_title':'Onelinr'}))

class ChannelPage(webapp.RequestHandler):

Expand Down Expand Up @@ -79,8 +85,8 @@ def post(self):

channel_key = self.request.get('key')
channel = db.get(channel_key)

q = db.GqlQuery("SELECT * FROM Post WHERE belongs_to = :channel ORDER BY post_id DESC", channel=channel)

last_post = q.get()

if last_post:
Expand All @@ -96,16 +102,19 @@ def post(self):
handle = user.handle
else:
handle = ""
post = db.get(post.put())

channel.post_count += 1

# update post_count
if post:
channel = db.get(channel_key)
logging.info(channel.post_count)
channel.post_count = channel.post_count + 1
channel.put()

self.response.out.write("{'post_id':"+str(post.post_id)+",'text':'"+re.escape(textile.textile(post.text))+"', 'handle':'"+re.escape(handle)+"'}")
batch = [post, channel]
db.put(batch)

d = {
'post_id':post.post_id,
'text':textile.textile(post.text),
'handle':handle,
}

self.response.out.write(simplejson.dumps(d))

class HandlePage(webapp.RequestHandler):

Expand Down Expand Up @@ -162,15 +171,15 @@ def get(self):
channel.put()

q = db.GqlQuery("SELECT * FROM Post WHERE belongs_to = :channel ORDER BY post_id DESC", channel=channel)
posts = q.fetch(100) #100 latest is sufficient
posts = q.fetch(20) #20 latest is sufficient

self.response.out.write(template.render('channel_feed.html', {'channel':channel, 'posts':posts}))

class Feed(webapp.RequestHandler):
def get(self):

q = db.GqlQuery("SELECT * FROM Post ORDER BY date_posted DESC")
posts = q.fetch(100) #100 latest is sufficient
posts = q.fetch(20) #20 latest is sufficient

self.response.out.write(template.render('feed.html', {'posts':posts}))

Expand All @@ -179,29 +188,34 @@ def get(self):
name = url_to_channel_name(self.request.uri)
get_from = self.request.get('from_id')

q = db.GqlQuery("SELECT * FROM Channel WHERE name = :name", name=name)
channel = q.get()
channel = memcache.get(name)
if channel is None:
q = db.GqlQuery("SELECT * FROM Channel WHERE name = :name", name=name)
channel = q.get()
memcache.set(name, channel)

q = db.GqlQuery("SELECT * FROM Post WHERE belongs_to = :channel AND post_id > :get_from ORDER BY post_id DESC", channel=channel, get_from=int(get_from))
posts = q.fetch(100)
posts = q.fetch(50)

# Own JSON formatting
posts_json = "["
idx = 1
d = {}
for post in posts:
if post.posted_by:
handle = post.posted_by.handle
else:
handle = ""

posts_json += "{'post_id':"+str(post.post_id)+",'text':'"+re.escape(textile.textile(post.text))+"', 'handle':'"+re.escape(handle)+"'}"
if idx != len(posts):
posts_json += ","
idx += 1
posts_json += "]"

self.response.out.write(posts_json)

d.update({
'post_id':post.post_id,
'text':textile.textile(post.text),
'handle':handle,
})

if len(d) > 0:
tmp = [d]
self.response.out.write(simplejson.dumps(tmp))
else:
self.response.out.write("[]")

def main():
application = webapp.WSGIApplication([('/', StartPage),
('/.*/handle', HandlePage),
Expand Down

0 comments on commit 0f6a352

Please sign in to comment.