beego

beego cache模块源码分析笔记二

2018-12-21  本文已影响0人  ljh123

之前已经分析了外面的那几个文件,现在分析下里面的这几个文件。

image.png
一、memcache源码文件
image.png
这边链接到了mamche包,我前面有一篇博客专门分析了这个包内容链接 image.png

这个目录夹里有两个文件,一个是测试文件。
里面有一个结构体:

type Cache struct {
    conn     *memcache.Client
    conninfo []string
}

这边有一个后面很多方法都会调用的一个方法:

func (rc *Cache) connectInit() error {
    rc.conn = memcache.New(rc.conninfo...)
    return nil
}

设置memcached服务器的地址信息

提供给开发者使用的方法:
1)func NewMemCache

func NewMemCache() cache.Cache {
    return &Cache{}
}

创建一个Cache变量
2)func (rc *Cache) Get(key string) interface{}

func (rc *Cache) Get(key string) interface{} {
    if rc.conn == nil {  // 如果与memcached服务器连接,则断开连接
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    if item, err := rc.conn.Get(key); err == nil {
        return item.Value
    }
    return nil
}

传入键,获取值
3)func (rc *Cache) GetMulti(keys []string) []interface{}

func (rc *Cache) GetMulti(keys []string) []interface{} {
    size := len(keys)
    var rv []interface{}
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            for i := 0; i < size; i++ {
                rv = append(rv, err)
            }
            return rv
        }
    }
    mv, err := rc.conn.GetMulti(keys)
    if err == nil {
        for _, v := range mv {
            rv = append(rv, v.Value)
        }
        return rv
    }
    for i := 0; i < size; i++ {
        rv = append(rv, err)
    }
    return rv
}

传入一组键,获取对应的一组值
4)func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error

func (rc *Cache) Put(key string, val interface{}, timeout time.Duration) error {
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    item := memcache.Item{Key: key, Expiration: int32(timeout / time.Second)}
    if v, ok := val.([]byte); ok {
        item.Value = v
    } else if str, ok := val.(string); ok {
        item.Value = []byte(str)
    } else {
        return errors.New("val only support string and []byte")
    }
    return rc.conn.Set(&item)
}

传入key,val,过期时间来设置一个memcache key/value
5)func (rc *Cache) Delete(key string) error

func (rc *Cache) Delete(key string) error {
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    return rc.conn.Delete(key)
}

删除一个键值
6)func (rc *Cache) Incr(key string) error

func (rc *Cache) Incr(key string) error {
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    _, err := rc.conn.Increment(key, 1)
    return err
}

对应键的值自加一
7)func (rc *Cache) Decr(key string) error

func (rc *Cache) Decr(key string) error {
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    _, err := rc.conn.Decrement(key, 1)
    return err
}

对应健的值减去一
8)func (rc *Cache) IsExist(key string) bool

func (rc *Cache) IsExist(key string) bool {
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return false
        }
    }
    _, err := rc.conn.Get(key)
    return !(err != nil)
}

判断该键是否存在
9)func (rc *Cache) ClearAll() error

func (rc *Cache) ClearAll() error {
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    return rc.conn.FlushAll()
}

清除所有缓存在memcache中的内容。
10)func (rc *Cache) StartAndGC(config string) error

func (rc *Cache) StartAndGC(config string) error {
    var cf map[string]string
    json.Unmarshal([]byte(config), &cf)
    if _, ok := cf["conn"]; !ok {
        return errors.New("config has no conn key")
    }
    rc.conninfo = strings.Split(cf["conn"], ";")
    if rc.conn == nil {
        if err := rc.connectInit(); err != nil {
            return err
        }
    }
    return nil
}

在没有进行连接的时候,以配置信息创建memcached服务器连接,如果出错,就返回错误

以上内容就是这个文件里的全部内容,对比memcached包,会发现这个文件是对memcached的简单封装。

上一篇 下一篇

猜你喜欢

热点阅读