Redis命令实践

Redis 基于实用主义,它有着非常广泛的应用场景,例如:消息队列,缓存,排行榜等等。我们已经学习了 Redis 的常用命令,接下来开始在应用中使用这些命令吧!

本实训通过四个典型的应用介绍 Redis 能做什么事情,并通过四个关卡检测你对 Redis 基本命令的掌握程度。
**
第1关:使用Redis管理登录令牌**

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import time
import redis

conn = redis.Redis()

# 核对令牌,并返回该令牌对应的用户 ID
def check_token(token):
    # 请在下面完成要求的功能
    #********* Begin *********#
    return conn.hget('login', token)
    #********* End *********#

# 更新令牌,同时存储令牌的创建时间
def update_token(token, user_id):
    # 请在下面完成要求的功能
    #********* Begin *********#
    timestamp = time.time()
    pipe = conn.pipeline()
    pipe.hset('login', token, user_id)
    pipe.zadd('recent:token', token, timestamp)
    pipe.execute()
    #********* End *********#

# 清理过期令牌
def clean_tokens():
    # 请在下面完成要求的功能
    #********* Begin *********#
    one_week_ago_timestamp = time.time() - 86400

    expired_tokens = conn.zrangebyscore('recent:token', 0, one_week_ago_timestamp)
    conn.zremrangebyscore('recent:token', 0, one_week_ago_timestamp)
    conn.hdel('login', *expired_tokens)
    #********* End ********

**
第2关:使用Redis实现购物车**

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import redis

conn = redis.Redis()

# 添加商品
def add_item(name, price):
    # 请在下面完成要求的功能
    #********* Begin *********#
    item_id = conn.incr('item_id')
    item_info_key = 'item:' + str(item_id) + ':info'
    conn.hmset(item_info_key, {
    
    "name": name, "price": price})
    conn.expire(item_info_key, 30 * 24 * 60 * 60)

    return item_id
    #********* End *********#

# 加入购物车
def add_to_cart(user_id, item, count):
    # 请在下面完成要求的功能
    #********* Begin *********#
    if count > 0:
        conn.hset('cart:' + user_id, item, count)
    else:
        conn.hrem('cart:' + user_id, item)
    #********* End *********#

# 获取购物车详情
def get_cart_info(user_id):
    # 请在下面完成要求的功能
    #********* Begin *********#
    return conn.hgetall('cart:' + user_id)
    #********* End *********#

第三关:使用Redis做页面缓存

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import redis

conn = redis.Redis()

# 使用 Redis 做页面缓存
def cache_request(request_url):
    # 请在下面完成要求的功能
    #********* Begin *********#
    page_key = 'cache:' + str(hash(request_url))
    content = conn.get(page_key)
    if not content:
        content = "content for " + request_url
        conn.setex(page_key, content, 600)
    return content



    #********* End *********#

第4关:使用Redis做数据缓存

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import time
import json
import redis

conn = redis.Redis()

# 将数据加入缓存队列
def add_cache_list(data_id, delay):
    # 请在下面完成要求的功能
    #********* Begin *********#
    conn.zadd('cache:delay', data_id, delay)
    conn.zadd('cache:list', data_id, time.time())
    #********* End *********#

# 缓存数据
def cache_data():
    # 请在下面完成要求的功能
    #********* Begin *********#
    next = conn.zrange('cache:list', 0, 0, withscores=True)
    now = time.time()
    if not next or next[0][1] > now:
        time.sleep(0.1)
    data_id = next[0][0]
    delay = conn.zscore('cache:delay', data_id)
    if delay <= 0:
        conn.zrem('cache:delay', data_id)
        conn.zrem('cache:list', data_id)
        conn.delete('cache:data:' + data_id)
    else:
        data = {
    
    'id': data_id, 'data': 'fake data'}
        conn.zadd('cache:list', data_id, now + delay)
        conn.set('cache:data:' + data_id, json.dumps(data))



    #********* End *********#

猜你喜欢

转载自blog.csdn.net/weixin_44196785/article/details/109191907