mod13b-memorystore/main.py
def root():
    'main application (GET) handler'
    # check for (hour-)cached visits
    ip_addr, usr_agt = request.remote_addr, request.user_agent
    visitor = '{}: {}'.format(ip_addr, usr_agt)
    rsp = REDIS.get('visits')
    visits = pickle.loads(rsp) if rsp else None

    # register visit & run DB query if cache empty or new visitor
    if not visits or visits[0]['visitor'] != visitor:
        store_visit(ip_addr, usr_agt)
        visits = list(fetch_visits(10))
        REDIS.set('visits', pickle.dumps(visits), ex=HOUR)

    return render_template('index.html', visits=visits)
main application (GET) handler
main application (GET) handler
main application ( GET ) handler
def root():
    ip_addr, usr_agt = request.remote_addr, request.user_agent
    visitor = '{}: {}'.format(ip_addr, usr_agt)
    rsp = REDIS.get('visits')
    visits = pickle.loads(rsp) if rsp else None
    if not visits or visits[0]['visitor'] != visitor:
        store_visit(ip_addr, usr_agt)
        visits = list(fetch_visits(10))
        REDIS.set('visits', pickle.dumps(visits), ex=HOUR)
    return render_template('index.html', visits=visits)
def root ( ) : ip_addr , usr_agt = request . remote_addr , request . user_agent visitor = '{}: {}' . format ( ip_addr , usr_agt ) rsp = REDIS . get ( 'visits' ) visits = pickle . loads ( rsp ) if rsp else None if not visits or visits [ 0 ] [ 'visitor' ] != visitor : store_visit ( ip_addr , usr_agt ) visits = list ( fetch_visits ( 10 ) ) REDIS . set ( 'visits' , pickle . dumps ( visits ) , ex = HOUR ) return render_template ( 'index.html' , visits = visits )
main application (GET) handler
main application ( GET ) handler
'main application (GET) handler' # check for (hour-)cached visits # register visit & run DB query if cache empty or new visitor