Skip to content

Commit

Permalink
fix: use json serialization to store cache instead of github.com/vmih…
Browse files Browse the repository at this point in the history
…ailenco/msgpack (argoproj#4965)

Signed-off-by: Alexander Matyushentsev <[email protected]>
  • Loading branch information
Alexander Matyushentsev authored Dec 3, 2020
1 parent 5f46815 commit 4417cc8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const (
MinClientVersion = "1.4.0"
// CacheVersion is a objects version cached using util/cache/cache.go.
// Number should be bumped in case of backward incompatible change to make sure cache is invalidated after upgrade.
CacheVersion = "1.8.2"
CacheVersion = "1.8.3"
)

// GetGnuPGHomePath retrieves the path to use for GnuPG home directory, which is either taken from GNUPGHOME environment or a default value
Expand Down
4 changes: 2 additions & 2 deletions reposerver/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func appDetailsCacheKey(revision string, appSrc *appv1.ApplicationSource) string
return fmt.Sprintf("appdetails|%s|%d", revision, appSourceKey(appSrc))
}

func (c *Cache) GetAppDetails(revision string, appSrc *appv1.ApplicationSource, res interface{}) error {
func (c *Cache) GetAppDetails(revision string, appSrc *appv1.ApplicationSource, res *apiclient.RepoAppDetailsResponse) error {
return c.cache.GetItem(appDetailsCacheKey(revision, appSrc), res)
}

func (c *Cache) SetAppDetails(revision string, appSrc *appv1.ApplicationSource, res interface{}) error {
func (c *Cache) SetAppDetails(revision string, appSrc *appv1.ApplicationSource, res *apiclient.RepoAppDetailsResponse) error {
return c.cache.SetItem(appDetailsCacheKey(revision, appSrc), res, c.repoCacheExpiration, res == nil)
}

Expand Down
2 changes: 1 addition & 1 deletion reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ func (s *Service) GetAppDetails(ctx context.Context, q *apiclient.RepoServerAppD

getCached := func(revision string, _ bool) (bool, interface{}, error) {
res := &apiclient.RepoAppDetailsResponse{}
err := s.cache.GetAppDetails(revision, q.Source, &res)
err := s.cache.GetAppDetails(revision, q.Source, res)
if err == nil {
log.Infof("app details cache hit: %s/%s", revision, q.Source.Path)
return true, res, nil
Expand Down
6 changes: 3 additions & 3 deletions util/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ type Cache struct {
}

func (c *Cache) SetItem(key string, item interface{}, expiration time.Duration, delete bool) error {
if item == nil {
return fmt.Errorf("cannot set item to nil for key %s", key)
}
key = fmt.Sprintf("%s|%s", key, common.CacheVersion)
if delete {
return c.client.Delete(key)
} else {
if item == nil {
return fmt.Errorf("cannot set item to nil for key %s", key)
}
return c.client.Set(&Item{Object: item, Key: key, Expiration: expiration})
}
}
Expand Down
19 changes: 14 additions & 5 deletions util/cache/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"context"
"encoding/json"
"time"

ioutil "github.com/argoproj/argo-cd/util/io"
Expand Down Expand Up @@ -30,20 +31,28 @@ func (r *redisCache) Set(item *Item) error {
expiration = r.expiration
}

val, err := json.Marshal(item.Object)
if err != nil {
return err
}

return r.cache.Set(&rediscache.Item{
Key: item.Key,
Value: item.Object,
Value: val,
TTL: expiration,
})
}

func (r *redisCache) Get(key string, obj interface{}) error {
err := r.cache.Get(context.TODO(), key, obj)

var data []byte
err := r.cache.Get(context.TODO(), key, &data)
if err == rediscache.ErrCacheMiss {
return ErrCacheMiss
err = ErrCacheMiss
}
if err != nil {
return err
}
return err
return json.Unmarshal(data, obj)
}

func (r *redisCache) Delete(key string) error {
Expand Down

0 comments on commit 4417cc8

Please sign in to comment.