Golang + Redis解决缓存击穿(双层缓存)

Golang + Redis解决缓存穿透(双层缓存)

代码地址:

https://github.com/ziyifast/ziyifast-code_instruction/tree/main/redis_demo/cache_breakdown

1 概念

目前主流方案是在数据库前加一层缓存。类似于DB的防弹衣。

缓存击穿:Redis热点key过期,查询Redis不存在,高并发情况下大量请求打到DB。

拓展:

概念 产生原因 解决思路
缓存穿透 恶意请求不存在的Key 缓存空对象、布隆过滤器等
缓存雪崩 Redis大面积key过期 快速失败熔断、主从模式、集群模式。过期时间尽量随机
缓存击穿 热点Key过期 互斥更新、随机退避、差异失效时间(两份缓存cacheA、cacheB,差异过期时间)

1.1 缓存击穿:热点key过期

如淘宝天猫聚优选页面,经常会有最火HOT排行,这个排行定时会更新,那么在更新时,我们会去删除之前Redis的缓存,然后从数据库加载新的数据到Redis。

  • 删除时,我们可以理解为key过期了,因为浏览器缓存如果此时有大量用户在页面点击Redis中已经过期的商品,就会有大量请求打到数据库。

1.2 解决方案:双层缓存

解决方案:双层缓存(cacheA、cacheB)

  • 互斥更新、随机退避、差异失效时间(两份缓存,差异过期时间)
  • 我们可以将热点Key在Redis中存两份(两份过期时间不同=》差异过期时间),cacheA过期时,保证在cacheA加载新数据完成前,用户能查询到cacheB中的数据,不至于大量请求直接打到数据库。
  • 因为热点key的数据较少,因此缓存两份不会对内存有太多开销。

2 代码

2.1 模拟缓存击穿

1 演示
  • 案例解析:
  1. Redis中存热点数据20个,然后提供一个接口返回topK的热点数据
  2. 8s后刷新redis缓存,模拟key过期
  3. 重新加载数据sleep 6秒,模拟数据库及IO耗时

①热点key过期前,请求到了redis就返回了

②当热点key过期时,请求直接打到数据库

2 代码
go 复制代码
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis/v8"
	"github.com/kataras/iris/v12"
	context2 "github.com/kataras/iris/v12/context"
	"github.com/ziyifast/log"
	"time"
)

var RedisCli *redis.Client

func init() {
	RedisCli = redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   0,
	})
	_, err := RedisCli.Del(context.TODO(), GoodsKeyCacheA).Result()
	if err != nil {
		panic(err)
	}
}

type Goods struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

var (
	GoodsKeyCacheA = "goodsA"
	GoodsKeyCacheB = "goodsB"
)

func InitDb(s, count int, cacheKey string) {
	for i := s; i < s+count; i++ {
		g := &Goods{
			Id:   i + 1,
			Name: fmt.Sprintf("good-%d", i+1),
		}
		marshal, err := json.Marshal(g)
		if err != nil {
			panic(err)
		}
		_, err = RedisCli.RPush(context.TODO(), cacheKey, string(marshal)).Result()
		if err != nil {
			panic(err)
		}
	}
}

func main() {
	InitDb(0, 20, GoodsKeyCacheA)
	app := iris.New()
	app.Get("/goods/top/{offset}/{pageSize}", func(c *context2.Context) {
		offset, err := c.Params().GetInt64("offset")
		if err != nil {
			panic(err)
		}
		pageSize, err := c.Params().GetInt64("pageSize")
		if err != nil {
			panic(err)
		}
		start := (offset - 1) * pageSize
		end := offset*pageSize - 1
		err = c.JSON(QueryForData(start, end))
		if err != nil {
			panic(err)
		}
	})
	//set the expire time
	_, err := RedisCli.Expire(context.TODO(), GoodsKeyCacheA, time.Second*8).Result()
	if err != nil {
		panic(err)
	}
	go ReloadNewGoods()
	app.Listen(":9999", nil)
}

func QueryForData(start, end int64) []string {
	val := RedisCli.LRange(context.TODO(), GoodsKeyCacheA, start, end).Val()
	log.Infof("query redis of cache A")
	if len(val) == 0 {
		log.Infof("redis is not exist, query db.....no!!!!")
	}
	return val
}

func ReloadNewGoods() {
	time.Sleep(time.Second * 15)
	log.Infof("start ReloadNewGoods......")
	InitDb(2000, 20, GoodsKeyCacheA)
	log.Infof("ReloadNewGoods......DONE")
}

2.2 双层缓存解决问题:热点key存两份(cacheA、cacheB)

在Redis中将热点key缓存两份。且cacheA、cacheB过期时间需要不同,保证在新热点数据加载到Redis之前,CacheB能提供服务。因为热点key数据不会存在太多,所以一般缓存两份对我们内存开销影响不会很大。(当然具体情况具体分析,并非绝对)

  1. 热点数据存两份一样的:cacheA、cacheB(两份缓存key的过期时间需要不同)
  2. 到点了,需要更新热点数据,cacheA过期,重新读取加载热点数据到Redis,更新cacheA的数据为最新。此时对外提供服务的是cacheB。防止请求直接打到DB。
  3. cacheA加载最新数据完成后,cacheA对外提供服务。更新cacheB中的数据到最新。(保证数据一致)

注意:service层查询时,先查cacheA有没有,如果有直接返回,如果没有查cacheB的数据。

1 演示

①获取TopK数据

②当热点key更新时(cacheA过期时),保证cacheB有数据,避免数据打到数据库

2 代码
go 复制代码
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis/v8"
	"github.com/kataras/iris/v12"
	context2 "github.com/kataras/iris/v12/context"
	"github.com/ziyifast/log"
	"time"
)

var RedisCli *redis.Client

func init() {
	RedisCli = redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
		DB:   0,
	})
	_, err := RedisCli.Del(context.TODO(), GoodsKeyCacheA).Result()
	if err != nil {
		panic(err)
	}
}

type Goods struct {
	Id   int    `json:"id"`
	Name string `json:"name"`
}

var (
	GoodsKeyCacheA = "goodsA"
	GoodsKeyCacheB = "goodsB"
)

func InitDb(s, count int, cacheKey string) {
	for i := s; i < s+count; i++ {
		g := &Goods{
			Id:   i + 1,
			Name: fmt.Sprintf("good-%d", i+1),
		}
		marshal, err := json.Marshal(g)
		if err != nil {
			panic(err)
		}
		_, err = RedisCli.RPush(context.TODO(), cacheKey, string(marshal)).Result()
		if err != nil {
			panic(err)
		}
	}
}

func main() {
	InitDb(0, 20, GoodsKeyCacheA)
	app := iris.New()
	app.Get("/goods/top/{offset}/{pageSize}", func(c *context2.Context) {
		offset, err := c.Params().GetInt64("offset")
		if err != nil {
			panic(err)
		}
		pageSize, err := c.Params().GetInt64("pageSize")
		if err != nil {
			panic(err)
		}
		start := (offset - 1) * pageSize
		end := offset*pageSize - 1
		err = c.JSON(QueryForData(start, end))
		if err != nil {
			panic(err)
		}
	})
	//set the expire time
	_, err := RedisCli.Expire(context.TODO(), GoodsKeyCacheA, time.Second*8).Result()
	if err != nil {
		panic(err)
	}
	InitDb(0, 20, GoodsKeyCacheB)
	//add cacheB, expire time is different from cacheA (make sure new goods will be added to cacheA)
	_, err = RedisCli.Expire(context.TODO(), GoodsKeyCacheB, time.Second*20).Result()
	if err != nil {
		panic(err)
	}
	go ReloadNewGoods()
	app.Listen(":9999", nil)
}

func QueryForData(start, end int64) []string {
	val := RedisCli.LRange(context.TODO(), GoodsKeyCacheA, start, end).Val()
	log.Infof("query redis of cache A")
	if len(val) == 0 {
		log.Infof("cacheA is not exist, query redis of cache B")
		val = RedisCli.LRange(context.TODO(), GoodsKeyCacheB, start, end).Val()
		if len(val) == 0 {
			log.Infof("cacheB is not exist, query db, no!!!")
			return val
		}
	}
	return val
}

func ReloadNewGoods() {
	time.Sleep(time.Second * 15)
	log.Infof("start ReloadNewGoods......")
	InitDb(2000, 20, GoodsKeyCacheA)
	//set the expire time of cacheA
	log.Infof("ReloadNewGoods......DONE")
	//reload cacheB....
	//set the expire time of cacheB
}
相关推荐
卷毛的技术笔记1 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
会编程的土豆1 小时前
Go 语言反射(Reflection)详解
开发语言·后端·golang
Cosolar1 小时前
从零写一个 Attention Is All You Need
人工智能·面试·架构
喵个咪2 小时前
GoWind Toolkit Go后端代码生成 完整全流程实战
后端·go·orm
basketball6162 小时前
Go 语言从入门到进阶:4. 数组和MAP使用方法总结
开发语言·后端·golang
qq_2518364572 小时前
SpringBoot+Vue 共享电池柜管理系统 完整实现 前后端分离项目实战 完整代码
vue.js·spring boot·后端
zhangxingchao3 小时前
AI 大模型核心六:量化、Workflow 与 Agent、多轮 RAG
前端·人工智能·后端
IT_陈寒4 小时前
Vite打包时遇到的坑,原来问题出在这里
前端·人工智能·后端
jiayong234 小时前
AI架构师面试题库 - 完整汇总文档
人工智能·面试·职场和发展
ayqy贾杰5 小时前
基层管理的三板斧,在AI时代行不通了
前端·后端·团队管理