cache.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package cache
  13. import (
  14. "fmt"
  15. lru "github.com/hashicorp/golang-lru"
  16. )
  17. // Cache is a generic lru cache that allows you to
  18. // lookup values using a key and a version.
  19. // By design, this cache allows access to only a single version of a given key.
  20. // A version mismatch is considered a cache miss and the key gets evicted if it exists.
  21. // When a key is evicted a optional cleanup function is called.
  22. type Cache[T any] struct {
  23. lru *lru.Cache
  24. size int
  25. cleanupFunc cleanupFunc[T]
  26. }
  27. // Key is the cache lookup key.
  28. type Key struct {
  29. Name string
  30. Namespace string
  31. Kind string
  32. }
  33. type value[T any] struct {
  34. Version string
  35. Client T
  36. }
  37. type cleanupFunc[T any] func(client T)
  38. // New constructs a new lru cache with the desired size and cleanup func.
  39. func New[T any](size int, cleanup cleanupFunc[T]) (*Cache[T], error) {
  40. lruCache, err := lru.NewWithEvict(size, func(_, val any) {
  41. if cleanup == nil {
  42. return
  43. }
  44. cleanup(val.(value[T]).Client)
  45. })
  46. if err != nil {
  47. return nil, fmt.Errorf("unable to create lru: %w", err)
  48. }
  49. return &Cache[T]{
  50. lru: lruCache,
  51. size: size,
  52. cleanupFunc: cleanup,
  53. }, nil
  54. }
  55. // Must creates a new lru cache with the desired size and cleanup func
  56. // This function panics if a error occurrs.
  57. func Must[T any](size int, cleanup cleanupFunc[T]) *Cache[T] {
  58. c, err := New(size, cleanup)
  59. if err != nil {
  60. panic(err)
  61. }
  62. return c
  63. }
  64. // Get retrieves the desired value using the key and
  65. // compares the version. If there is a mismatch
  66. // it is considered a cache miss and the existing key is purged.
  67. func (c *Cache[T]) Get(version string, key Key) (T, bool) {
  68. val, ok := c.lru.Get(key)
  69. if ok {
  70. cachedClient := val.(value[T])
  71. if cachedClient.Version == version {
  72. return cachedClient.Client, true
  73. }
  74. c.lru.Remove(key)
  75. }
  76. return value[T]{}.Client, false
  77. }
  78. // Add adds a new value for the given key/version.
  79. func (c *Cache[T]) Add(version string, key Key, client T) {
  80. c.lru.Add(key, value[T]{Version: version, Client: client})
  81. }
  82. // Contains returns true if a value with the given key exists.
  83. func (c *Cache[T]) Contains(key Key) bool {
  84. return c.lru.Contains(key)
  85. }