From d69018f71eb9bf95e092e347c7f90b70fd089f4d Mon Sep 17 00:00:00 2001 From: HanMengnan <1448189829@qq.com> Date: Wed, 9 Nov 2022 14:18:34 +0800 Subject: [PATCH] Fix: reuse existing lru golang library Signed-off-by: HanMengnan <1448189829@qq.com> --- go.mod | 1 + go.sum | 1 + references/cli/top/model/cache.go | 76 ------------------- references/cli/top/model/cache_test.go | 44 ----------- .../cli/top/view/application_topology_view.go | 13 ++-- 5 files changed, 10 insertions(+), 125 deletions(-) delete mode 100644 references/cli/top/model/cache.go delete mode 100644 references/cli/top/model/cache_test.go diff --git a/go.mod b/go.mod index 8c8c88862..0d3ba9079 100644 --- a/go.mod +++ b/go.mod @@ -50,6 +50,7 @@ require ( github.com/gosuri/uilive v0.0.4 github.com/gosuri/uitable v0.0.4 github.com/hashicorp/go-version v1.3.0 + github.com/hashicorp/golang-lru v0.5.4 github.com/hashicorp/hcl/v2 v2.9.1 github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 github.com/imdario/mergo v0.3.12 diff --git a/go.sum b/go.sum index 7fb2c5ea1..c40c887bb 100644 --- a/go.sum +++ b/go.sum @@ -1182,6 +1182,7 @@ github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= diff --git a/references/cli/top/model/cache.go b/references/cli/top/model/cache.go deleted file mode 100644 index bafc09971..000000000 --- a/references/cli/top/model/cache.go +++ /dev/null @@ -1,76 +0,0 @@ -/* -Copyright 2022 The KubeVela Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package model - -// LRU type cache -type LRU struct { - capacity int - cache []data -} - -type data struct { - key string - value interface{} -} - -// NewLRUCache return the LRU type cache -func NewLRUCache(capacity int) *LRU { - return &LRU{ - capacity: capacity, - cache: make([]data, 0), - } -} - -// Get return the value of the key -func (c *LRU) Get(key string) interface{} { - index := c.exist(key) - if index != -1 { - c.upgrade(index) - return c.cache[len(c.cache)-1].value - } - return nil -} - -// Put store a new k-v pair -func (c *LRU) Put(key string, value interface{}) { - index := c.exist(key) - if index != -1 { - c.cache[index].value = value - c.upgrade(index) - } else { - c.cache = append(c.cache, data{key: key, value: value}) - if len(c.cache) > c.capacity { - c.cache = c.cache[1:] - } - } -} - -func (c *LRU) exist(key string) int { - for i := 0; i < len(c.cache); i++ { - if c.cache[i].key == key { - return i - } - } - return -1 -} - -func (c *LRU) upgrade(index int) { - size := len(c.cache) - item := c.cache[index] - copy(c.cache[index:size-1], c.cache[index+1:]) - c.cache[size-1] = item -} diff --git a/references/cli/top/model/cache_test.go b/references/cli/top/model/cache_test.go deleted file mode 100644 index 988aff470..000000000 --- a/references/cli/top/model/cache_test.go +++ /dev/null @@ -1,44 +0,0 @@ -/* -Copyright 2022 The KubeVela Authors. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package model - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestLRUCache(t *testing.T) { - cache := NewLRUCache(2) - cache.Put("app1-vela-system", 1) - cache.Put("app2-vela-system", 2) - value, ok := cache.Get("app1-vela-system").(int) - assert.Equal(t, ok, true) - assert.Equal(t, value, 1) - cache.Put("app3-vela-system", 3) - _, ok = cache.Get("app2-vela-system").(int) - assert.Equal(t, ok, false) - cache.Put("app4-vela-system", 4) - _, ok = cache.Get("app1-vela-system").(int) - assert.Equal(t, ok, false) - value, ok = cache.Get("app3-vela-system").(int) - assert.Equal(t, ok, true) - assert.Equal(t, value, 3) - value, ok = cache.Get("app4-vela-system").(int) - assert.Equal(t, ok, true) - assert.Equal(t, value, 4) -} diff --git a/references/cli/top/view/application_topology_view.go b/references/cli/top/view/application_topology_view.go index 0a7166bb0..9d6250b8e 100644 --- a/references/cli/top/view/application_topology_view.go +++ b/references/cli/top/view/application_topology_view.go @@ -21,6 +21,7 @@ import ( "fmt" "github.com/gdamore/tcell/v2" + lru "github.com/hashicorp/golang-lru" "github.com/rivo/tview" "github.com/oam-dev/kubevela/pkg/velaql/providers/query/types" @@ -36,7 +37,7 @@ type TopologyView struct { actions model.KeyActions ctx context.Context focusTopology bool - cache *model.LRU + cache *lru.Cache appTopologyInstance *TopologyTree resourceTopologyInstance *TopologyTree } @@ -52,7 +53,7 @@ type TopologyTree struct { } const ( - numberOfCacheView = 5 + numberOfCacheView = 10 ) var ( @@ -67,7 +68,7 @@ func NewTopologyView(ctx context.Context, app *App) model.View { if topologyViewInstance.Grid == nil { topologyViewInstance.Grid = tview.NewGrid() topologyViewInstance.actions = make(model.KeyActions) - topologyViewInstance.cache = model.NewLRUCache(numberOfCacheView) + topologyViewInstance.cache, _ = lru.New(numberOfCacheView) topologyViewInstance.appTopologyInstance = new(TopologyTree) topologyViewInstance.resourceTopologyInstance = new(TopologyTree) @@ -93,13 +94,15 @@ func (v *TopologyView) Start() { namespace := v.ctx.Value(&model.CtxKeyNamespace).(string) key := fmt.Sprintf("%s-%s", appName, namespace) - if view, ok := v.cache.Get(key).(*cacheView); ok { + value, exist := v.cache.Get(key) + view, ok := value.(*cacheView) + if exist && ok { v.appTopologyInstance = view.appTopologyInstance v.resourceTopologyInstance = view.resourceTopologyInstance } else { v.resourceTopologyInstance = v.NewResourceTopologyView() v.appTopologyInstance = v.NewAppTopologyView() - v.cache.Put(key, &cacheView{ + v.cache.Add(key, &cacheView{ resourceTopologyInstance: v.resourceTopologyInstance, appTopologyInstance: v.appTopologyInstance, })