-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugin: introduce the cache system to speed up the plugin with big node
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
1 parent
59bcaf2
commit d99abce
Showing
6 changed files
with
211 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ vendor | |
go-lnmetrics | ||
|
||
*.out | ||
|
||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.