cache.go 2.7 KB

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