how to apply Web2py's @cache decorator to a function with multiple parameters

128 views
Skip to first unread message

Carl

unread,
Feb 28, 2011, 7:42:32 PM2/28/11
to web2py-users
When I add @cache as below in my default.py

@cache(request.env.path_info, 60, cache.ram)
def search_user_contacts(searchText):
from applications.init.modules.user import User
user = User(globals())
contacts = user.find_contacts(searchText)
return searchText, contacts

I get a runtime error: TypeError: <lambda>() takes no arguments (1
given)

In some way I'm failing to set-up this decorator to be smart enough to
call the function with the 'searchText' parameter.

Does anyone know where I've gone wrong?

Carl

unread,
Feb 28, 2011, 8:36:29 PM2/28/11
to web2py-users
This works but doubles up on the number of functions needed. But by
calling cache.ram directly I'm able to define a lambda function that
uses the 'searchText' parameter. can anyone take this one step
further?

def search_user_contacts_cached(searchText):
from applications.init.modules.user import User
user = User(globals())
contacts = user.find_contacts(searchText)

return searchText, contacts

@service.jsonrpc
def search_user_contacts(searchText):
return cache.ram('search_contact=%s'%searchText,
lambda: search_user_contacts_cached(searchText),
60)
Reply all
Reply to author
Forward
0 new messages