Created
January 18, 2013 17:23
-
-
Save jmoz/4566295 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/') | |
@app.route('/<int:page>') | |
def index(page=1): | |
# set up the pagination params, set count later | |
p = Pagination(per_page=10, current_page=page) | |
timeline = RedisTimeline.find_paginated(p) | |
# pass the pagination object to the view, so a list of links can be displayed | |
return render_template('index.html', timeline=timeline, pagination=p) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a simplified class that uses Redis | |
class RedisTimeline(object): | |
@classmethod | |
def find_paginated(self, pagination): | |
# self.count returns the total number of items in the timeline | |
pagination.total = self.count() | |
# pass in the pagination params which can be used as offset | |
timeline = self.find(limit=pagination.per_page, start=pagination.start) | |
return timeline | |
@classmethod | |
def find(self, limit=100, start=0): | |
"""ZREVRANGE foo <$OFFSET> <$OFFSET + $LIMIT - 1> will yield correct results if both OFFSET and LIMIT are assumed to be zero-based. | |
So 0/0 gives you the full range, then offset 10 limit 10 will give you items 11 to 20.""" | |
ids = r.zrevrange('myrediskey', start, start + limit - 1) | |
# do something with ids and generate a timeline object | |
return timeline |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment