Skip to content

Commit 0d94ed1

Browse files
committed
refactor(plugin): move cache logic from Lua to Go layer
Simplify the caching mechanism by moving the cache logic for the Available hook from the Lua layer to the Go layer. This improves maintainability and reduces complexity in the Lua code. The cache duration and behavior remain consistent with the previous implementation.
1 parent 986bc2c commit 0d94ed1

3 files changed

Lines changed: 55 additions & 89 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55

66
coverage.out
77
**/available.cache
8+
**/.available.cache
89

910
vfox

internal/plugin/luai/plugin.go

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package luai
33
import (
44
"errors"
55
"fmt"
6-
"os"
76
"path/filepath"
8-
"strings"
97

108
"github.com/version-fox/vfox/internal/base"
11-
"github.com/version-fox/vfox/internal/cache"
129
"github.com/version-fox/vfox/internal/config"
1310
"github.com/version-fox/vfox/internal/logger"
1411
"github.com/version-fox/vfox/internal/util"
@@ -237,91 +234,10 @@ func CreateLuaPlugin(pluginDirPath string, config *config.Config, runtimeVersion
237234
}
238235

239236
source := &LuaPlugin{
240-
vm: vm,
241-
pluginObj: PLUGIN,
242-
237+
vm: vm,
238+
pluginObj: PLUGIN,
243239
PluginInfo: pluginInfo,
244240
}
245241

246-
// wrap Available hook with Cache.
247-
if source.HasFunction("Available") {
248-
targetHook := PLUGIN.RawGetString("Available")
249-
source.pluginObj.RawSetString("Available", vm.Instance.NewFunction(func(L *lua.LState) int {
250-
ctxTable := L.CheckTable(2)
251-
252-
cachePath := filepath.Join(pluginDirPath, "available.cache")
253-
invokeAvailableHook := func() int {
254-
logger.Debugf("Calling the original Available hook. \n")
255-
table, err := vm.CallFunction(targetHook, PLUGIN, ctxTable)
256-
if err != nil {
257-
L.RaiseError(err.Error())
258-
return 0
259-
}
260-
if util.FileExists(cachePath) {
261-
logger.Debugf("Removing the old cache file: %s \n", cachePath)
262-
_ = os.Remove(cachePath)
263-
}
264-
L.Push(table)
265-
return 1
266-
}
267-
268-
logger.Debugf("Available hook cache duration: %v\n", config.Cache.AvailableHookDuration)
269-
// Cache is disabled
270-
if config.Cache.AvailableHookDuration == 0 {
271-
return invokeAvailableHook()
272-
}
273-
274-
ctx := &base.AvailableHookCtx{}
275-
if err := Unmarshal(ctxTable, ctx); err != nil {
276-
L.RaiseError(err.Error())
277-
return 0
278-
}
279-
280-
cacheKey := strings.Join(ctx.Args, "##")
281-
if cacheKey == "" {
282-
cacheKey = "empty"
283-
}
284-
fileCache, err := cache.NewFileCache(cachePath)
285-
if err != nil {
286-
return invokeAvailableHook()
287-
}
288-
cacheValue, ok := fileCache.Get(cacheKey)
289-
logger.Debugf("Available hook cache key: %s, hit: %v \n", cacheKey, ok)
290-
if ok {
291-
var hookResult []map[string]interface{}
292-
if err = cacheValue.Unmarshal(&hookResult); err != nil {
293-
return invokeAvailableHook()
294-
}
295-
table, err := Marshal(L, hookResult)
296-
if err != nil {
297-
return invokeAvailableHook()
298-
}
299-
L.Push(table)
300-
return 1
301-
} else {
302-
table, err := vm.CallFunction(targetHook, PLUGIN, ctxTable)
303-
if err != nil {
304-
L.RaiseError(err.Error())
305-
return 0
306-
}
307-
if table == nil || table.Type() == lua.LTNil {
308-
fileCache.Set(cacheKey, nil, cache.ExpireTime(config.Cache.AvailableHookDuration))
309-
_ = fileCache.Close()
310-
} else {
311-
var hookResult []map[string]interface{}
312-
if err = Unmarshal(table, &hookResult); err == nil {
313-
if value, err := cache.NewValue(hookResult); err == nil {
314-
fileCache.Set(cacheKey, value, cache.ExpireTime(config.Cache.AvailableHookDuration))
315-
_ = fileCache.Close()
316-
}
317-
}
318-
}
319-
L.Push(table)
320-
return 1
321-
}
322-
323-
}))
324-
325-
}
326242
return source, nil
327243
}

internal/plugin/plugin.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package plugin
1919
import (
2020
"fmt"
2121
"path/filepath"
22+
"strings"
2223

2324
"github.com/pterm/pterm"
2425
"github.com/version-fox/vfox/internal/base"
26+
"github.com/version-fox/vfox/internal/cache"
2527
"github.com/version-fox/vfox/internal/config"
2628
"github.com/version-fox/vfox/internal/env"
2729
"github.com/version-fox/vfox/internal/logger"
@@ -55,7 +57,8 @@ func isValidName(name string) bool {
5557
}
5658

5759
type PluginWrapper struct {
58-
impl base.Plugin
60+
impl base.Plugin
61+
config config.Config
5962

6063
// plugin source path
6164
Path string
@@ -92,8 +95,8 @@ func (l *PluginWrapper) validate() error {
9295
func (l *PluginWrapper) Close() {
9396
l.impl.Close()
9497
}
95-
96-
func (l *PluginWrapper) Available(args []string) ([]*base.Package, error) {
98+
func (l *PluginWrapper) invokeAvailable(args []string) ([]*base.Package, error) {
99+
logger.Debug("Calling Available hook")
97100
ctx := base.AvailableHookCtx{
98101
Args: args,
99102
}
@@ -108,6 +111,51 @@ func (l *PluginWrapper) Available(args []string) ([]*base.Package, error) {
108111
return base.CreatePackages(l.Name, *result), nil
109112
}
110113

114+
func (l *PluginWrapper) Available(args []string) ([]*base.Package, error) {
115+
cachePath := filepath.Join(l.Path, ".available.cache")
116+
cacheDuration := l.config.Cache.AvailableHookDuration
117+
logger.Debugf("Available hook cache duration: %v\n", cacheDuration)
118+
119+
// Cache is disabled
120+
if cacheDuration == 0 {
121+
return l.invokeAvailable(args)
122+
}
123+
124+
cacheKey := strings.Join(args, "##")
125+
if cacheKey == "" {
126+
cacheKey = "empty"
127+
}
128+
129+
fileCache, err := cache.NewFileCache(cachePath)
130+
if err == nil {
131+
cacheValue, ok := fileCache.Get(cacheKey)
132+
logger.Debugf("Available hook cache key: %s, hit: %+v \n", cacheKey, ok)
133+
if ok {
134+
var hookResult []*base.Package
135+
if err = cacheValue.Unmarshal(&hookResult); err == nil {
136+
return hookResult, nil
137+
}
138+
}
139+
}
140+
141+
result, err := l.invokeAvailable(args)
142+
if err != nil {
143+
return result, err
144+
}
145+
146+
if result == nil {
147+
fileCache.Set(cacheKey, nil, cache.ExpireTime(cacheDuration))
148+
}
149+
150+
if value, err := cache.NewValue(result); err == nil {
151+
logger.Debugf("Available hook cache set\n")
152+
fileCache.Set(cacheKey, value, cache.ExpireTime(cacheDuration))
153+
_ = fileCache.Close()
154+
}
155+
156+
return result, nil
157+
}
158+
111159
func (l *PluginWrapper) PreInstall(version base.Version) (*base.Package, error) {
112160
ctx := base.PreInstallHookCtx{
113161
Version: version,
@@ -317,6 +365,7 @@ func NewLuaPlugin(pluginDirPath string, config *config.Config, runtimeVersion st
317365
impl: plugin2,
318366
Path: pluginDirPath,
319367
PluginInfo: plugin2.PluginInfo,
368+
config: *config,
320369
}
321370

322371
if err = source.validate(); err != nil {

0 commit comments

Comments
 (0)