欢迎来看!

Andelu

事无繁琐,在于人

Go实践:用Sync.Map实现简易内存缓存系统

介绍

定义了一个Cache结构体,其中使用sync.Map作为底层数据结构来存储缓存项。Set方法用于设置缓存项,指定键、值以及过期时间。Get方法用于获取缓存项,如果缓存项存在且未过期,则返回值和true,否则返回nil和false。方法的接受者为指针类型,是为了对Cache对象进行操作,并在方法内部访问和修改对象的数据。
在main函数中,创建了一个Cache实例,并设置了两个缓存项。然后,我们尝试获取一个存在的缓存项,并在控制台上打印值。接着,我们等待6秒,再次尝试获取同一个缓存项,这次应该返回Key not found,因为该缓存项已过期。

代码

GoPratice/memcache/v0/simple.go at main · honlu/GoPratice

package main

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

type Cache struct {
	data sync.Map
}

type cacheItem struct {
	value   interface{} // 值
	expired time.Time   // 过期时间
}

// Set 指针类型的方法接受者,才可以保证在方法内部访问和修改对象的数据。
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
	c.data.Store(key, cacheItem{
		value:   value,
		expired: time.Now().Add(duration),
	})
}

func (c *Cache) Get(key string) (interface{}, bool) {
	item, ok := c.data.Load(key)
	if !ok {
		return nil, false
	}

	cacheItem := item.(cacheItem)
	if time.Now().After(cacheItem.expired) { // 判断是否过期
		c.data.Delete(key)
		return nil, false
	}

	return cacheItem.value, true
}

func main() {
	cache := &Cache{}

	cache.Set("key1", "value1", 5*time.Second)
	cache.Set("key2", "value2", 10*time.Second)

	value1, found1 := cache.Get("key1")
	if found1 {
		fmt.Printf("Value1:%v\n", value1)
	} else {
		fmt.Printf("Key1 not found\n")
	}

	value2, found2 := cache.Get("key2")
	if found2 {
		fmt.Printf("Value2:%v\n", value2)
	} else {
		fmt.Printf("Key2 not found\n")
	}

	time.Sleep(6 * time.Second)

	value1, found1 = cache.Get("key1")
	if found1 {
		fmt.Printf("Value1:%v\n", value1)
	} else {
		fmt.Printf("Key1 not found\n")
	}
	value2, found2 = cache.Get("key2")
	if found2 {
		fmt.Printf("Value2:%v\n", value2)
	} else {
		fmt.Printf("Key2 not found\n")
	}
}

后续

可以考虑具体需求进行扩展和优化,例如添加缓存淘汰策略、高并发等。

备注:

内容首发在个人知乎文章, 源码在Github.

posted @ 2024-04-14 21:21  轻荇  阅读(42)  评论(0编辑  收藏  举报