|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/maypok86/otter/v2" |
| 8 | +) |
| 9 | + |
| 10 | +const DEFAULT_CACHE_SIZE = 70_000 |
| 11 | + |
| 12 | +// Cache provides a simple key-value cache for Nautobot resources |
| 13 | +type Cache struct { |
| 14 | + store *otter.Cache[string, any] |
| 15 | +} |
| 16 | + |
| 17 | +// New creates a new cache instance |
| 18 | +// New creates a new cache instance with the specified maximum size |
| 19 | +func New(maxSize int) (*Cache, error) { |
| 20 | + if maxSize <= 0 { |
| 21 | + maxSize = DEFAULT_CACHE_SIZE |
| 22 | + } |
| 23 | + |
| 24 | + store, err := otter.New(&otter.Options[string, any]{ |
| 25 | + MaximumSize: maxSize, |
| 26 | + ExpiryCalculator: otter.ExpiryWriting[string, any](time.Hour), |
| 27 | + }) |
| 28 | + if err != nil { |
| 29 | + return nil, fmt.Errorf("failed to create cache: %w", err) |
| 30 | + } |
| 31 | + |
| 32 | + return &Cache{store: store}, nil |
| 33 | +} |
| 34 | + |
| 35 | +// Get retrieves a value from cache by key |
| 36 | +func (c *Cache) Get(key string) (any, bool) { |
| 37 | + return c.store.GetIfPresent(key) |
| 38 | +} |
| 39 | + |
| 40 | +// Set stores a value in cache with the given key |
| 41 | +func (c *Cache) Set(key string, value any) { |
| 42 | + c.store.Set(key, value) |
| 43 | +} |
| 44 | + |
| 45 | +// SetCollection stores a collection of items in cache |
| 46 | +func (c *Cache) SetCollection(resourceType string, items any) { |
| 47 | + c.Set(BuildKey(resourceType), items) |
| 48 | +} |
| 49 | + |
| 50 | +// FindByName searches a cached collection for an item by name |
| 51 | +func FindByName[T any](c *Cache, resourceType, name string, getName func(T) string) (T, bool) { |
| 52 | + var zero T |
| 53 | + val, ok := c.Get(BuildKey(resourceType)) |
| 54 | + if !ok { |
| 55 | + return zero, false |
| 56 | + } |
| 57 | + |
| 58 | + items, ok := val.([]T) |
| 59 | + if !ok { |
| 60 | + return zero, false |
| 61 | + } |
| 62 | + |
| 63 | + for _, item := range items { |
| 64 | + if getName(item) == name { |
| 65 | + return item, true |
| 66 | + } |
| 67 | + } |
| 68 | + return zero, false |
| 69 | +} |
| 70 | + |
| 71 | +// FindByID searches a cached collection for an item by ID |
| 72 | +func FindByID[T any](c *Cache, resourceType, id string, getID func(T) *string) (T, bool) { |
| 73 | + var zero T |
| 74 | + val, ok := c.Get(BuildKey(resourceType)) |
| 75 | + if !ok { |
| 76 | + return zero, false |
| 77 | + } |
| 78 | + |
| 79 | + items, ok := val.([]T) |
| 80 | + if !ok { |
| 81 | + return zero, false |
| 82 | + } |
| 83 | + |
| 84 | + for _, item := range items { |
| 85 | + itemID := getID(item) |
| 86 | + if itemID != nil && *itemID == id { |
| 87 | + return item, true |
| 88 | + } |
| 89 | + } |
| 90 | + return zero, false |
| 91 | +} |
| 92 | + |
| 93 | +// AddToCollection adds a new item to a cached collection |
| 94 | +func AddToCollection[T any](c *Cache, resourceType string, item T) { |
| 95 | + key := BuildKey(resourceType) |
| 96 | + val, ok := c.Get(key) |
| 97 | + if !ok { |
| 98 | + c.Set(key, []T{item}) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + items, ok := val.([]T) |
| 103 | + if !ok { |
| 104 | + c.Set(key, []T{item}) |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + items = append(items, item) |
| 109 | + c.Set(key, items) |
| 110 | +} |
| 111 | + |
| 112 | +// UpdateInCollection updates an existing item in a cached collection |
| 113 | +func UpdateInCollection[T any](c *Cache, resourceType string, updatedItem T, matchFunc func(T) bool) { |
| 114 | + key := BuildKey(resourceType) |
| 115 | + val, ok := c.Get(key) |
| 116 | + if !ok { |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + items, ok := val.([]T) |
| 121 | + if !ok { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + // Find and update the item |
| 126 | + for i, item := range items { |
| 127 | + if matchFunc(item) { |
| 128 | + items[i] = updatedItem |
| 129 | + c.Set(key, items) |
| 130 | + return |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// RemoveFromCollection removes an item from a cached collection |
| 136 | +func RemoveFromCollection[T any](c *Cache, resourceType string, matchFunc func(T) bool) { |
| 137 | + key := BuildKey(resourceType) |
| 138 | + val, ok := c.Get(key) |
| 139 | + if !ok { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + items, ok := val.([]T) |
| 144 | + if !ok { |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + // Filter out the item |
| 149 | + filtered := make([]T, 0, len(items)) |
| 150 | + for _, item := range items { |
| 151 | + if !matchFunc(item) { |
| 152 | + filtered = append(filtered, item) |
| 153 | + } |
| 154 | + } |
| 155 | + c.Set(key, filtered) |
| 156 | +} |
| 157 | + |
| 158 | +// Delete removes a value from cache by key |
| 159 | +func (c *Cache) Delete(key string) { |
| 160 | + c.store.Invalidate(key) |
| 161 | +} |
| 162 | + |
| 163 | +// Clear removes all entries from the cache |
| 164 | +func (c *Cache) Clear() { |
| 165 | + c.store.InvalidateAll() |
| 166 | +} |
| 167 | + |
| 168 | +// Close closes the cache and releases resources |
| 169 | +func (c *Cache) Close() { |
| 170 | + c.store.StopAllGoroutines() |
| 171 | +} |
| 172 | + |
| 173 | +// BuildKey creates a cache key for a resource collection |
| 174 | +func BuildKey(resourceType string) string { |
| 175 | + return resourceType |
| 176 | +} |
0 commit comments