TOC

Golang KV 缓存实现

后期优化:

  1. items 按照链表的方式组织起来,按过期时间排序,加快清理速度。
    或者另外设计一个数据结构,存储缓存过期时间。
  2. 放弃每次 Get 的时候判断是否过期
package main

import (
    "fmt"
    "sync"
    "time"
)

type CacheItem struct {
    key        string
    value      interface{}
    expiration time.Time
}

type Cache struct {
    items           map[string]CacheItem // 缓存项
    mu              sync.Mutex           // 锁保护并发读写
    cleanupInterval time.Duration        // 清理缓存的时间间隔
}

// 创建一个新的缓存实例
func NewCache(cleanupInterval time.Duration) *Cache {
    c := &Cache{
        items:           make(map[string]CacheItem),
        cleanupInterval: cleanupInterval,
    }
    // 启动后台清理任务
    go c.startCleanup()
    return c
}

// 启动后台定期清理过期缓存的任务
func (c *Cache) startCleanup() {
    ticker := time.NewTicker(c.cleanupInterval)
    for {
        <-ticker.C
        c.cleanUpExpiredItems()
    }
}

// 清理过期缓存项
func (c *Cache) cleanUpExpiredItems() {
    c.mu.Lock()
    defer c.mu.Unlock()

    // 清理有过期时间的缓存项
    for key, item := range c.items {
        if time.Now().After(item.expiration) {
            delete(c.items, key)
        }
    }

    // 打印清理日志(可以根据需要调整)
    fmt.Println("Expired cache items cleaned up")
}

// 添加缓存项,支持过期时间
func (c *Cache) Set(key string, value interface{}, expiration time.Time) {
    c.mu.Lock()
    defer c.mu.Unlock()

    c.items[key] = CacheItem{key: key, value: value, expiration: expiration}
}

// 获取缓存项
func (c *Cache) Get(key string) (interface{}, bool) {
    c.mu.Lock()
    defer c.mu.Unlock()

    // 再检查有过期时间的缓存项
    if item, found := c.items[key]; found {
        // if item.expiration.IsZero() || time.Now().Before(item.expiration) {
        //  return item.value, true
        // }
        // // 如果缓存项已过期,则删除并返回未找到
        // delete(c.items, key)
        return item.value, true
    }
    return nil, false
}

// 删除缓存项
func (c *Cache) Delete(key string) {
    c.mu.Lock()
    defer c.mu.Unlock()

    delete(c.items, key)
}

func main() {
    // 创建一个缓存实例,每 1 秒清理一次过期缓存
    cache := NewCache(1 * time.Second)

    // 设置缓存项(带过期时间)
    cache.Set("key1", "value1", time.Now().Add(5*time.Second))

    // 设置没有过期时间的缓存项
    cache.Set("key2", "value2", time.Time{}) // 空时间表示永不过期

    // 获取缓存项
    if value, found := cache.Get("key1"); found {
        fmt.Println("Found key1:", value)
    } else {
        fmt.Println("key1 not found")
    }

    if value, found := cache.Get("key2"); found {
        fmt.Println("Found key2:", value)
    } else {
        fmt.Println("key2 not found")
    }

    // 等待 6 秒后,key1 会过期
    time.Sleep(6 * time.Second)

    // 再次获取缓存项
    if value, found := cache.Get("key1"); found {
        fmt.Println("Found key1:", value)
    } else {
        fmt.Println("key1 not found (after expiration)")
    }

    if value, found := cache.Get("key2"); found {
        fmt.Println("Found key2:", value)
    } else {
        fmt.Println("key2 not found")
    }

    // 让清理任务继续运行
    select {}
}
如果你有魔法,你可以看到一个评论框~