Skip to content

Commit

Permalink
plugin: introduce the cache system to speed up the plugin with big node
Browse files Browse the repository at this point in the history
Changelog-Fixed: introduce the cache system to speed up the plugin with big node

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo committed Apr 1, 2022
1 parent 59bcaf2 commit d99abce
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ vendor
go-lnmetrics

*.out

.idea/
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/LNOpenMetrics/go-lnmetrics.reporter
go 1.17

require (
github.com/LNOpenMetrics/lnmetrics.utils v0.0.3
github.com/LNOpenMetrics/lnmetrics.utils v0.0.6
github.com/elastic/go-sysinfo v1.7.1
github.com/kinbiko/jsonassert v1.0.2
github.com/robfig/cron/v3 v3.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/LNOpenMetrics/lnmetrics.utils v0.0.2 h1:USJE6C1dhiNUjiQ29iQ3MHxxZt9Y0
github.com/LNOpenMetrics/lnmetrics.utils v0.0.2/go.mod h1:6PC0XEUljl08AHdHdMQtR2tGJ1o9Jzg8yt7nioTnlE4=
github.com/LNOpenMetrics/lnmetrics.utils v0.0.3 h1:emd43at2elU8FDAeJhTDNkMF1/cbYR0lFS0iyka34Go=
github.com/LNOpenMetrics/lnmetrics.utils v0.0.3/go.mod h1:6PC0XEUljl08AHdHdMQtR2tGJ1o9Jzg8yt7nioTnlE4=
github.com/LNOpenMetrics/lnmetrics.utils v0.0.6 h1:fO6nTbMsLzJDEHRRfyS+QUSEzmyXHCaOWOpmttlYGrE=
github.com/LNOpenMetrics/lnmetrics.utils v0.0.6/go.mod h1:6PC0XEUljl08AHdHdMQtR2tGJ1o9Jzg8yt7nioTnlE4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
124 changes: 124 additions & 0 deletions internal/cache/cache_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package cache

import (
"encoding/json"
"fmt"
db "github.com/LNOpenMetrics/lnmetrics.utils/db/leveldb"
"github.com/LNOpenMetrics/lnmetrics.utils/log"
"strings"
)

// cacheManager is the internal rappresentation
// of the cache manager, that contains all the utils
// function to manage the cache
type cacheManager struct {
prefix string
cacheIdx string
cache map[string]*string
}

func GetInstance() *cacheManager {
return &cacheManager{
prefix: "cache",
cacheIdx: "cache/idx",
cache: nil,
}
}

func (instance *cacheManager) buildID(key string) string {
return strings.Join([]string{instance.prefix, key}, "/")
}

func (instance *cacheManager) initCache() error {
instance.cache = make(map[string]*string)
arrJson, err := json.Marshal(instance.cache)
if err != nil {
return err
}
if err := db.GetInstance().PutValueInBytes(instance.cacheIdx, arrJson); err != nil {
log.GetInstance().Errorf("%s", err)
return err
}
return nil
}

func (instance *cacheManager) getCacheIndex() (map[string]*string, error) {
if instance.cache == nil {
if err := instance.initCache(); err != nil {
return nil, err
}
return instance.getCacheIndex()
}
value, err := db.GetInstance().GetValueInBytes(instance.cacheIdx)
if err != nil {
return nil, err
}
if err := json.Unmarshal(value, &instance.cache); err != nil {
return nil, err
}
return instance.cache, nil
}

func (instance *cacheManager) addToCache(key string) error {
idx := instance.cache
if idx == nil {
tmpIdx, err := instance.getCacheIndex()
if err != nil {
return err
}
idx = tmpIdx
}
if _, ok := idx[key]; !ok {
idx[key] = &key
}
return nil
}

func (instance *cacheManager) IsInCache(key string) bool {
if instance.cache != nil {
_, ok := instance.cache[key]
if ok {
return ok
}
// otherwise, continue and check with the database
}
key = instance.buildID(key)
_, err := db.GetInstance().GetValue(key)
if err != nil {
log.GetInstance().Errorf("Error inside the cache: %s", err)
return false
}
return true
}

// GetFromCache retrieval the information that are in the cache in bytes
func (instance *cacheManager) GetFromCache(key string) ([]byte, error) {
if instance.IsInCache(key) {
key = instance.buildID(key)
return db.GetInstance().GetValueInBytes(key)
}
return nil, fmt.Errorf("no value with key %s in the cache", key)
}

// PutToCache put the value in the json form inside the cache with the key specified
func (instance *cacheManager) PutToCache(key string, value interface{}) error {
jsonValue, err := json.Marshal(value)
if err != nil {
return err
}
key = instance.buildID(key)
if err := db.GetInstance().PutValueInBytes(key, jsonValue); err != nil {
return err
}
return instance.addToCache(key)
}

// CleanCache clean the index from the database
func (instance *cacheManager) CleanCache() error {
for key := range instance.cache {
if err := db.GetInstance().DeleteValue(key); err != nil {
return err
}
}
return nil
}
16 changes: 16 additions & 0 deletions internal/cache/cache_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Package cache package implement the basic block to implement the
// cache persistence
package cache

import (
"github.com/vincenzopalazzo/glightning/glightning"
)

// NodeInfoCache implement the interface to
// store the node information inside the cache.
type NodeInfoCache struct {
ID string
Alias string
Color string
Features *glightning.Hexed
}
Loading

0 comments on commit d99abce

Please sign in to comment.