irpas技术客

Redis基本命令 educoder_????simulate

网络投稿 6041

第一关 字符串、列表与集合

在 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”

#!/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 *********#

第二关 哈希表与有序集合

在 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 的值

#!/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 *********#

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

在 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

#!/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.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #Redis基本命令 #Educoder #第一关 #字符串列表与集合在 #task_empty #方法中从 #redis #中获取列表