当前位置:   article > 正文

头歌 Redis安全与性能(实验答题)_头歌redis命令实践答案最新

头歌redis命令实践答案最新

第一关

6cb2cab6fbca42baa4567a8fd696eee0.png

8adda7d63863471b8bcacd08f7d02e1c.png 2da583b903264d47997a5114b0be1bcb.png

 

 

第二关

1fb0f48a60a54cf38ce754c06d0138cb.png

2f13c4d7c5d34896b31a81faf9cf573f.png 

f7df83d34ecc45938cadd8b54032611c.png 

 

第三关(将代码文件全部替换即可)

 

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

import time
import redis

conn = redis.Redis()

# 将商品放到平台上
def add_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)
            if not pipe.sismember(repertory, itemid):
                pipe.unwatch()
                return None
            pipe.multi()
            pipe.zadd("market", item, price)
            pipe.srem(repertory, itemid)
            pipe.execute()
            return True
        except redis.exceptions.WatchError:
            pass
    return False
    #********* End *********#

# 购买商品
def purchase(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()
                return None

            pipe.multi()
            pipe.hincrby(seller, "funds", int(price))
            pipe.hincrby(buyer, "funds", int(-price))
            pipe.sadd(repertory, item)
            pipe.zrem("market", itemid)
            pipe.execute()
            return True
        except redis.exceptions.WatchError:
            pass
    return False
    #********* End *********#

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/668426
推荐阅读
相关标签
  

闽ICP备14008679号