I have a listing site having a model with many properties on which I would like to use filters. I would like to use memcache and cursor for querying, e.g:
results=Model.all().filter("x =", a).filter("y =",b).with_cursor(cursor).fetch(20).
How should I handle cursor and pagination, when 开发者_开发百科the user change the filter criteria, e.g.
from `x=a to x=c`?
Should I store cursor having key = query string? But then the query string changes with page numbers :( . I guess i will need to parse query string, remove page numbers and use that as a key for cursor. Is that how I should do it?
You can make an "hash" of your current filter, and pass it to view. That can be stores there as an hidden field, like <input type="hidden" name="prev_query" value="{{query_hash}}"/>
On second request you'll check that current filter's hash equals to passed as parameter.
'Hash' maybe md5 of your filter params, or just join concatenation of them.
Think of a cursor like a bookmark, holding place in a result set. Cursors are specific to the query they are for. You can't use the same cursor for two different queries - that would be akin to expecting a bookmark from one book to show you how far through you are in another.
If you want to store cursors elsewhere, you'll need to key them by the filter criteria so you can look up the appropriate one. Memcache is a poor choice, though, as elements may be evicted at any time. Why not just make the cursor part of the 'next page' URL?
精彩评论