github.com/argoproj/argo-cd@v1.8.7/server/cache/cache.go (about) 1 package cache 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 "github.com/go-redis/redis/v8" 9 "github.com/spf13/cobra" 10 11 appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1" 12 cacheutil "github.com/argoproj/argo-cd/util/cache" 13 appstatecache "github.com/argoproj/argo-cd/util/cache/appstate" 14 "github.com/argoproj/argo-cd/util/oidc" 15 "github.com/argoproj/argo-cd/util/session" 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", 1*time.Hour, "Cache expiration for cluster/repo connection status") 42 cmd.Flags().DurationVar(&oidcCacheExpiration, "oidc-cache-expiration", 3*time.Minute, "Cache expiration for OIDC state") 43 cmd.Flags().DurationVar(&loginAttemptsExpiration, "login-attempts-expiration", 24*time.Hour, "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) GetLoginAttempts(attempts *map[string]session.LoginAttempts) error { 70 return c.cache.GetItem("session|login.attempts", attempts) 71 } 72 73 func (c *Cache) SetLoginAttempts(attempts map[string]session.LoginAttempts) error { 74 return c.cache.SetItem("session|login.attempts", attempts, c.loginAttemptsExpiration, attempts == nil) 75 } 76 77 func (c *Cache) SetRepoConnectionState(repo string, state *appv1.ConnectionState) error { 78 return c.cache.SetItem(repoConnectionStateKey(repo), &state, c.connectionStatusCacheExpiration, state == nil) 79 } 80 81 func repoConnectionStateKey(repo string) string { 82 return fmt.Sprintf("repo|%s|connection-state", repo) 83 } 84 85 func (c *Cache) GetRepoConnectionState(repo string) (appv1.ConnectionState, error) { 86 res := appv1.ConnectionState{} 87 err := c.cache.GetItem(repoConnectionStateKey(repo), &res) 88 return res, err 89 } 90 91 func (c *Cache) GetClusterInfo(server string, res *appv1.ClusterInfo) error { 92 return c.cache.GetClusterInfo(server, res) 93 } 94 95 func (c *Cache) SetClusterInfo(server string, res *appv1.ClusterInfo) error { 96 return c.cache.SetClusterInfo(server, res) 97 } 98 99 func oidcStateKey(key string) string { 100 return fmt.Sprintf("oidc|%s", key) 101 } 102 103 func (c *Cache) GetOIDCState(key string) (*oidc.OIDCState, error) { 104 res := oidc.OIDCState{} 105 err := c.cache.GetItem(oidcStateKey(key), &res) 106 return &res, err 107 } 108 109 func (c *Cache) SetOIDCState(key string, state *oidc.OIDCState) error { 110 return c.cache.SetItem(oidcStateKey(key), state, c.oidcCacheExpiration, state == nil) 111 } 112 113 func (c *Cache) GetCache() *cacheutil.Cache { 114 return c.cache.Cache 115 }