I am writing a web application on google app engine in python using jinja2 as a templating system and the app basically allows users to write posts/comments and rank other user’s posts. The ranking system is based on the number of up/downvotes and the number of comments. I am trying to use memcache to store this calculated value and rank the posts accordingly.
I only want to store the value in the database occasionally so as to not make the write costs expensive. I am planning on having a counter and storing it in the database every 10 votes/comments. I was thinking something like this:
# I update the counter every time that I add a vote or comment
counter = 0
def posts_cache(update = False):
global counter
key = 'main'
posts = memcache.get(key)
if posts is None or update:
logging.error("DB QUERY")
posts = db.GqlQuery("SELECT * "
"FROM Post "
"ORDER BY rank DESC "
"LIMIT 100",
key)
posts = list(posts)
memcache.set(key, posts)
if counter>=10:
counter = 0
#put the memcache posts in the database
return posts
But I am not sure how to take the posts I have in memcache and store them in the database. Is there any way to do this in python? I’ve looked through the docs but haven’t found a clear way to do it.
1 Answer