Skip to content

Commit

Permalink
fix: use map to cache all Available results, not only AvailableHo…
Browse files Browse the repository at this point in the history
…okResults
  • Loading branch information
aooohan committed May 13, 2024
1 parent 34001e4 commit f524c33
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
_ "embed"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"

Expand Down Expand Up @@ -541,12 +542,17 @@ func NewLuaPlugin(pluginDirPath string, manager *Manager) (*LuaPlugin, error) {
source.pluginObj.RawSetString("Available", vm.Instance.NewFunction(func(L *lua.LState) int {
ctxTable := L.CheckTable(2)

cachePath := filepath.Join(pluginDirPath, "available.cache")
invokeAvailableHook := func() int {
logger.Debugf("Calling the original Available hook. \n")
if err := vm.CallFunction(targetHook, PLUGIN, ctxTable); err != nil {
L.RaiseError(err.Error())
return 0
}
if util.FileExists(cachePath) {
logger.Debugf("Removing the old cache file: %s \n", cachePath)
_ = os.Remove(cachePath)
}
table := source.vm.ReturnedValue()
L.Push(table)
return 1
Expand All @@ -568,14 +574,14 @@ func NewLuaPlugin(pluginDirPath string, manager *Manager) (*LuaPlugin, error) {
if cacheKey == "" {
cacheKey = "empty"
}
fileCache, err := cache.NewFileCache(filepath.Join(pluginDirPath, "available.cache"))
fileCache, err := cache.NewFileCache(cachePath)
if err != nil {
return invokeAvailableHook()
}
cacheValue, ok := fileCache.Get(cacheKey)
logger.Debugf("Available hook cache key: %s, hit: %v \n", cacheKey, ok)
if ok {
hookResult := AvailableHookResult{}
var hookResult []map[string]interface{}
if err = cacheValue.Unmarshal(&hookResult); err != nil {
return invokeAvailableHook()
}
Expand All @@ -595,7 +601,7 @@ func NewLuaPlugin(pluginDirPath string, manager *Manager) (*LuaPlugin, error) {
fileCache.Set(cacheKey, nil, cache.ExpireTime(config.Cache.AvailableHookDuration))
_ = fileCache.Close()
} else {
hookResult := AvailableHookResult{}
var hookResult []map[string]interface{}
if err = luai.Unmarshal(table, &hookResult); err == nil {
if value, err := cache.NewValue(hookResult); err == nil {
fileCache.Set(cacheKey, value, cache.ExpireTime(config.Cache.AvailableHookDuration))
Expand Down

0 comments on commit f524c33

Please sign in to comment.