Go-Cache Source Analysis

一、简介

基于内存的K/V存储/缓存:(类似Memcacheed),适用于单机应用程序,支持删除,过期,默认Cache共享锁

主要优点:本质上是一个map[string]interface{}具有过期时间的安全线程,它不需要序列化或通过网络传输其内容。

大量Key的情况下会造成锁竞争严重。

go-cache可以存储任何对象(在给定的持续时间内或永久存储),并且缓存可以由多个goroutine安全地使用。

虽然go-cache并不打算用作持久性数据存储,但整个缓存可以保存到文件中并从文件中加载(c.Items()用于检索条目映射以进行序列化,NewFrom()从反序列化的缓存中创建一个缓存)快速从停机时间恢复

二、示例

import (
	"fmt"
	"github.com/patrickmn/go-cache"
	"time"
)

type MyStruct struct {
	Age int
}

func main() {
    
        // 设置超时时间、清理时间
	// Create a cache with a default expiration time of 5 minutes, and which
	// purges expired items every 10 minutes
	c := cache.New(5*time.Minute, 10*time.Minute)
    
        // 设置缓存值,默认过期时间:永久不过期
	// Set the value of the key "foo" to "bar", with the default expiration time
	c.Set("foo", "bar", cache.DefaultExpiration)
    
	// 设置没有过期时间的KEY,这个KEY不会被自动清除,清除使用:c.Delete("baz")
	// Set the value of the key "baz" to 42, with no expiration time
	// (the item won't be removed until it is re-set, or removed using
	// c.Delete("baz")
	c.Set("baz", 42, cache.NoExpiration)

        // 获取值, 并断言
	// This gets tedious if the value is used several times in the same function.
	// You might do either of the following instead:
	if x, found := c.Get("foo"); found {
		foo := x.(string)
		// ...
	}

        // 对结构体指针进行操作
	// Want performance? Store pointers!
	c.Set("foo", &MyStruct, cache.DefaultExpiration)
	if x, found := c.Get("foo"); found {
		foo := x.(*MyStruct)
			// ...
	}
}

三、源码分析

package cache

import (
	"encoding/gob"
	"fmt"
	"io"
	"os"
	"runtime"
	"sync"
	"time"
)

// Key对应的Item
type Item struct {
	Object     interface{}	//value
	Expiration int64        //过期时间戳:设置的时间+缓存时长
}

// 判断是否过期
func (item Item) Expired() bool {
	if item.Expiration == 0 {
		return false
	}
	return time.Now().UnixNano() > item.Expiration
}

const (
	// 永不过期
	NoExpiration time.Duration = -1
        // 默认时间
	DefaultExpiration time.Duration = 0
)

type Cache struct {
	*cache
	// If this is confusing, see the comment at the bottom of New()
}

type cache struct {
	defaultExpiration time.Duration				// item默认过期时间
	items             map[string]Item			// 一个cache实例内存中多个Item
	mu                sync.RWMutex  			// 读写锁
	onEvicted         func(string, interface{})	        // 删除Key时的CallBack函数
	janitor           *janitor			        // 定时清理回收,检查过期的Item,并调用c.Delete()
}

//添加key对应的Item存储到cache,如果key存在则覆盖,如果d为0,使用默认时间,d为-1,永不过期
func (c *cache) Set(k string, x interface{}, d time.Duration) {
	// "Inlining" of set
	var e int64
	if d == DefaultExpiration {
		d = c.defaultExpiration
	}
	if d > 0 {
		e = time.Now().Add(d).UnixNano()
	}
	c.mu.Lock()
	c.items[k] = Item{
		Object:     x,	// 实际数据
		Expiration: e,	// 过期时间
	}
	// TODO: Calls to mu.Unlock are currently not deferred because defer
	// adds ~200 ns (as of go1.)
	c.mu.Unlock()
}

//与上述一样,不带锁
func (c *cache) set(k string, x interface{}, d time.Duration) {
	var e int64
	if d == DefaultExpiration {
		d = c.defaultExpiration
	}
	if d > 0 {
		e = time.Now().Add(d).UnixNano()
	}
	c.items[k] = Item{
		Object:     x,
		Expiration: e,
	}
}

// Set缓存使用默认过期时间
func (c *cache) SetDefault(k string, x interface{}) {
	c.Set(k, x, DefaultExpiration)
}

// key对应的Item不存在或已过期,添加Item到cache中,否则error
func (c *cache) Add(k string, x interface{}, d time.Duration) error {
	c.mu.Lock()
	_, found := c.get(k)
	if found {
		c.mu.Unlock()
		return fmt.Errorf("Item %s already exists", k)
	}
	c.set(k, x, d)
	c.mu.Unlock()
	return nil
}

// 如果key已存在且对应的Item未过期,可设置new value,否则error
func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
	c.mu.Lock()
	_, found := c.get(k)
	if !found {
		c.mu.Unlock()
		return fmt.Errorf("Item %s doesn't exist", k)
	}
	c.set(k, x, d)
	c.mu.Unlock()
	return nil
}

// 从缓存中获取key对应的Item,否则返回nil,bool表示返回是否存在
func (c *cache) Get(k string) (interface{}, bool) {
	c.mu.RLock()	                // 加锁,限制并发读写
	// "Inlining" of get and Expired
	item, found := c.items[k]	// 查询cache中k的Item
	if !found {
		c.mu.RUnlock()
		return nil, false
	}
	if item.Expiration > 0 {
		if time.Now().UnixNano() > item.Expiration {	//已过期返回nil
			c.mu.RUnlock()
			return nil, false
		}
	}
	c.mu.RUnlock()
	return item.Object, true
}

// 从缓存中获取key对应的Item的过期时间,bool表示返回是否存在
func (c *cache) GetWithExpiration(k string) (interface{}, time.Time, bool) {
	c.mu.RLock()
	// "Inlining" of get and Expired
	item, found := c.items[k]
	if !found {
		c.mu.RUnlock()
		return nil, time.Time{}, false
	}

	if item.Expiration > 0 {
		if time.Now().UnixNano() > item.Expiration {
			c.mu.RUnlock()
			return nil, time.Time{}, false
		}

		// Return the item and the expiration time
		c.mu.RUnlock()
		return item.Object, time.Unix(0, item.Expiration), true
	}
	//
	// If expiration <= 0 (i.e. no expiration time set) then return the item
	// and a zeroed time.Time
	c.mu.RUnlock()
	return item.Object, time.Time{}, true
}

func (c *cache) get(k string) (interface{}, bool) {
	item, found := c.items[k]
	if !found {
		return nil, false
	}
	// "Inlining" of Expired
	if item.Expiration > 0 {
		if time.Now().UnixNano() > item.Expiration {
			return nil, false
		}
	}
	return item.Object, true
}

// 增加操作,针对不同的类型进行适配
func (c *cache) Increment(k string, n int64) error {
	c.mu.Lock()
	v, found := c.items[k]
	if !found || v.Expired() {
		c.mu.Unlock()
		return fmt.Errorf("Item %s not found", k)
	}
	switch v.Object.(type) {
	case int:
		v.Object = v.Object.(int) + int(n)
	case int8:
		v.Object = v.Object.(int8) + int8(n)
	case int16:
		v.Object = v.Object.(int16) + int16(n)
	case int32:
		v.Object = v.Object.(int32) + int32(n)
	case int64:
		v.Object = v.Object.(int64) + n
	case uint:
		v.Object = v.Object.(uint) + uint(n)
	case uintptr:
		v.Object = v.Object.(uintptr) + uintptr(n)
	case uint8:
		v.Object = v.Object.(uint8) + uint8(n)
	case uint16:
		v.Object = v.Object.(uint16) + uint16(n)
	case uint32:
		v.Object = v.Object.(uint32) + uint32(n)
	case uint64:
		v.Object = v.Object.(uint64) + uint64(n)
	case float32:
		v.Object = v.Object.(float32) + float32(n)
	case float64:
		v.Object = v.Object.(float64) + float64(n)
	default:
		c.mu.Unlock()
		return fmt.Errorf("The value for %s is not an integer", k)
	}
	c.items[k] = v
	c.mu.Unlock()
	return nil
}

... ... 省略部分代码(针对不同类型代码操作)

// 删除Item
func (c *cache) Delete(k string) {
	c.mu.Lock()
	v, evicted := c.delete(k)
	c.mu.Unlock()
	if evicted {
		c.onEvicted(k, v) // 删除k的CallBack
	}
}

// 判断删除Item触发的Callback是否为nil,不为nil删除存在的Item,并返回value,反之直接删除Item
func (c *cache) delete(k string) (interface{}, bool) {
	if c.onEvicted != nil {
		if v, found := c.items[k]; found {
			delete(c.items, k)
			return v.Object, true
		}
	}
	delete(c.items, k)
	return nil, false
}

type keyAndValue struct {
	key   string
	value interface{}
}

// 删除缓存中所有过期的Item
func (c *cache) DeleteExpired() {
	var evictedItems []keyAndValue
	now := time.Now().UnixNano()
	c.mu.Lock()
	for k, v := range c.items {	// 加锁遍历整个Items列表
		// "Inlining" of expired
		if v.Expiration > 0 && now > v.Expiration {
			ov, evicted := c.delete(k)
			if evicted {
				evictedItems = append(evictedItems, keyAndValue{k, ov})
			}
		}
	}
	c.mu.Unlock()
	for _, v := range evictedItems {
		c.onEvicted(v.key, v.value)
	}
}

// 设置删除Item时的回调函数
func (c *cache) OnEvicted(f func(string, interface{})) {
	c.mu.Lock()
	c.onEvicted = f
	c.mu.Unlock()
}

// 使用gob(序列化编/解码工具)编码保存
func (c *cache) Save(w io.Writer) (err error) {
	enc := gob.NewEncoder(w)
	defer func() {
		if x := recover(); x != nil {
			err = fmt.Errorf("Error registering item types with Gob library")
		}
	}()
	c.mu.RLock()
	defer c.mu.RUnlock()
	for _, v := range c.items {
		gob.Register(v.Object)
	}
	err = enc.Encode(&c.items)
	return
}

// 把cache中的数据使用gob编码保存到文件中
func (c *cache) SaveFile(fname string) error {
	fp, err := os.Create(fname)
	if err != nil {
		return err
	}
	err = c.Save(fp)
	if err != nil {
		fp.Close()
		return err
	}
	return fp.Close()
}

// Add (Gob-serialized) cache items from an io.Reader, excluding any items with
// keys that already exist (and haven't expired) in the current cache.
//
// NOTE: This method is deprecated in favor of c.Items() and NewFrom() (see the
// documentation for NewFrom().)
func (c *cache) Load(r io.Reader) error {
	dec := gob.NewDecoder(r)
	items := map[string]Item{}
	err := dec.Decode(&items)
	if err == nil {
		c.mu.Lock()
		defer c.mu.Unlock()
		for k, v := range items {
			ov, found := c.items[k]
			if !found || ov.Expired() {
				c.items[k] = v
			}
		}
	}
	return err
}

// 把文件中的内容 编码存到cache中
func (c *cache) LoadFile(fname string) error {
	fp, err := os.Open(fname)
	if err != nil {
		return err
	}
	err = c.Load(fp)
	if err != nil {
		fp.Close()
		return err
	}
	return fp.Close()
}

// 获取cache中所有未过期的item
func (c *cache) Items() map[string]Item {
	c.mu.RLock()
	defer c.mu.RUnlock()
	m := make(map[string]Item, len(c.items))
	now := time.Now().UnixNano()
	for k, v := range c.items {
		// "Inlining" of Expired
		if v.Expiration > 0 {
			if now > v.Expiration {
				continue
			}
		}
		m[k] = v
	}
	return m
}

// Returns the number of items in the cache. This may include items that have
// expired, but have not yet been cleaned up.
func (c *cache) ItemCount() int {
	c.mu.RLock()
	n := len(c.items)
	c.mu.RUnlock()
	return n
}

// 清空cache
func (c *cache) Flush() {
	c.mu.Lock()
	c.items = map[string]Item{}
	c.mu.Unlock()
}

//定时清空缓存结构
type janitor struct {
	Interval time.Duration		// 回收间隔时长
	stop     chan bool		// 是否停止
}

// 定时调用删除函数,设置一个Interval
func (j *janitor) Run(c *cache) {
	ticker := time.NewTicker(j.Interval)
	for {
		select {
		case <-ticker.C: 		// 每到一个周期就全部遍历一次
			c.DeleteExpired()	// 实际的删除逻辑
		case <-j.stop:
			ticker.Stop()
			return
		}
	}
}

// 停止清除
func stopJanitor(c *Cache) {
	c.janitor.stop <- true
}

// 设置一个goroutine定时清理
func runJanitor(c *cache, ci time.Duration) {
	j := &janitor{
		Interval: ci,
		stop:     make(chan bool),
	}
	c.janitor = j
	go j.Run(c)     // 新的协程做过期删除逻辑
}

func newCache(de time.Duration, m map[string]Item) *cache {
    // 永不过期
	if de == 0 {
		de = -1
	}
	c := &cache{
		defaultExpiration: de,
		items:             m,
	}
	return c
}

// 定期清理cache中过期的Item
func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item) *Cache {
	c := newCache(de, m)
	C := &Cache{c}
	if ci > 0 {
		runJanitor(c, ci)	                // 定时清理过期的key
                // C被垃圾回收时,确保c也能被回收,回收时把c.janitor所在的goroutine停掉,这样c才能被回收
                // runtime.SetFinalizer(obj,func(obj *typeObj))
                // golang提供runtime.SetFinalizer函数,当GC准备释放对象时,会回调该函数指定的方法
		runtime.SetFinalizer(C, stopJanitor)    //当C被GC回收时,停止runJanitor中的协程
                // 对象可以关联一个SetFinalizer函数, 当gc检测到unreachable对象有关联的SetFinalizer函数时,会执行关联的SetFinalizer函数, 同时取消关联。 当下一次gc的时候,对象重新处于unreachable状态并且没有SetFinalizer关联, 就会被回收
	}
	return C
}

// 传入cache的默认过期时间和定期清除时间
func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
	items := make(map[string]Item)
	return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}

//创建实例的同时把现有的items存储到cache中
func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]Item) *Cache {
	return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}

go-cache Doc

posted @ 2021-08-23 15:11  自己有自己的调调、  阅读(220)  评论(0编辑  收藏  举报