惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
T
Threatpost
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
O
OpenAI News
Project Zero
Project Zero
G
GRAHAM CLULEY
P
Privacy International News Feed
A
Arctic Wolf
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
D
Docker
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
W
WeLiveSecurity
P
Proofpoint News Feed
腾讯CDC
Cloudbric
Cloudbric
S
Secure Thoughts
C
Check Point Blog
博客园 - Franky
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
GbyAI
GbyAI
Security Archives - TechRepublic
Security Archives - TechRepublic
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
F
Fortinet All Blogs
博客园 - 叶小钗
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
C
Cisco Blogs
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research

Chenxu's Blog

RAG检索优化:从 68% 到 82.2% 学习深度学习 北邮计算机研究生选课指北 学习机器学习 上海篇 提出一个好问题 写代码的环境 选购一台合适的设备 计算机的第零课 序言 内蒙古篇 山东篇 一篇对transformers的疑惑 浅析GPUStack 浅谈后端项目分层 浙江篇 河南篇 一些三端协同的开发工具记录 浅谈编程语言中的 GC 在 Windows 上安装 Rust 美化博客 消息队列 MyBatis 现代交换原理2024年题目 回忆版 北邮计科本科课程指北 ClkLog埋点用户分析系统研究报告 大数据梧桐实验分享 大数据HBase实验分享 国产向量数据库研究实践 —— 基于 Milvus 的电影推荐系统 服务器环境配置 大数据HDFS实验分享 SSE多服务之间推送数据 数据库系统原理 操作系统 编译原理 手把手教你gozero从开发到部署 (番外篇): 介绍 gtodolist 前端项目 天津篇 北京篇 前端相关问题 vue3 + element-plus学习 git相关命令 人工智能原理 使用 MongoTemplate 时开启事务 MySQL Redis 力扣刷题笔记之动态规划 记录一次 VSCode 出现的各种奇怪的问题 手把手教你gozero从开发到部署 (4): 在 model 中自己编写函数实现数据库分页查询 记录平时用到的 windows 右键管理 记录平时使用的 linux 的指令 安装 gstore 并使用 java api 手把手教你gozero从开发到部署 (3): gtodolist 之任务的创建和修改 手把手教你gozero从开发到部署 (番外篇): 记录第一次部署 CI/CD 的流程 记录完成 gtodolist 中遇到的 bug 手把手教你gozero从开发到部署 (2): gtodolist 之 user 模块开发 手把手教你gozero从开发到部署 (1): gtodolist 项目说明和环境准备 快速入门 gozero 框架 vue部署在nginx上的相关问题 记一次安装scrapy的报错 将博客部署至服务器 Java CS143: Compliers 《MySQL必知必会》读书笔记(2) 初识 GORM 初识 gin 框架 Spring 《MySQL必知必会》读书笔记(1) 本地搭建博客 🤝友链 🙋🏻‍♂️关于 Browser and Device Check
手把手教你gozero从开发到部署 (5): gtodolist 之任务的删除
Chenxu · 2024-02-11 · via Chenxu's Blog

代码重构

上一章节末尾 我们增加了一个删除缓存的代码以供增加和修改任务时调用, 但是这个写法不太好. 不仅代码逻辑上不够清晰, 再调用时也会每次都需要新建一个新的ListTaskLogic对象, 对性能有一定消耗. 因此我新建了一个cache_logic.go文件专门用于操作缓存.

删除 list 缓存的代码做部分修改如下:

// DeleteListCache 删除 list 缓存
func DeleteListCache(r *redis.Redis, uid int64) {
	prefix := "cache:gtodolist:task:list:"
	listKey := fmt.Sprintf("%s%v:*", prefix, uid)
	keys, _ := r.Keys(listKey)

	for _, key := range keys {
		_, _ = r.Del(key)
	}
}

调用语句如下:

DeleteListCache(l.svcCtx.RedisClient, in.Uid)

删除任务

代码框架已经生成好, 直接填写逻辑代码即可

api 层代码, 依旧是直接调用 rpc 即可:

func (l *TaskDeleteLogic) TaskDelete(req *types.DeleteReq) (resp *types.DeleteResp, err error) {
	deleteResp, err := l.svcCtx.TaskRpcClient.DeleteTask(l.ctx, &pb.DeleteReq{
		Uid: ctxdata.GetUidFromCtx(l.ctx),
		Id:  req.Id,
	})
	if err != nil {
		return &types.DeleteResp{
			Status:  int(vo.ErrServerCommonError.GetErrCode()),
			Message: err.Error(),
			Error:   err.Error(),
		}, nil
	}

	resp = &types.DeleteResp{}
	_ = copier.Copy(resp, deleteResp)
	return resp, err
}

rpc 层代码, 先校验任务 id 是否合法, 然后删除 list 缓存和数据库内数据

func (l *DeleteTaskLogic) DeleteTask(in *pb.DeleteReq) (*pb.DeleteResp, error) {
	tid, err := strconv.ParseInt(in.Id, 10, 64)
	if err != nil {
		return nil, errors.Wrap(err, "转换任务id失败, 请传入合法id")
	}

	DeleteListCache(l.svcCtx.RedisClient, in.Uid)

	err = l.svcCtx.TaskModel.Delete(l.ctx, nil, tid)
	if err != nil && err != gormc.ErrNotFound {
		return nil, errors.Wrap(err, "删除任务失败")
	}

	return &pb.DeleteResp{
		Status:  vo.Ok,
		Message: "删除成功",
	}, nil
}

代码测试无误后至此手把手教你 gozero 从开发到部署系列 就全部完结啦