beegofreecache

package module
v0.0.0-...-17dae30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2017 License: GPL-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

#beegofreecache

什么是beegofreecache

fatal error: concurrent map iteration and map write

更重要的是memory使用的是同步锁来确保数据一致性,这必然后导致性能有所损耗。 如果有熟悉Java的同学应该知道Map。HashMap中未进行同步考虑,而Hashtable则使用了synchronized,带来的直接影响就是可选择,我们可以在单线程时使用HashMap提高效率,而多线程时用Hashtable来保证安全。 beego内置的memory实现就相当于Hashtable。
所以我们需要一个更好的memory的实现,经过测试决定使用freecache,大家可以去freecache主页查看详细信息。 freecache就相当于Java中的ConcurrentHashMap,性能也有很大的提高,官网上它的性能测试报告,在此不赘述了。

但是freecache也会有一些缺点:

  1. 当需要缓存的数据占用超过提前分配缓存的 1/1024 则不能缓存
  2. 当分配内存过多则会导致内存占用高
  3. key的长度需要小于65535字节
  4. value只能是[]byte类型,使用很不友好

使用方法

为此我对freecache进行了简单的封装,让freecache可以集成搭配beego框架中,也可以不在beego框架中独立使用。

安装

go get  git.oschina.net/qiangmzsx/beegofreecache
或
go get github.com/qiangmzsx/beegofreecache

导入包

import git.oschina.net/qiangmzsx/beegofreecache

集成到beego框架:

与beego使用其他的cache一样。

var bm, err = cache.NewCache("beegofreecache", `{"size":64}`)
bm.Put("beegofreecache", 1, 10*time.Second)
bm.Get("beegofreecache")
bm.IsExist("beegofreecache")
bm.Delete("beegofreecache")

注意:Get()函数返回的是[]byte这个需要业务使用beegofreecache内置GobDecode()转码

var bm, err = cache.NewCache("beegofreecache", `{"size":64}`)
func TestBeeFreeCacheGP(t *testing.T)  {

    if err!=nil {
        println(err.Error())
    }
    key:="key"
    bm.Put(key,1200,10*time.Second)
    val:=bm.Get(key)
    var ii int
    GobDecode(val.([]byte),&ii)
    println(val,ii)

}

独立使用

不依赖beego框架。

bm:=beegofreecache.NewFree(512)//size为M
bm.Put("beegofreecache", 1, 10*time.Second)
var ii int
bm.GetValue("beegofreecache",ii)
bm.IsExist("beegofreecache")
bm.Delete("beegofreecache")

转化失败

在使用中如果出现了

gob: type not registered for interface

别着急,可以在init()中加入:

gob.Register(map[string]interface{}{})
gob.Register(map[string][]int{})
gob.Register(map[string][]int64{})

其中Register函数中的参数就是需要转化的类型了。 欢迎大家提供修改意见。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultSize = 64 //64M

)

Functions

func GobDecode

func GobDecode(data []byte, to interface{}) error

Gob反序列化

func GobEncode

func GobEncode(data interface{}) ([]byte, error)

Gob序列化

func NewFreeRegister

func NewFreeRegister() cache.Cache

该函数是为了集成到beego框架中

Types

type Cache

type Cache struct {
	Free *freecache.Cache
}

func NewFree

func NewFree(m int) *Cache

该函数是可以在任意地方使用,初始化Free进程缓存

func (*Cache) CacheStatus

func (free *Cache) CacheStatus() map[string]interface{}

获取cache状态

func (*Cache) ClearAll

func (free *Cache) ClearAll() error

清出所有缓存

func (*Cache) Decr

func (free *Cache) Decr(key string) error

对key值为int64的减1

func (*Cache) Delete

func (free *Cache) Delete(key string) error

删除key

func (*Cache) Get

func (free *Cache) Get(key string) interface{}

根据key获取对应的value,如果不是在beego框架中,建议使用GetValue

func (*Cache) GetMulti

func (free *Cache) GetMulti(keys []string) []interface{}

批量获取keys

func (*Cache) GetValue

func (free *Cache) GetValue(key string, value interface{}) error

推荐使用,但是beego框架不支持

func (*Cache) Incr

func (free *Cache) Incr(key string) error

对key值为int64的加1

func (*Cache) IsExist

func (free *Cache) IsExist(key string) bool

判断指定key是否存在

func (*Cache) Put

func (free *Cache) Put(key string, val interface{}, timeout time.Duration) error

设置缓存

func (*Cache) StartAndGC

func (cache *Cache) StartAndGC(config string) error

在beego框架中,注册时会自动执行该函数初始化

func (*Cache) String

func (free *Cache) String() string

输出cache状态

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址