Skip to content

Commit

Permalink
feat(pkg/cache): support version prefix (#3829)
Browse files Browse the repository at this point in the history
* feat(pkg/cache): support version prefix

* chore: update changelog

* tests: add versioned storage cache tests

---------

Co-authored-by: Danilo Pantani <[email protected]>
  • Loading branch information
jeronimoalbi and Pantani authored Dec 13, 2023
1 parent 1b08ba3 commit 1999c2b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- [#3822](https://github.com/ignite/cli/pull/3822) Improve default scaffolded AutoCLI config
- [#3838](https://github.com/ignite/cli/pull/3838) Scaffold chain with Cosmos SDK `v0.50.2`, and bump confix and x/upgrade to latest
- [#3829](https://github.com/ignite/cli/pull/3829) Support version prefix for cached values
- [#3723](https://github.com/ignite/cli/pull/3723) Create a wrapper for errors

### Fixes
Expand Down
5 changes: 4 additions & 1 deletion ignite/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ func newCache(cmd *cobra.Command) (cache.Storage, error) {
return cache.Storage{}, err
}

storage, err := cache.NewStorage(filepath.Join(cacheRootDir, cacheFileName))
storage, err := cache.NewStorage(
filepath.Join(cacheRootDir, cacheFileName),
cache.WithVersion(version.Version),
)
if err != nil {
return cache.Storage{}, err
}
Expand Down
23 changes: 16 additions & 7 deletions ignite/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"bytes"
"encoding/gob"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -17,7 +18,7 @@ var ErrorNotFound = errors.New("no value was found with the provided key")

// Storage is meant to be passed around and used by the New function (which provides namespacing and type-safety).
type Storage struct {
storagePath string
path, version string
}

// Cache is a namespaced and type-safe key-value store.
Expand All @@ -29,16 +30,24 @@ type Cache[T any] struct {
// NewStorage sets up the storage needed for later cache usage
// path is the full path (including filename) to the database file to use.
// It does not need to be closed as this happens automatically in each call to the cache.
func NewStorage(path string) (Storage, error) {
func NewStorage(path string, options ...StorageOption) (Storage, error) {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return Storage{}, err
}

return Storage{path}, nil
s := Storage{path: path}
for _, apply := range options {
apply(&s)
}
return s, nil
}

// New creates a namespaced and typesafe key-value Cache.
func New[T any](storage Storage, namespace string) Cache[T] {
if storage.version != "" {
namespace = fmt.Sprint(storage.version, namespace)
}

return Cache[T]{
storage: storage,
namespace: namespace,
Expand All @@ -52,7 +61,7 @@ func Key(keyParts ...string) string {

// Clear deletes all namespaces and cached values.
func (s Storage) Clear() error {
db, err := openDB(s.storagePath)
db, err := openDB(s.path)
if err != nil {
return err
}
Expand All @@ -68,7 +77,7 @@ func (s Storage) Clear() error {
// Put sets key to value within the namespace
// If the key already exists, it will be overwritten.
func (c Cache[T]) Put(key string, value T) error {
db, err := openDB(c.storage.storagePath)
db, err := openDB(c.storage.path)
if err != nil {
return err
}
Expand All @@ -93,7 +102,7 @@ func (c Cache[T]) Put(key string, value T) error {
// Get fetches the value of key within the namespace.
// If no value exists, it will return found == false.
func (c Cache[T]) Get(key string) (val T, err error) {
db, err := openDB(c.storage.storagePath)
db, err := openDB(c.storage.path)
if err != nil {
return val, err
}
Expand Down Expand Up @@ -129,7 +138,7 @@ func (c Cache[T]) Get(key string) (val T, err error) {

// Delete removes a value for key within the namespace.
func (c Cache[T]) Delete(key string) error {
db, err := openDB(c.storage.storagePath)
db, err := openDB(c.storage.path)
if err != nil {
return err
}
Expand Down
43 changes: 39 additions & 4 deletions ignite/pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,49 @@ type TestStruct struct {
}

func TestCreateStorage(t *testing.T) {
tmpDir1 := t.TempDir()
tmpDir2 := t.TempDir()
cases := []struct {
name string
options []cache.StorageOption
}{
{
name: "simple",
},
{
name: "versioned",
options: []cache.StorageOption{
cache.WithVersion("v0.1.0"),
},
},
}

for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
_, err := cache.NewStorage(filepath.Join(t.TempDir(), "cache.db"), tt.options...)
require.NoError(t, err)
})
}
}

_, err := cache.NewStorage(filepath.Join(tmpDir1, "test.db"))
func TestStoreWithVersion(t *testing.T) {
path := filepath.Join(t.TempDir(), "cache.db")
storage, err := cache.NewStorage(path, cache.WithVersion("v0.1.0"))
require.NoError(t, err)

_, err = cache.NewStorage(filepath.Join(tmpDir2, "test.db"))
nsCache := cache.New[string](storage, "cacheNS")
err = nsCache.Put("myKey", "myValue")
require.NoError(t, err)

v, err := nsCache.Get("myKey")
require.NoError(t, err)
require.Equal(t, "myValue", v)

// Create a non versioned storage with the same file path
storage, err = cache.NewStorage(path)
require.NoError(t, err)

nsCache = cache.New[string](storage, "cacheNS")
_, err = nsCache.Get("myKey")
require.ErrorIs(t, err, cache.ErrorNotFound)
}

func TestStoreString(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions ignite/pkg/cache/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cache

// StorageOptions configures the cache storage.
type StorageOption func(*Storage)

// WithVersion sets a version for the storage.
// Version is used as prefix for any cached value.
func WithVersion(version string) StorageOption {
return func(o *Storage) {
o.version = version
}
}
6 changes: 5 additions & 1 deletion ignite/services/plugin/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ignite/cli/v28/ignite/pkg/cache"
"github.com/ignite/cli/v28/ignite/pkg/errors"
"github.com/ignite/cli/v28/ignite/version"
)

const (
Expand Down Expand Up @@ -80,7 +81,10 @@ func newCache() (*cache.Cache[hplugin.ReattachConfig], error) {
return nil, err
}
if storageCache == nil {
storage, err := cache.NewStorage(path.Join(cacheRootDir, cacheFileName))
storage, err := cache.NewStorage(
path.Join(cacheRootDir, cacheFileName),
cache.WithVersion(version.Version),
)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 1999c2b

Please sign in to comment.