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

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