github.com/argoproj/argo-cd/v3@v3.2.1/server/cache/cache.go (about) 1 package cache 2 3 import ( 4 "context" 5 "fmt" 6 "math" 7 "time" 8 9 "github.com/spf13/cobra" 10 11 appv1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1" 12 cacheutil "github.com/argoproj/argo-cd/v3/util/cache" 13 appstatecache "github.com/argoproj/argo-cd/v3/util/cache/appstate" 14 "github.com/argoproj/argo-cd/v3/util/env" 15 ) 16 17 var ErrCacheMiss = appstatecache.ErrCacheMiss 18 19 type Cache struct { 20 cache *appstatecache.Cache 21 connectionStatusCacheExpiration time.Duration 22 oidcCacheExpiration time.Duration 23 } 24 25 func NewCache( 26 cache *appstatecache.Cache, 27 connectionStatusCacheExpiration time.Duration, 28 oidcCacheExpiration time.Duration, 29 ) *Cache { 30 return &Cache{cache, connectionStatusCacheExpiration, oidcCacheExpiration} 31 } 32 33 func AddCacheFlagsToCmd(cmd *cobra.Command, opts ...cacheutil.Options) func() (*Cache, error) { 34 var connectionStatusCacheExpiration time.Duration 35 var oidcCacheExpiration time.Duration 36 var loginAttemptsExpiration time.Duration 37 38 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") 39 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") 40 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. DEPRECATED: this flag is unused and will be removed in a future version.") 41 42 fn := appstatecache.AddCacheFlagsToCmd(cmd, opts...) 43 44 return func() (*Cache, error) { 45 cache, err := fn() 46 if err != nil { 47 return nil, err 48 } 49 50 return NewCache(cache, connectionStatusCacheExpiration, oidcCacheExpiration), nil 51 } 52 } 53 54 func (c *Cache) GetAppResourcesTree(appName string, res *appv1.ApplicationTree) error { 55 return c.cache.GetAppResourcesTree(appName, res) 56 } 57 58 func (c *Cache) OnAppResourcesTreeChanged(ctx context.Context, appName string, callback func() error) error { 59 return c.cache.OnAppResourcesTreeChanged(ctx, appName, callback) 60 } 61 62 func (c *Cache) GetAppManagedResources(appName string, res *[]*appv1.ResourceDiff) error { 63 return c.cache.GetAppManagedResources(appName, res) 64 } 65 66 func (c *Cache) SetRepoConnectionState(repo string, project string, state *appv1.ConnectionState) error { 67 return c.cache.SetItem(repoConnectionStateKey(repo, project), &state, c.connectionStatusCacheExpiration, state == nil) 68 } 69 70 func repoConnectionStateKey(repo string, project string) string { 71 return fmt.Sprintf("repo|%s|%s|connection-state", repo, project) 72 } 73 74 func (c *Cache) GetRepoConnectionState(repo string, project string) (appv1.ConnectionState, error) { 75 res := appv1.ConnectionState{} 76 err := c.cache.GetItem(repoConnectionStateKey(repo, project), &res) 77 return res, err 78 } 79 80 func (c *Cache) GetClusterInfo(server string, res *appv1.ClusterInfo) error { 81 return c.cache.GetClusterInfo(server, res) 82 } 83 84 func (c *Cache) SetClusterInfo(server string, res *appv1.ClusterInfo) error { 85 return c.cache.SetClusterInfo(server, res) 86 } 87 88 func (c *Cache) GetCache() *cacheutil.Cache { 89 return c.cache.Cache 90 }