0


头歌 Redis安全与性能

Redis安全与性能

第1关:持久化

在这里插入图片描述

第2关:复制

在这里插入图片描述

第3关:Redis事务与流水线

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

conn = redis.Redis()# 将商品放到平台上defadd_item_to_market(itemid, sellerid, price):# 请在下面完成要求的功能#********* Begin *********#
    repertory ="inventory:"+ sellerid
    item = itemid +"."+ sellerid
    end = time.time()+5
    pipe = conn.pipeline()while time.time()< end:try:
            pipe.watch(repertory)ifnot pipe.sismember(repertory, itemid):
                pipe.unwatch()returnNone
            pipe.multi()
            pipe.zadd("market", item, price)
            pipe.srem(repertory, itemid)
            pipe.execute()returnTrueexcept redis.exceptions.WatchError:passreturnFalse#********* End *********## 购买商品defpurchase(buyerid, itemid):# 请在下面完成要求的功能#********* Begin *********#
    item, sellerid = itemid.split(".")
    buyer ="users:"+ buyerid
    seller ="users:"+ sellerid
    repertory ="inventory:"+ buyerid
    end = time.time()+10
    pipe = conn.pipeline()while time.time()< end:try:
            pipe.watch("market", buyer)
            price = pipe.zscore("market", itemid)
            funds =int(pipe.hget(buyer,"funds"))if funds < price:
                pipe.unwatch()returnNone

            pipe.multi()
            pipe.hincrby(seller,"funds",int(price))
            pipe.hincrby(buyer,"funds",int(-price))
            pipe.sadd(repertory, item)
            pipe.zrem("market", itemid)
            pipe.execute()returnTrueexcept redis.exceptions.WatchError:passreturnFalse#********* End *********#
标签: redis 数据库 缓存

本文转载自: https://blog.csdn.net/qq_59334339/article/details/127865022
版权归原作者 小陈cc_79 所有, 如有侵权,请联系我们删除。

“头歌 Redis安全与性能”的评论:

还没有评论