gocache 源码解读

背景

go-cache 是一款类似memcache 的缓存,他比较适用于单机执行的应用程序。源码地址: github.com/patrickmn/g…

主要功能

  1. 主要存储 map[string]interface{} 的对象

  2. 提供协程安全,过期时间的功能

  3. 提供将所有键值永久存储到文件中的功能。

提供的主要函数

  1. cache.New(save_time,delete_time )这里的 save_time 是默认的存活时间,delete_time 是扫描一次内存删除的时间。 创建一个cache 对象
  2. c.Set(“foo”,"bar",cache.DefaultExpiration)设置key,value,然后设置国过期时间
  3. c.Get("foo") 这个不必多说

过期机制

go-cache 使用了goroutine 执行自动过期清理,整个都不用了会被删除,但是还有后台清理goroutine。

cache.go

首先是一个 struct Item

type Item struct {
	// 这里就是value
    Object     interface{}
    // 这是设置的过期时间
	Expiration int64
}

// 判断是否过期
func (item Item) Expired() bool;
复制代码

然后是const定义两个

const (
	// For use with functions that take an expiration time.
	NoExpiration time.Duration = -1
	// For use with functions that take an expiration time. Equivalent to
	// passing in the same expiration duration as was given to New() or
	// NewFrom() when the cache was created (e.g. 5 minutes.)
	DefaultExpiration time.Duration = 0
)
复制代码

然后就是核心 cache 结构体

type cache struct {
    //应该是默认的删除时间
	defaultExpiration time.Duration
	items             map[string]Item
	mu                sync.RWMutex
	// 不知道是啥东西
    onEvicted         func(string, interface{})
	//不知道是啥东西
    janitor           *janitor
}
复制代码

后续是添加条目item的函数

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()
    //这里就是设置新的k/v
	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()
}
//这个set就是单机使用
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,
	}
}

//这个add是因为要键值不存在和过期才能set,不然返回错误。
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()
	//然后返回nil ,就是没错误
    return nil
}
// 这是Replace 设置新的value ,就是在没过期的情况下
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
}

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

//然后也是一个不是线程安全的get
func (c *cache) get(k string) (interface{}, bool);
//这个是递增n的一个函数,如果不是整数或者找不到或者无法将其递增n,就返回错误,
//就是会变大
func (c *cache) Increment(k string, n int64) error {
    //上锁
	c.mu.Lock()
	// 查k 在不在 c.items中
    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
}

复制代码

有增加就会有减少

//这个是减少的
func (c *cache) Decrement(k string, n int64) error {
	// TODO: Implement Increment and Decrement more cleanly.
	// (Cannot do Increment(k, n*-1) for uints.)
	c.mu.Lock()
	v, found := c.items[k]
	if !found || v.Expired() {
		c.mu.Unlock()
		return fmt.Errorf("Item not found")
	}
	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
}
复制代码

这个Decrement和Increment的功能类似

然后就是删除的原理

怎删改查

//这里是用于删除c.itemes
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
}
复制代码

然后是超时数据的处理工作

//先是一个结构体,应该就是kv结构
type keyAndValue struct {
	key   string
	value interface{}
}
//这个应该就是设置线程安全之类
func (c *cache) OnEvicted(f func(string, interface{})) {
	c.mu.Lock()
	c.onEvicted = f
	c.mu.Unlock()
}

// 这里就是删除所有过期kv的函数
func (c *cache) DeleteExpired() {
	var evictedItems []keyAndValue
	now := time.Now().UnixNano()
	c.mu.Lock()
	for k, v := range c.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)
	}
}
复制代码

后面就是一些保存和和载入的api,这里是使用了 import 中的encoding/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
}
//这个就是和拿到句柄然后save
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()
}
复制代码

然后是load的两个api。

后续的api

//这个items 是为了复制所有还没过期的items
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
}
复制代码

然后后面是看门狗(janitor)结构体

//这个应该是后台清理回收goroutine用的结构体
type janitor struct {
	Interval time.Duration
	stop     chan bool
}
复制代码

然后看看他们的函数

//这个run感觉是后台gorutione定时清理的作用
//这个定时要不要清理其实是靠这个channel 来实现的
func (j *janitor) Run(c *cache) {
	ticker := time.NewTicker(j.Interval)
	for {
		select {
		case <-ticker.C:
			c.DeleteExpired()
		case <-j.stop:
			ticker.Stop()
			return
		}
	}
}
//这个是靠这个channel 看这一次要不要删除过期键
func stopJanitor(c *Cache) {
	c.janitor.stop <- true
}

//这里应该是启动的时候开启一个后台清理的gorotine
func runJanitor(c *cache, ci time.Duration) {
	j := &janitor{
		Interval: ci,
		stop:     make(chan bool),
	}
	c.janitor = j
	go j.Run(c)
}


复制代码

这是后台清理go程的代码。

后面是一些创建的代码

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

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)
		runtime.SetFinalizer(C, stopJanitor)
	}
	return C
}

复制代码

最后New 函数

//
func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
	items := make(map[string]Item)
	return newCacheWithJanitor(defaultExpiration, cleanupInterval, items)
}
复制代码