项目作者: MoeYang

项目描述 :
A mem-cached library by golang.
高级语言: Go
项目地址: git://github.com/MoeYang/go-localcache.git
创建时间: 2021-03-05T09:07:55Z
项目社区:https://github.com/MoeYang/go-localcache

开源协议:MIT License

下载


A mem-cached library by golang.

Why choose go-localcache ?

1、Support to set different TTL for every key

2、LRU policy to delete useless keys

3、Similar performance like sync.Map -> see bench_test

How to use

  1. cache := localcache.NewLocalCache(
  2. localcache.WithCapacity(1024), // WithShardCount set max Capacity
  3. localcache.WithShardCount(256),// WithShardCount shardCnt must be a power of 2
  4. localcache.WithGlobalTTL(120), // WithGlobalTTL set all keys default expire time of seconds
  5. localcache.WithStatist(true), // WithStatist set whether need to caculate the cache stastic
  6. localcache.WithPolicy(localcache.PolicyTypeLRU), // WithPolicy set the elimination policy of key
  7. )
  8. // Get a key and return the value and if the key exists
  9. cache.Get(key string) (interface{}, bool)
  10. // GetOrLoad get a key, while key not exists, call f() to load data, and will set the load data to cache.
  11. // Load data process will called singleFlight called
  12. cache.GetOrLoad(key string, f LoadFunc) (interface{}, error)
  13. // Set a key-value with default seconds to live
  14. cache.Set(key string, value interface{})
  15. // SetWithExpire set a key-value with seconds to live
  16. cache.SetWithExpire(key string, value interface{}, ttl int64)
  17. // Del delete key and return if the key exists
  18. cache.Del(key string) bool
  19. // Len return count of keys in cache
  20. cache.Len() int
  21. // Flush clear all keys in chache, should do this when set and del is stop
  22. cache.Flush()
  23. // Stop the cacheProcess by close stopChan
  24. cache.Stop()
  25. // Statistic return cache Statics {"hit":1, "miss":1, "hitRate":50.0}
  26. Statistic() map[string]interface{}