cache.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 cache provides a generic LRU cache with versioning support.
  14. package cache
  15. import (
  16. "fmt"
  17. lru "github.com/hashicorp/golang-lru"
  18. )
  19. // Cache is a generic lru cache that allows you to
  20. // lookup values using a key and a version.
  21. // By design, this cache allows access to only a single version of a given key.
  22. // A version mismatch is considered a cache miss and the key gets evicted if it exists.
  23. // ContainsOrAdd is first-writer-wins: any existing version counts as present.
  24. // When a key is evicted an optional cleanup function is called.
  25. type Cache[T any] struct {
  26. lru *lru.Cache
  27. size int
  28. cleanupFunc cleanupFunc[T]
  29. }
  30. // Key is the cache lookup key.
  31. type Key struct {
  32. Name string
  33. Namespace string
  34. Kind string
  35. }
  36. type value[T any] struct {
  37. Version string
  38. Client T
  39. }
  40. type cleanupFunc[T any] func(client T)
  41. // New constructs a new lru cache with the desired size and cleanup func.
  42. func New[T any](size int, cleanup cleanupFunc[T]) (*Cache[T], error) {
  43. lruCache, err := lru.NewWithEvict(size, func(_, val any) {
  44. if cleanup == nil {
  45. return
  46. }
  47. cleanup(val.(value[T]).Client)
  48. })
  49. if err != nil {
  50. return nil, fmt.Errorf("unable to create lru: %w", err)
  51. }
  52. return &Cache[T]{
  53. lru: lruCache,
  54. size: size,
  55. cleanupFunc: cleanup,
  56. }, nil
  57. }
  58. // Must creates a new lru cache with the desired size and cleanup func
  59. // This function panics if a error occurrs.
  60. func Must[T any](size int, cleanup cleanupFunc[T]) *Cache[T] {
  61. c, err := New(size, cleanup)
  62. if err != nil {
  63. panic(err)
  64. }
  65. return c
  66. }
  67. // Get retrieves the desired value using the key and
  68. // compares the version. If there is a mismatch
  69. // it is considered a cache miss and the existing key is purged.
  70. func (c *Cache[T]) Get(version string, key Key) (T, bool) {
  71. val, ok := c.lru.Get(key)
  72. if ok {
  73. cachedClient := val.(value[T])
  74. if cachedClient.Version == version {
  75. return cachedClient.Client, true
  76. }
  77. c.lru.Remove(key)
  78. }
  79. return value[T]{}.Client, false
  80. }
  81. // Add adds a new value for the given key/version.
  82. func (c *Cache[T]) Add(version string, key Key, client T) {
  83. c.lru.Add(key, value[T]{Version: version, Client: client})
  84. }
  85. // ContainsOrAdd atomically checks whether the key exists and adds the value if
  86. // it does not. An existing key counts as present even when its version differs,
  87. // preventing a concurrent constructor from overwriting the cached value.
  88. // It returns true when the key already exists. Rejected values are not passed
  89. // to the cleanup function because they never become owned by the cache.
  90. func (c *Cache[T]) ContainsOrAdd(version string, key Key, client T) bool {
  91. exists, _ := c.lru.ContainsOrAdd(key, value[T]{Version: version, Client: client})
  92. return exists
  93. }
  94. // Contains returns true if a value with the given key exists.
  95. func (c *Cache[T]) Contains(key Key) bool {
  96. return c.lru.Contains(key)
  97. }