Problems using @cache.action(valid_statuses=[200])

63 views
Skip to first unread message

Lisandro

unread,
Jun 9, 2016, 12:13:51 PM6/9/16
to web2py-users
I'm having trouble to avoid caching pages that returned anything else than HTTP 200.

Consider the following simple example:

@cache.action(time_expire=300, cache_model=cache.redis, session=False, vars=False, public=True, valid_statuses=[200])
def index():
   
raise HTTP(503)

When I hit the URL the first time, I can see in deed a 503 response status (I checked headers with Chrome Inspector), and there is nothing cached (I checked redis stored keys).
But the second time I hit the URL, the server returns an HTTP 200 with an empty body, and it keeps returning HTTP 200 for the 5 minutes (time_expire=300).
After that time, I hit the URL again, and I can see the HTTP 500. 

What am I missing?

Dave S

unread,
Jun 9, 2016, 2:46:27 PM6/9/16
to web...@googlegroups.com


As a WAG (I dunno the cache manipulators), your "valid_statuses" is setting the status to be returned when the page is served from cache.

 /dps

Anthony

unread,
Jun 9, 2016, 2:55:52 PM6/9/16
to web2py-users
Looks like a bug in the Redis cache implementation. cache.action first saves the output to the cache, and later if it is determined that the HTTP status is not valid, it then attempts to delete the output from the cache via cache_model(cache_key, None). Passing None as the second argument to one of the cache models is supposed to delete the key, but this does not work with Redis -- see https://github.com/web2py/web2py/blob/R-2.14.6/gluon/contrib/redis_cache.py#L152. In the Redis cache client, setting the f argument to None only deletes the key if the object wasn't found in the cache or has expired. We should probably change the Redis cache to match the behavior of the other cache models.

Anthony

Anthony

unread,
Jun 9, 2016, 2:58:13 PM6/9/16
to web2py-users
On Thursday, June 9, 2016 at 2:46:27 PM UTC-4, Dave S wrote:

As a WAG (I dunno the cache manipulators), your "valid_statuses" is setting the status to be returned when the page is served from cache.

No, he's using valid_statuses correctly -- the output is cached only when the status matches one of those in the valid_statuses list. Actually, it is unnecessary to list 200, because by default, only statuses beginning with 1, 2, or 3 are considered valid.

Anthony

Anthony

unread,
Jun 9, 2016, 3:09:27 PM6/9/16
to web2py-users, web2py-developers

Lisandro

unread,
Apr 27, 2017, 11:16:03 AM4/27/17
to web2py-users, web2py-d...@googlegroups.com
Sorry to bother in this old thread, but I've noticed that the last stable version of web2py doesn't include the fix that was made previously to correct this bug.

I'm trying with last version: 2.14.6-stable+timestamp.2016.05.10.00.21.47 but the bug is there.
I checked the source code of gluon/contrib/redis_cache.py and I noticed that the fix isn't there. 

The issue had been reported here: https://github.com/web2py/web2py/issues/1355

I've tried applying that fix over the current version, but I don't think it solves the problem: the function now does return 503, but it takes about 10 seconds to respond, and I can see that with every request I made to the function, a new key is added to the cache. Should the issue be re-opened?
Reply all
Reply to author
Forward
0 new messages