37 Star 390 Fork 118

联犀/物联网iot模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
scriptRepo.go 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-10-10 22:13 . feat: 更新mod
package cache
import (
"context"
"fmt"
"gitee.com/unitedrhino/share/devices"
"gitee.com/unitedrhino/share/errors"
"gitee.com/unitedrhino/things/service/dmsvr/internal/domain/productCustom"
"gitee.com/unitedrhino/things/service/dmsvr/internal/repo/relationDB"
"github.com/dgraph-io/ristretto"
"github.com/dop251/goja"
"sync"
"time"
)
const (
expireTime = time.Hour
)
type (
ScriptRepo struct {
cache *ristretto.Cache
}
)
type CustomCacheStu struct {
LoginAuthPool *sync.Pool
Topics []*productCustom.CustomTopic
}
func NewScriptRepo() *ScriptRepo {
cache, _ := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
return &ScriptRepo{
cache: cache,
}
}
func (t *ScriptRepo) GetCustomTopic(ctx context.Context, productID string) (topics []*productCustom.CustomTopic, err error) {
c, err := t.getCache(ctx, productID)
return c.Topics, err
}
func (t ScriptRepo) GetTransFormFunc(ctx context.Context, productID string) (productCustom.LoginAuthFunc, error) {
c, err := t.getCache(ctx, productID)
if err != nil || c == nil {
return nil, err
}
vm := c.LoginAuthPool.Get().(*goja.Runtime)
f, ok := goja.AssertFunction(vm.Get(productCustom.LoginAuthFuncName))
if !ok {
return nil, errors.Parameter.AddDetail("未找到函数:" + productCustom.LoginAuthFuncName)
}
return func(dir devices.Direction, clientID string, userName string, password string) (*devices.Core, error) {
ret, err := f(goja.Undefined(), vm.ToValue(dir), vm.ToValue(clientID), vm.ToValue(userName), vm.ToValue(password))
if err != nil {
return nil, err
}
v, err := ret.ToObject(vm).MarshalJSON()
if err != nil {
return nil, err
}
fmt.Println(v)
return nil, nil
}, nil
}
func (t *ScriptRepo) getCache(ctx context.Context, productID string) (*CustomCacheStu, error) {
temp, ok := t.cache.Get(productID)
if ok {
if temp == nil {
return nil, nil
}
return temp.(*CustomCacheStu), nil
}
ps, err := relationDB.NewProductCustomRepo(ctx).FindOneByProductID(ctx, productID)
if err != nil {
if err == errors.NotFind {
return nil, nil
}
return nil, err
}
ret := &CustomCacheStu{
Topics: ps.CustomTopics,
}
if ps.LoginAuthScript != "" {
ret.LoginAuthPool = &sync.Pool{New: func() any {
vm := goja.New()
_, err := vm.RunString(ps.LoginAuthScript)
if err != nil {
return nil
}
return vm
},
}
}
t.cache.SetWithTTL(productID, ps, 1, expireTime)
return ret, nil
}
func (t *ScriptRepo) ClearCache(ctx context.Context, productID string) error {
t.cache.Del(productID)
info, err := t.getCache(ctx, productID)
if err != nil {
return err
}
t.cache.SetWithTTL(productID, info, 1, expireTime)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/things.git
git@gitee.com:unitedrhino/things.git
unitedrhino
things
物联网iot模块
v1.0.3

搜索帮助