文章目录
- +——————+ +—————–+ | Cache | | Item | +——————+ +—————–+ | – items: map |1 * | – Value: interface{} | – mu: RWMutex |———| – Expiration: int64 | – cleanupInterval| +—————–+ | – stopChan: chan | +——————+ 结构说明: Cache 结构体: items:存储缓存项的映射表 mu:读写锁,保证并发安全 cleanupInterval:清理过期项的间隔时间 stopChan:停止后台清理的信号通道 Item 结构体: Value:存储的任意类型值 Expiration:过期时间戳(纳秒级)
- +————-+ | Set操作 | +——+——+ | v +——+ 获取写锁 +——+ | +———-> | | 缓存 | | 缓存 | | 状态 | | 状态 | | <———-+ | +——+ 设置值后释放锁 +——+ | v +————-+ | Get操作 | +——+——+ | v +——+ 获取读锁 +——+ | +———-> | | 缓存 | | 缓存 | | 状态 | | 状态 | | <———-+ | +——+ 读取值后释放锁 +——+ | v +————-+ | 后台清理任务 | +——+——+ | v +——+ 获取写锁 +——+ | +———-> | | 缓存 | | 缓存 | | 状态 | 删除过期项 | 状态 | | <———-+ | +——+ 释放锁 +——+
- 使用sync.RWMutex实现读写分离: 写操作使用互斥锁(Lock/Unlock) 读操作使用读锁(RLock/RUnlock) 允许多个读操作并行,提高读密集型场景性能 func (c *Cache) Set(key string, value interface{}, ttl time.Duration) { c.mu.Lock() // 写操作使用互斥锁 defer c.mu.Unlock() // … } func (c *Cache) Get(key string) (interface{}, bool) { c.mu.RLock() // 读操作使用读锁 defer c.mu.RUnlock() // … }
- 清理策略特点: 后台goroutine定期执行清理 避免每次读写都检查过期,提高性能 清理间隔可配置(默认1分钟) 使用通道实现优雅停止 func (c *Cache) startCleanup() { ticker := time.NewTicker(c.cleanupInterval) defer ticker.Stop() for { select { case <-ticker.C: c.cleanup() // 定期执行清理 case <-c.stopChan: // 接收停止信号 return } } } func (c *Cache) cleanup() { c.mu.Lock() defer c.mu.Unlock() now := time.Now().UnixNano() for key, item := range c.items { if item.Expiration > 0 && now > item.Expiration { delete(c.items, key) // 删除过期项 } } }
- 使用纳秒级时间戳存储过期时间: 精度高,避免时间精度问题 比较时直接使用整数比较,效率高 支持永久存储(设置过期时间为0) expiration := time.Now().Add(ttl).UnixNano()
- type ShardedCache struct { shards []*Cache } func NewShardedCache(shardCount int, cleanupInterval time.Duration) *ShardedCache { cache := &ShardedCache{ shards: make([]*Cache, shardCount), } for i := range cache.shards { cache.shards[i] = NewCache(cleanupInterval) } return cache } func (sc *ShardedCache) getShard(key string) *Cache { h := fnv.New32a() h.Write([]byte(key)) return sc.shards[h.Sum32()%uint32(len(sc.shards))] } 优势: 减少锁竞争 提高并发性能 特别适合高并发场景
- func (c *Cache) Get(key string) (interface{}, bool) { c.mu.RLock() item, found := c.items[key] c.mu.RUnlock() if !found { return nil, false } // 惰性检查过期 if time.Now().UnixNano() > item.Expiration { c.mu.Lock() delete(c.items, key) // 过期则删除 c.mu.Unlock() return nil, false } return item.Value, true } 优势: 避免定期清理遗漏 减少定期清理的遍历次数 及时释放内存
- type Cache struct { items map[string]Item // 直接存储结构体而非指针 // … } type Item struct { Value interface{} Expiration int64 } 优化点: 直接存储结构体减少内存分配 避免指针带来的内存碎片 减少GC压力
- RWMutex允许并发读操作,在缓存这种读多写少的场景下,能显著提升性能。当有活跃的读锁时,写操作会被阻塞,但读操作可以并行执行。
- 设置随机的过期时间偏移 使用单飞模式(singleflight)避免重复请求 实现缓存穿透保护(空值缓存)
- 布隆过滤器过滤无效请求 缓存空值(设置较短TTL) 请求限流
- type LRUCache struct { cache map[string]*list.Element list *list.List capacity int mu sync.Mutex } func (l *LRUCache) Get(key string) (interface{}, bool) { l.mu.Lock() defer l.mu.Unlock() if elem, ok := l.cache[key]; ok { l.list.MoveToFront(elem) return elem.Value.(*Item).Value, true } return nil, false } func (l *LRUCache) Set(key string, value interface{}) { l.mu.Lock() defer l.mu.Unlock() if elem, ok := l.cache[key]; ok { l.list.MoveToFront(elem) elem.Value.(*Item).Value = value return } if len(l.cache) >= l.capacity { // 淘汰最久未使用 elem := l.list.Back() delete(l.cache, elem.Value.(*Item).Key) l.list.Remove(elem) } elem := l.list.PushFront(&Item{Key: key, Value: value}) l.cache[key] = elem }
- UnixNano()返回纳秒级时间戳,相比秒级时间戳: 精度更高,避免短时间内多次操作的时间冲突 比较效率更高(整数比较) 在TTL设置上更精确
目录
- 引言
- 数据结构设计
- 缓存结构核心组件
- 缓存操作流程图
- 关键设计解析
- 1. 并发安全实现
- 2. 过期清理机制
- 3. 过期时间处理
- 使用示例
- 性能优化建议
- 1. 分片缓存(Sharding)
- 2. 惰性删除
- 3. 内存优化
- 常见面试问题
- 1. 为什么使用RWMutex而不是Mutex?
- 2. 如何避免缓存雪崩?
- 3. 如何处理缓存穿透?
- 4. 如何实现LRU淘汰策略?
- 5. 时间为什么使用UnixNano()?
- 总结
在Golang面试中,实现一个并发安全且支持过期清理的缓存结构是常见的高频题目。这类问题不仅考察候选人对Go并发模型的理解,还考察对实际应用场景的把握能力。本文将详细解析如何设计并实现这样一个缓存系统,并提供完整可运行的代码示例。
+------------------+ +-----------------+
| Cache | | Item |
+------------------+ +-----------------+
| - items: map |1 * | - Value: interface{}
| - mu: RWMutex |---------| - Expiration: int64
| - cleanupInterval| +-----------------+
| - stopChan: chan |
+------------------+
结构说明:
Cache 结构体:
items:存储缓存项的映射表mu:读写锁,保证并发安全cleanupInterval:清理过期项的间隔时间stopChan:停止后台清理的信号通道
Item 结构体:
Value:存储的任意类型值Expiration:过期时间戳(纳秒级)
+-------------+
| Set操作 |
+------+------+
|
v
+------+ 获取写锁 +------+
| +----------> |
| 缓存 | | 缓存 |
| 状态 | | 状态 |
| <----------+ |
+------+ 设置值后释放锁 +------+
|
v
+-------------+
| Get操作 |
+------+------+
|
v
+------+ 获取读锁 +------+
| +----------> |
| 缓存 | | 缓存 |
| 状态 | | 状态 |
| <----------+ |
+------+ 读取值后释放锁 +------+
|
v
+-------------+
| 后台清理任务 |
+------+------+
|
v
+------+ 获取写锁 +------+
| +----------> |
| 缓存 | | 缓存 |
| 状态 | 删除过期项 | 状态 |
| <----------+ |
+------+ 释放锁 +------+
使用sync.RWMutex实现读写分离:
- 写操作使用互斥锁(Lock/Unlock)
- 读操作使用读锁(RLock/RUnlock)
- 允许多个读操作并行,提高读密集型场景性能
func (c *Cache) Set(key string, value interface{}, ttl time.Duration) {
c.mu.Lock() // 写操作使用互斥锁
defer c.mu.Unlock()
// ...
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.mu.RLock() // 读操作使用读锁
defer c.mu.RUnlock()
// ...
}
清理策略特点:
- 后台goroutine定期执行清理
- 避免每次读写都检查过期,提高性能
- 清理间隔可配置(默认1分钟)
- 使用通道实现优雅停止
func (c *Cache) startCleanup() {
ticker := time.NewTicker(c.cleanupInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.cleanup() // 定期执行清理
case <-c.stopChan: // 接收停止信号
return
}
}
}
func (c *Cache) cleanup() {
c.mu.Lock()
defer c.mu.Unlock()
now := time.Now().UnixNano()
for key, item := range c.items {
if item.Expiration > 0 && now > item.Expiration {
delete(c.items, key) // 删除过期项
}
}
}
使用纳秒级时间戳存储过期时间:
- 精度高,避免时间精度问题
- 比较时直接使用整数比较,效率高
- 支持永久存储(设置过期时间为0)
expiration := time.Now().Add(ttl).UnixNano()
func main() {
// 创建缓存,每10秒清理一次过期项
cache := cache.NewCache(10 * time.Second)
defer cache.Close() // 程序退出时关闭缓存
// 设置缓存项,5秒过期
cache.Set("key1", "value1", 5*time.Second)
cache.Set("key2", 42, 10*time.Second) // 整数
cache.Set("key3", struct{}{}, 0) // 永久有效
// 立即获取
if val, ok := cache.Get("key1"); ok {
fmt.Println("key1:", val) // 输出: key1: value1
}
// 6秒后获取
time.Sleep(6 * time.Second)
if _, ok := cache.Get("key1"); !ok {
fmt.Println("key1 expired") // 输出: key1 expired
}
// 获取永久项
if _, ok := cache.Get("key3"); ok {
fmt.Println("key3 still exists")
}
// 测试并发读写
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
key := fmt.Sprintf("goroutine_%d", i)
cache.Set(key, i, time.Minute)
if val, ok := cache.Get(key); ok {
_ = val // 使用值
}
}(i)
}
wg.Wait()
fmt.Println("Concurrent test completed")
}
type ShardedCache struct {
shards []*Cache
}
func NewShardedCache(shardCount int, cleanupInterval time.Duration) *ShardedCache {
cache := &ShardedCache{
shards: make([]*Cache, shardCount),
}
for i := range cache.shards {
cache.shards[i] = NewCache(cleanupInterval)
}
return cache
}
func (sc *ShardedCache) getShard(key string) *Cache {
h := fnv.New32a()
h.Write([]byte(key))
return sc.shards[h.Sum32()%uint32(len(sc.shards))]
}
优势:
- 减少锁竞争
- 提高并发性能
- 特别适合高并发场景
func (c *Cache) Get(key string) (interface{}, bool) {
c.mu.RLock()
item, found := c.items[key]
c.mu.RUnlock()
if !found {
return nil, false
}
// 惰性检查过期
if time.Now().UnixNano() > item.Expiration {
c.mu.Lock()
delete(c.items, key) // 过期则删除
c.mu.Unlock()
return nil, false
}
return item.Value, true
}
优势:
- 避免定期清理遗漏
- 减少定期清理的遍历次数
- 及时释放内存
type Cache struct {
items map[string]Item // 直接存储结构体而非指针
// ...
}
type Item struct {
Value interface{}
Expiration int64
}
优化点:
- 直接存储结构体减少内存分配
- 避免指针带来的内存碎片
- 减少GC压力
RWMutex允许并发读操作,在缓存这种读多写少的场景下,能显著提升性能。当有活跃的读锁时,写操作会被阻塞,但读操作可以并行执行。
- 设置随机的过期时间偏移
- 使用单飞模式(singleflight)避免重复请求
- 实现缓存穿透保护(空值缓存)
- 布隆过滤器过滤无效请求
- 缓存空值(设置较短TTL)
- 请求限流
type LRUCache struct {
cache map[string]*list.Element
list *list.List
capacity int
mu sync.Mutex
}
func (l *LRUCache) Get(key string) (interface{}, bool) {
l.mu.Lock()
defer l.mu.Unlock()
if elem, ok := l.cache[key]; ok {
l.list.MoveToFront(elem)
return elem.Value.(*Item).Value, true
}
return nil, false
}
func (l *LRUCache) Set(key string, value interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
if elem, ok := l.cache[key]; ok {
l.list.MoveToFront(elem)
elem.Value.(*Item).Value = value
return
}
if len(l.cache) >= l.capacity {
// 淘汰最久未使用
elem := l.list.Back()
delete(l.cache, elem.Value.(*Item).Key)
l.list.Remove(elem)
}
elem := l.list.PushFront(&Item{Key: key, Value: value})
l.cache[key] = elem
}
UnixNano()返回纳秒级时间戳,相比秒级时间戳:
- 精度更高,避免短时间内多次操作的时间冲突
- 比较效率更高(整数比较)
- 在TTL设置上更精确
实现一个并发安全且支持过期清理的缓存结构需要综合考虑:
- 并发控制:合理使用sync.RWMutex
- 过期处理:结合定期清理和惰性删除
- 内存管理:避免内存泄漏
- 性能优化:分片、内存布局优化等
- 资源释放:优雅停止goroutine
本文实现的缓存结构满足面试题要求,并提供了多种优化思路。在实际应用中,可根据需求添加LRU淘汰、持久化、监控统计等功能。掌握这类并发数据结构的设计思想,对于深入理解Go语言并发模型和解决实际问题至关重要。
面试提示:在回答此类问题时,不仅要展示代码实现,更要解释设计决策背后的思考过程,特别是权衡不同方案时的考虑因素,这能体现你的工程思维深度。
到此这篇关于Golang实现并发安全带过期清理的缓存结构的文章就介绍到这了,更多相关Golang 过期清理缓存结构内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- 批量清理mongodb历史数据的方法详解
- Go prometheus metrics条目自动回收与清理方法
- Go语言Elasticsearch数据清理工具思路详解