1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-08-30 20:58:12 +02:00

Improved code documentation.

This commit is contained in:
Henrik Hautakoski 2023-08-22 16:22:03 +02:00
parent 96764ef651
commit 31c7ba6a4b
6 changed files with 45 additions and 9 deletions

View file

@ -8,12 +8,14 @@ import (
redis_cache "github.com/go-redis/cache/v9"
)
// Cache represents a abi cache in redis.
type Cache struct {
c *redis_cache.Cache
ctx context.Context
prefix string
}
// Create a new cache
func NewCache(prefix string, options *redis_cache.Options) *Cache {
return &Cache{
c: redis_cache.New(options),
@ -22,12 +24,14 @@ func NewCache(prefix string, options *redis_cache.Options) *Cache {
}
}
// Get an ABI from the cache using the contract account name as the key.
func (cache *Cache) Get(account string) (*eos.ABI, error) {
var v eos.ABI
err := cache.c.Get(cache.ctx, cache.key(account), &v)
return &v, err
}
// Set an ABI in the cache.
func (cache *Cache) Set(account string, abi *eos.ABI, ttl time.Duration) error {
return cache.c.Set(&redis_cache.Item{
Ctx: cache.ctx,

View file

@ -10,12 +10,14 @@ import (
"github.com/redis/go-redis/v9"
)
// AbiManager handles an ABI cache that fetches the ABI from an API on cache miss.
type AbiManager struct {
cache *Cache
api *eos.API
ctx context.Context
}
// Create a new ABI Manager
func NewAbiManager(rdb *redis.Client, api *eos.API, id string) *AbiManager {
// Init abi cache
cache := NewCache("thalos::cache::"+id+"::abi", &redis_cache.Options{
@ -36,6 +38,8 @@ func (mgr *AbiManager) SetAbi(account eos.AccountName, abi *eos.ABI) error {
return mgr.cache.Set(string(account), abi, time.Hour)
}
// Get an ABI from the cache, on cache miss it is fetched from the
// API, gets cached and then returned to the user
func (mgr *AbiManager) GetAbi(account eos.AccountName) (*eos.ABI, error) {
key := string(account)