cache.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 cache
  14. import (
  15. "fmt"
  16. lru "github.com/hashicorp/golang-lru"
  17. )
  18. // Cache is a generic lru cache that allows you to
  19. // lookup values using a key and a version.
  20. // By design, this cache allows access to only a single version of a given key.
  21. // A version mismatch is considered a cache miss and the key gets evicted if it exists.
  22. // When a key is evicted a optional cleanup function is called.
  23. type Cache[T any] struct {
  24. lru *lru.Cache
  25. size int
  26. cleanupFunc cleanupFunc[T]
  27. }
  28. // Key is the cache lookup key.
  29. type Key struct {
  30. Name string
  31. Namespace string
  32. Kind string
  33. }
  34. type value[T any] struct {
  35. Version string
  36. Client T
  37. }
  38. type cleanupFunc[T any] func(client T)
  39. // New constructs a new lru cache with the desired size and cleanup func.
  40. func New[T any](size int, cleanup cleanupFunc[T]) (*Cache[T], error) {
  41. lruCache, err := lru.NewWithEvict(size, func(_, val any) {
  42. if cleanup == nil {
  43. return
  44. }
  45. cleanup(val.(value[T]).Client)
  46. })
  47. if err != nil {
  48. return nil, fmt.Errorf("unable to create lru: %w", err)
  49. }
  50. return &Cache[T]{
  51. lru: lruCache,
  52. size: size,
  53. cleanupFunc: cleanup,
  54. }, nil
  55. }
  56. // Must creates a new lru cache with the desired size and cleanup func
  57. // This function panics if a error occurrs.
  58. func Must[T any](size int, cleanup cleanupFunc[T]) *Cache[T] {
  59. c, err := New(size, cleanup)
  60. if err != nil {
  61. panic(err)
  62. }
  63. return c
  64. }
  65. // Get retrieves the desired value using the key and
  66. // compares the version. If there is a mismatch
  67. // it is considered a cache miss and the existing key is purged.
  68. func (c *Cache[T]) Get(version string, key Key) (T, bool) {
  69. val, ok := c.lru.Get(key)
  70. if ok {
  71. cachedClient := val.(value[T])
  72. if cachedClient.Version == version {
  73. return cachedClient.Client, true
  74. }
  75. c.lru.Remove(key)
  76. }
  77. return value[T]{}.Client, false
  78. }
  79. // Add adds a new value for the given key/version.
  80. func (c *Cache[T]) Add(version string, key Key, client T) {
  81. c.lru.Add(key, value[T]{Version: version, Client: client})
  82. }
  83. // Contains returns true if a value with the given key exists.
  84. func (c *Cache[T]) Contains(key Key) bool {
  85. return c.lru.Contains(key)
  86. }