1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
Files
go-mixed/cache/readme.md
2023-10-13 10:52:36 +08:00

31 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 二级缓存模块
提供本地缓存和分布式缓存组合的缓存模块,可以只使用本地缓存或分布式缓存。并可全局禁用缓存。
1. 一级缓存可使用freecache或bigcache作为本地缓存当数据在本地缓存不存在时会向二级缓存请求数据
2. 二级缓存使用redis作为缓存模块当数据在二级缓存不存在时向资源请求数据。
3. 更新数据时将二级分布式缓存中对应的数据删除,一级缓存使用订阅/发布机制进行删除。
## 功能列表
1. 支持自定义一级或二级缓存和订阅/发布机制。
2. 缓存击穿;单机资源互斥锁,集群环境中每台机器只有一次请求到资源层,其它请求等待数据同步到缓存后获取数据。
3. 缓存穿透;从数据源中未找到数据时,在缓存中缓存空值。
4. 缓存雪崩;为防止缓存雪崩将资源放入缓存时,对过期时间添加一个随机过期时间,防止缓存同时过期。
5. 自动续期;当访问二级缓存时对使用的资源进行延期。
## 使用方式
创建
```go
cache.New().UseRedis().UseBigCache().Build()
```
```go
Cache.Get(key, dist, func() (bool,error){}, options func(){})
Cache.GetFn(context, key, dist, func() (bool, error))
```