github.com/argoproj/argo-cd@v1.8.7/util/cache/appstate/cache.go (about)

     1  package appstate
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sort"
     7  	"time"
     8  
     9  	"github.com/go-redis/redis/v8"
    10  	"github.com/spf13/cobra"
    11  
    12  	appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    13  	cacheutil "github.com/argoproj/argo-cd/util/cache"
    14  )
    15  
    16  var ErrCacheMiss = cacheutil.ErrCacheMiss
    17  
    18  const (
    19  	clusterInfoCacheExpiration = 10 * time.Minute
    20  )
    21  
    22  type Cache struct {
    23  	Cache                   *cacheutil.Cache
    24  	appStateCacheExpiration time.Duration
    25  }
    26  
    27  func NewCache(cache *cacheutil.Cache, appStateCacheExpiration time.Duration) *Cache {
    28  	return &Cache{cache, appStateCacheExpiration}
    29  }
    30  
    31  func AddCacheFlagsToCmd(cmd *cobra.Command, opts ...func(client *redis.Client)) func() (*Cache, error) {
    32  	var appStateCacheExpiration time.Duration
    33  
    34  	cmd.Flags().DurationVar(&appStateCacheExpiration, "app-state-cache-expiration", 1*time.Hour, "Cache expiration for app state")
    35  
    36  	cacheFactory := cacheutil.AddCacheFlagsToCmd(cmd, opts...)
    37  
    38  	return func() (*Cache, error) {
    39  		cache, err := cacheFactory()
    40  		if err != nil {
    41  			return nil, err
    42  		}
    43  		return NewCache(cache, appStateCacheExpiration), nil
    44  	}
    45  }
    46  
    47  func (c *Cache) GetItem(key string, item interface{}) error {
    48  	return c.Cache.GetItem(key, item)
    49  }
    50  
    51  func (c *Cache) SetItem(key string, item interface{}, expiration time.Duration, delete bool) error {
    52  	return c.Cache.SetItem(key, item, expiration, delete)
    53  }
    54  
    55  func appManagedResourcesKey(appName string) string {
    56  	return fmt.Sprintf("app|managed-resources|%s", appName)
    57  }
    58  
    59  func (c *Cache) GetAppManagedResources(appName string, res *[]*appv1.ResourceDiff) error {
    60  	err := c.GetItem(appManagedResourcesKey(appName), &res)
    61  	return err
    62  }
    63  
    64  func (c *Cache) SetAppManagedResources(appName string, managedResources []*appv1.ResourceDiff) error {
    65  	sort.Slice(managedResources, func(i, j int) bool {
    66  		return managedResources[i].FullName() < managedResources[j].FullName()
    67  	})
    68  	return c.SetItem(appManagedResourcesKey(appName), managedResources, c.appStateCacheExpiration, managedResources == nil)
    69  }
    70  
    71  func appResourcesTreeKey(appName string) string {
    72  	return fmt.Sprintf("app|resources-tree|%s", appName)
    73  }
    74  
    75  func clusterInfoKey(server string) string {
    76  	return fmt.Sprintf("cluster|info|%s", server)
    77  }
    78  
    79  func (c *Cache) GetAppResourcesTree(appName string, res *appv1.ApplicationTree) error {
    80  	err := c.GetItem(appResourcesTreeKey(appName), &res)
    81  	return err
    82  }
    83  
    84  func (c *Cache) OnAppResourcesTreeChanged(ctx context.Context, appName string, callback func() error) error {
    85  	return c.Cache.OnUpdated(ctx, appManagedResourcesKey(appName), callback)
    86  }
    87  
    88  func (c *Cache) SetAppResourcesTree(appName string, resourcesTree *appv1.ApplicationTree) error {
    89  	if resourcesTree != nil {
    90  		resourcesTree.Normalize()
    91  	}
    92  	err := c.SetItem(appResourcesTreeKey(appName), resourcesTree, c.appStateCacheExpiration, resourcesTree == nil)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	return c.Cache.NotifyUpdated(appManagedResourcesKey(appName))
    97  }
    98  
    99  func (c *Cache) SetClusterInfo(server string, info *appv1.ClusterInfo) error {
   100  	return c.SetItem(clusterInfoKey(server), info, clusterInfoCacheExpiration, info == nil)
   101  }
   102  
   103  func (c *Cache) GetClusterInfo(server string, res *appv1.ClusterInfo) error {
   104  	err := c.GetItem(clusterInfoKey(server), &res)
   105  	return err
   106  }