github.com/argoproj/argo-cd/v2@v2.10.9/server/cache/cache.go (about)

     1  package cache
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"math"
     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  	appstatecache "github.com/argoproj/argo-cd/v2/util/cache/appstate"
    15  	"github.com/argoproj/argo-cd/v2/util/env"
    16  )
    17  
    18  var ErrCacheMiss = appstatecache.ErrCacheMiss
    19  
    20  type Cache struct {
    21  	cache                           *appstatecache.Cache
    22  	connectionStatusCacheExpiration time.Duration
    23  	oidcCacheExpiration             time.Duration
    24  	loginAttemptsExpiration         time.Duration
    25  }
    26  
    27  func NewCache(
    28  	cache *appstatecache.Cache,
    29  	connectionStatusCacheExpiration time.Duration,
    30  	oidcCacheExpiration time.Duration,
    31  	loginAttemptsExpiration time.Duration,
    32  ) *Cache {
    33  	return &Cache{cache, connectionStatusCacheExpiration, oidcCacheExpiration, loginAttemptsExpiration}
    34  }
    35  
    36  func AddCacheFlagsToCmd(cmd *cobra.Command, opts ...func(client *redis.Client)) func() (*Cache, error) {
    37  	var connectionStatusCacheExpiration time.Duration
    38  	var oidcCacheExpiration time.Duration
    39  	var loginAttemptsExpiration time.Duration
    40  
    41  	cmd.Flags().DurationVar(&connectionStatusCacheExpiration, "connection-status-cache-expiration", env.ParseDurationFromEnv("ARGOCD_SERVER_CONNECTION_STATUS_CACHE_EXPIRATION", 1*time.Hour, 0, math.MaxInt64), "Cache expiration for cluster/repo connection status")
    42  	cmd.Flags().DurationVar(&oidcCacheExpiration, "oidc-cache-expiration", env.ParseDurationFromEnv("ARGOCD_SERVER_OIDC_CACHE_EXPIRATION", 3*time.Minute, 0, math.MaxInt64), "Cache expiration for OIDC state")
    43  	cmd.Flags().DurationVar(&loginAttemptsExpiration, "login-attempts-expiration", env.ParseDurationFromEnv("ARGOCD_SERVER_LOGIN_ATTEMPTS_EXPIRATION", 24*time.Hour, 0, math.MaxInt64), "Cache expiration for failed login attempts")
    44  
    45  	fn := appstatecache.AddCacheFlagsToCmd(cmd, opts...)
    46  
    47  	return func() (*Cache, error) {
    48  		cache, err := fn()
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  
    53  		return NewCache(cache, connectionStatusCacheExpiration, oidcCacheExpiration, loginAttemptsExpiration), nil
    54  	}
    55  }
    56  
    57  func (c *Cache) GetAppResourcesTree(appName string, res *appv1.ApplicationTree) error {
    58  	return c.cache.GetAppResourcesTree(appName, res)
    59  }
    60  
    61  func (c *Cache) OnAppResourcesTreeChanged(ctx context.Context, appName string, callback func() error) error {
    62  	return c.cache.OnAppResourcesTreeChanged(ctx, appName, callback)
    63  }
    64  
    65  func (c *Cache) GetAppManagedResources(appName string, res *[]*appv1.ResourceDiff) error {
    66  	return c.cache.GetAppManagedResources(appName, res)
    67  }
    68  
    69  func (c *Cache) SetRepoConnectionState(repo string, state *appv1.ConnectionState) error {
    70  	return c.cache.SetItem(repoConnectionStateKey(repo), &state, c.connectionStatusCacheExpiration, state == nil)
    71  }
    72  
    73  func repoConnectionStateKey(repo string) string {
    74  	return fmt.Sprintf("repo|%s|connection-state", repo)
    75  }
    76  
    77  func (c *Cache) GetRepoConnectionState(repo string) (appv1.ConnectionState, error) {
    78  	res := appv1.ConnectionState{}
    79  	err := c.cache.GetItem(repoConnectionStateKey(repo), &res)
    80  	return res, err
    81  }
    82  
    83  func (c *Cache) GetClusterInfo(server string, res *appv1.ClusterInfo) error {
    84  	return c.cache.GetClusterInfo(server, res)
    85  }
    86  
    87  func (c *Cache) SetClusterInfo(server string, res *appv1.ClusterInfo) error {
    88  	return c.cache.SetClusterInfo(server, res)
    89  }
    90  
    91  func (c *Cache) GetCache() *cacheutil.Cache {
    92  	return c.cache.Cache
    93  }