Redis基本命令


第1关:字符串、列表与集合

编程要求
根据提示,在右侧Begin-End区域补充代码,完成任务分配的后端处理逻辑:

在 task_empty() 方法中:
从 Redis 中获取列表 task_list 的长度,判断是否为 0
若为 0,则返回 True
若不为 0,则返回 False
在 get_task() 方法中:
从列表 task_list 的最右侧弹出一个元素,赋值给 task
将 task 的值设置到 Redis 的字符串键 current_task 中
在 get_unallocated_staff() 方法中:
从集合 unallocated_staff 中随机返回一个元素,赋值给 staff
将上面的 staff 从集合 unallocated_staff 移动到集合 allocated_staff 中
返回(return)staff 的值
在 allocate_task(staff) 方法中:
将参数 staff 的值追加到 Redis 字符串键 current_task 的尾部,中间以 : 间隔
将追加后的字符串键 current_task 从左侧推入列表 task_queue
将字符串键 current_task 的值设置为 “None”
测试说明
我会对你编写的代码进行测试:

测试输入:

task_1 task_2 task_3 task_4 task_5
staff_1 staff_2 staff_3 staff_4 staff_5
预期输出:

Init task list: [‘task_1’, ‘task_2’, ‘task_3’, ‘task_4’, ‘task_5’]
Init staff list: set([‘staff_4’, ‘staff_5’, ‘staff_1’, ‘staff_2’, ‘staff_3’])
Cur task list is empty: False
Get new task: task_5
Current staff is allocated: True
Current staff is unallocated: False
Current task is: None
Allocated all tasks
Task queue length: 5
Task list is empty: True
Allocated_staff: set([‘staff_4’, ‘staff_5’, ‘staff_1’, ‘staff_2’, ‘staff_3’])
Unallocated_staff: set([])
代码示例如下:

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

import redis

conn = redis.Redis()

def task_empty():
    # 请在下面完成判断任务列表是否为空
    #********* Begin *********#
    return int(conn.llen("task_list"))==0
    #********* End *********#

def get_task():
    # 请在下面完成获取一个任务
    #********* Begin *********#
    task = conn.rpop("task_list")
    conn.set("current_task",task)

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

def get_unallocated_staff():
    # 请在下面完成获取一个未分配的员工
    #********* Begin *********#
    staff=conn.srandmember("unallocated_staff")
    conn.smove("unallocated_staff","allocated_staff",staff)
    return staff
    #********* End *********#

def allocate_task(staff):
    # 请在下面完成分配任务
    #********* Begin *********#
    conn.append("current_task",':'+str(staff))
    conn.lpush("task_queue",conn.get("current_task"))
    conn.set("current_task","None")

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

在这里插入图片描述

第2关:哈希与有序集合

编程要求
根据提示,在右侧Begin-End区域补充代码,完成带优先级的队列系统的后端处理逻辑:

在 set_task_info(task_id) 方法中:
使用参数 task_id 作为域,初始状态 “init” 作为值构成域-值对,存放在 task_status 哈希键中。
在 add_task_to_queue(task_id, priority) 方法中:
参数说明:
task_id 为任务 ID
priority 为任务优先级。
将分值(优先级)为 priority 的成员 task_id 存入有序集合 task_queue 中。
注意将参数 priority 转换为整型
调用 set_task_info() 方法,传入参数 task_id
在 get_task() 方法中:
新建变量 task_list_by_priority,值为:
使用 ZREVRANGE 命令按照分值(优先级)从大到小顺序返回有序集合 task_queue 的全部成员。
新建变量 current_task,值为:
task_list_by_priority 中的第一个元素(下标为 0)
将成员 current_task 从有序集合 task_queue 中移除
修改哈希 task_status 中的 current_task 域的值为 “processing”
返回(return)current_task 的值
测试说明
我会对你编写的代码进行测试:

测试输入:

1 2 3 4 5 6 7 8 9 10
2 4 9 1 0 5 8 6 7 3
预期输出:

Add new task: 1, priority: 2, status: init
Add new task: 2, priority: 4, status: init
Add new task: 3, priority: 9, status: init
Add new task: 4, priority: 1, status: init
Add new task: 5, priority: 0, status: init
Add new task: 6, priority: 5, status: init
Add new task: 7, priority: 8, status: init
Add new task: 8, priority: 6, status: init
Add new task: 9, priority: 7, status: init
Add new task: 10, priority: 3, status: init
Before: task list is: [‘3’, ‘7’, ‘9’, ‘8’, ‘6’, ‘2’, ‘10’, ‘1’, ‘4’, ‘5’]
Get new task: 3
After: task list is: [‘7’, ‘9’, ‘8’, ‘6’, ‘2’, ‘10’, ‘1’, ‘4’, ‘5’]
Current task status: processing

代码示例如下:

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

import redis

conn = redis.Redis()

# 初始化任务信息到 Redis 中
def set_task_info(task_id):
    # 请在下面完成要求的功能
    #********* Begin *********#
    conn.hset("task_status",task_id,"init")
    #********* End *********#

# 将任务添加至任务队列
def add_task_to_queue(task_id, priority):
    # 请在下面完成要求的功能
    #********* Begin *********#
    conn.zadd("task_queue",task_id,int(priority))
    set_task_info(task_id)
    #********* End *********#

# 从任务队列中取出优先级最高的任务
def get_task():
    # 请在下面完成要求的功能
    #********* Begin *********#
    task_list_by_priority=conn.zrevrange('task_queue',0,-1)
    current_task=task_list_by_priority[0]
    conn.zrem('task_queue',current_task)
    conn.hset("task_status",current_task,"processing")
    return current_task
    #********* End *********#

在这里插入图片描述

第3关:Redis基本事务与其他命令

编程要求
根据提示,在右侧Begin-End区域补充代码,完成网络约车的后端处理逻辑:

在 request_cab(user_id, priority) 方法中:
判断是否存在哈希键 request:info:用户ID 的 time 域:
提示:可使用 HEXISTS 命令
若存在,则直接 return
若不存在,做如下操作
使用事务提交下列命令:
将参数 user_id 从最左侧推入列表 cab:queue
使用 HMSET 命令设置哈希键 request:info:用户ID:
域 time,值为 time.time()
域 priority,值为参数 priority
将上述哈希键的过期时间设置为 10分钟
在 allocate() 方法中:
使用 SORT 命令对列表 cab:queue 排序,并将结果赋值给 cab_queue:
使用 BY 参数
参考键为哈希键 request:info:*,其中 * 为占位符
使用上述参考键中的 priority 域
使用 DESC 参数做倒序排序
取出 cab_queue 的第一个元素(下标为 0)赋值给 current_respond
从列表 cab:queue 中移除变量 current_respond 中包含的元素
返回(return)current_respond
测试说明
我会对你编写的代码进行测试:

测试输入:

1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1
预期输出:

Receive new request: 1, priority: 9, is_expired? True
Receive new request: 2, priority: 8, is_expired? True
Receive new request: 3, priority: 7, is_expired? True
Receive new request: 4, priority: 6, is_expired? True
Receive new request: 5, priority: 5, is_expired? True
Receive new request: 6, priority: 4, is_expired? True
Receive new request: 7, priority: 3, is_expired? True
Receive new request: 8, priority: 2, is_expired? True
Receive new request: 9, priority: 1, is_expired? True
Before: request queue: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
Allocate new request: 1
After: request queue: [‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
Repeat request in few seconds:
Before: request queue length: 8
After: request queue length: 8

代码示例如下:

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

import time
import redis

conn = redis.Redis()

# 用户端发起派车请求
def request_cab(user_id, priority):
    # 请在下面完成要求的功能
    #********* Begin *********#
    if conn.hexists('request:info:' + str(user_id), 'time'):
        return
    pipe = conn.pipeline()
    pipe.lpush('cab:queue', user_id)
    pipe.hmset('request:info:'+str(user_id), {
   'time': time.time(), 'priority':priority})
    pipe.expire('request:info:'+ str(user_id), 10 * 60)
    pipe.execute()
    #********* End *********#

# 平台选择优先级最高的派车请求并派车
def allocate():
    # 请在下面完成要求的功能
    #********* Begin *********#
    cab_queue=conn.sort('cab:queue',by='request:info:*->priority',desc=True)
    current_respond=cab_queue[0]
    conn.lrem('cab:queue', current_respond, 1)
    return current_respond
    #********* End *********#

# 用户端取消派车请求
def cancel_cab(user_id):
    conn.expire('request:info:' + str(user_id), 0)
    conn.lrem('cab:queue', user_id)


在这里插入图片描述


相关推荐

  1. redis基本命令

    2023-12-05 21:24:04       8 阅读
  2. Redis 常用基本命令

    2023-12-05 21:24:04       9 阅读
  3. Redis基础命令集详解

    2023-12-05 21:24:04       19 阅读
  4. Redis基础命令详解

    2023-12-05 21:24:04       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-05 21:24:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-05 21:24:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-05 21:24:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-05 21:24:04       20 阅读

热门阅读

  1. mysql多版本并发控制mvcc

    2023-12-05 21:24:04       30 阅读
  2. python代码块整行缩进与取消整行缩进快捷键

    2023-12-05 21:24:04       39 阅读
  3. 题目 异常处理

    2023-12-05 21:24:04       32 阅读
  4. day69

    day69

    2023-12-05 21:24:04      32 阅读
  5. 数据产品经理常用的ChatGPT通用提示词模板

    2023-12-05 21:24:04       37 阅读
  6. Ubuntu20.04 Kimera Semantic运行记录

    2023-12-05 21:24:04       42 阅读
  7. 机器学习之决策树及随机森林

    2023-12-05 21:24:04       37 阅读
  8. 修复 Ubuntu 2204 Wi-Fi 热点无法连接问题

    2023-12-05 21:24:04       43 阅读
  9. 决策树详解

    2023-12-05 21:24:04       30 阅读
  10. 编译ubuntu kernel

    2023-12-05 21:24:04       32 阅读