BigCache是用于在Go中写入千兆字节数据的高效缓存。快速,并发,逐行扫描内存缓存,以保持大量条目,而不影响性能。BigCache在堆上保留条目,但为它们省略了GC。要实现对字节数组的操作,因此在大多数用例中将需要在高速缓存前面进行条目(de)序列化。
使用简单初始化
import "github.com/allegro/bigcache"cache, _ := bigcache.NewBigCache(bigcache.DefaultCofig(10 * time.Miute))cache.Set("my-uique-key", []byte("value"))etry, _ := cache.Get("my-uique-key")fmt.Pritl(strig(etry))自定义初始化
import ("log""github.com/allegro/bigcache")cofig := bigcache.Cofig {// umber of shards (must be a power of 2)Shards: 1024,// time after which etry ca be evictedLifeWidow: 10 * time.Miute,// rps * lifeWidow, used oly i iitial memory allocatioMaxEtriesIWidow: 1000 * 10 * 60,// max etry size i bytes, used oly i iitial memory allocatioMaxEtrySize: 500,// prits iformatio about additioal memory allocatioVerbose: true,// cache will ot allocate more memory tha this limit, value i MB// if value is reached the the oldest etries ca be overridde for the ew oes// 0 value meas o size limitHardMaxCacheSize: 8192,// callback fired whe the oldest etry is removed because of its// expiratio time or o space left for the ew etry. Default value is il which// meas o callback ad it prevets from uwrappig the oldest etry.ORemove: il,}cache, iitErr := bigcache.NewBigCache(cofig)if iitErr != il {log.Fatal(iitErr)}cache.Set("my-uique-key", []byte("value"))if etry, err := cache.Get("my-uique-key"); err == il {fmt.Pritl(strig(etry))}










评论