cache.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package doppler
  14. import (
  15. "sync"
  16. dclient "github.com/external-secrets/external-secrets/providers/v1/doppler/client"
  17. "github.com/external-secrets/external-secrets/runtime/cache"
  18. )
  19. type cacheEntry struct {
  20. etag string
  21. secrets dclient.Secrets
  22. body []byte
  23. }
  24. // Constant version because entries are invalidated explicitly on mutations.
  25. const etagCacheVersion = ""
  26. type secretsCache struct {
  27. cache *cache.Cache[*cacheEntry]
  28. keys map[string]map[cache.Key]struct{}
  29. keysMu sync.RWMutex
  30. }
  31. func newSecretsCache(size int) *secretsCache {
  32. if size <= 0 {
  33. return nil
  34. }
  35. return &secretsCache{
  36. cache: cache.Must(size, func(_ *cacheEntry) {
  37. // No cleanup needed on eviction — entries are plain data with no external resources.
  38. }),
  39. keys: make(map[string]map[cache.Key]struct{}),
  40. }
  41. }
  42. type storeIdentity struct {
  43. namespace string
  44. name string
  45. kind string
  46. }
  47. func cacheKey(store storeIdentity, secretName string) cache.Key {
  48. name := store.name
  49. if secretName != "" {
  50. name = store.name + "|" + secretName
  51. }
  52. return cache.Key{
  53. Name: name,
  54. Namespace: store.namespace,
  55. Kind: store.kind,
  56. }
  57. }
  58. func storeKey(store storeIdentity) string {
  59. return store.namespace + "|" + store.name + "|" + store.kind
  60. }
  61. func (c *secretsCache) get(store storeIdentity, secretName string) (*cacheEntry, bool) {
  62. if c == nil || c.cache == nil {
  63. return nil, false
  64. }
  65. key := cacheKey(store, secretName)
  66. return c.cache.Get(etagCacheVersion, key)
  67. }
  68. func (c *secretsCache) set(store storeIdentity, secretName string, entry *cacheEntry) {
  69. if c == nil || c.cache == nil {
  70. return
  71. }
  72. key := cacheKey(store, secretName)
  73. c.keysMu.Lock()
  74. c.cache.Add(etagCacheVersion, key, entry)
  75. prefix := storeKey(store)
  76. if c.keys[prefix] == nil {
  77. c.keys[prefix] = make(map[cache.Key]struct{})
  78. }
  79. // Prune keys that have been evicted from the underlying cache to prevent unbounded growth.
  80. for k := range c.keys[prefix] {
  81. if !c.cache.Contains(k) {
  82. delete(c.keys[prefix], k)
  83. }
  84. }
  85. c.keys[prefix][key] = struct{}{}
  86. c.keysMu.Unlock()
  87. }
  88. func (c *secretsCache) invalidate(store storeIdentity) {
  89. if c == nil || c.cache == nil {
  90. return
  91. }
  92. c.keysMu.Lock()
  93. defer c.keysMu.Unlock()
  94. prefix := storeKey(store)
  95. keys, exists := c.keys[prefix]
  96. if !exists {
  97. return
  98. }
  99. for key := range keys {
  100. if c.cache.Contains(key) {
  101. c.cache.Get("__invalidate__", key) // wrong version triggers eviction
  102. }
  103. }
  104. delete(c.keys, prefix)
  105. }
  106. var etagCache *secretsCache