On Mar 15, 2012, at 1:15 PM, Sushant Taneja wrote:
> I have a tags table which will be available to all logged in users. The table 
> has the following structure:
> 
> db.define_table('META_TAG',
>     Field('tag','string',notnull=True),
>     Field('root_tag','string',notnull=True),
>     Field('active_ind','string',length=1,default='Y')
> )
> 
> This table will be rarely (once a month) updated via an administrative 
> interface.
> I read various web2py based examples and gae examples on using memcache. Each 
> followed a different approach.
> As per my understanding from appengine documentation, I have written the 
> following helper function in a controller to use memcache:
> 
> from google.appengine.api import memcache
> 
> def get_tags():
>     """This function returns cached value for META_TAG table"""
>     tags = memcache.get("tags")
>     if not words:
>         words = 
> db(db.META_TAG.active_ind=='Y').select(db.META_TAG.tag,db.META_TAG.root_tag)
>         memcache.add("tags",tags)
>         return tags
>     else:
>         return tags
> 
> Will the above code ensure that the correct data is always available in the 
> memcache ?

You seem to be mixing the names 'words' and 'tags'.

As a matter of style, I'd move the return outside the if/else.

Don't forget to set the cache when you update the tags.

It's best to test for 'is None', in case your cached object can legitimately 
evaluate to False (an empty list or string, for example). So more like:

from google.appengine.api import memcache

def get_tags():
    """This function returns cached value for META_TAG table"""
    tags = memcache.get("tags")
    if tags is None:
        tags = 
db(db.META_TAG.active_ind=='Y').select(db.META_TAG.tag,db.META_TAG.root_tag)
        memcache.add("tags",tags)
    return tags

Reply via email to