19 Star 123 Fork 0

kelvins-io/kelvins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
conn_cache.go 1.05 KB
一键复制 编辑 原始数据 按行查看 历史
雨化田 提交于 2021-08-20 20:08 . conn_cache fix
package client_conn
import (
"errors"
"fmt"
"github.com/bluele/gcache"
"google.golang.org/grpc"
"math"
"time"
)
var (
internalCache gcache.Cache
internalNotFound = errors.New("object not found")
)
const (
internalCachePrefix = "kelvins-%v"
internalCacheExpire = 24 * time.Hour
)
func init() {
internalCache = gcache.New(math.MaxInt16).LRU().Build()
}
func getRPCConn(serviceName string) (conn *grpc.ClientConn, err error) {
key := genInternalCacheKey(serviceName)
exist := internalCache.Has(key)
if exist {
var value interface{}
value, err = internalCache.Get(key)
if err != nil {
return
}
if value == nil {
err = internalNotFound
return
}
connCache, ok := value.(*grpc.ClientConn)
if ok {
conn = connCache
return
}
}
err = internalNotFound
return
}
func storageRPCConn(serviceName string, conn *grpc.ClientConn) error {
key := genInternalCacheKey(serviceName)
return internalCache.Set(key, conn)
}
func genInternalCacheKey(serviceName string) string {
return fmt.Sprintf(internalCachePrefix, serviceName)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kelvins-io/kelvins.git
git@gitee.com:kelvins-io/kelvins.git
kelvins-io
kelvins
kelvins
v1.5.8

搜索帮助