|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2025 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package cache |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "encoding/json" |
| 26 | + goStrings "strings" |
| 27 | + "sync" |
| 28 | + |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/cache" |
| 31 | +) |
| 32 | + |
| 33 | +func NewRemoteCache[T cache.RemoteCacheObject]() cache.RemoteCache[T] { |
| 34 | + return &localRemoteCache[T]{ |
| 35 | + objects: map[string]json.RawMessage{}, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +type localRemoteCache[T cache.RemoteCacheObject] struct { |
| 40 | + lock sync.Mutex |
| 41 | + |
| 42 | + objects map[string]json.RawMessage |
| 43 | +} |
| 44 | + |
| 45 | +func (l *localRemoteCache[T]) Put(ctx context.Context, key string, obj T) error { |
| 46 | + l.lock.Lock() |
| 47 | + defer l.lock.Unlock() |
| 48 | + |
| 49 | + data, err := json.Marshal(obj) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + l.objects[key] = data |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (l *localRemoteCache[T]) Get(ctx context.Context, key string) (T, bool, error) { |
| 59 | + l.lock.Lock() |
| 60 | + defer l.lock.Unlock() |
| 61 | + |
| 62 | + data, ok := l.objects[key] |
| 63 | + if !ok { |
| 64 | + return util.Default[T](), false, nil |
| 65 | + } |
| 66 | + |
| 67 | + var obj T |
| 68 | + |
| 69 | + if err := json.Unmarshal(data, &obj); err != nil { |
| 70 | + return obj, false, err |
| 71 | + } |
| 72 | + |
| 73 | + return obj, true, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (l *localRemoteCache[T]) Remove(ctx context.Context, key string) (bool, error) { |
| 77 | + l.lock.Lock() |
| 78 | + defer l.lock.Unlock() |
| 79 | + |
| 80 | + if _, ok := l.objects[key]; ok { |
| 81 | + delete(l.objects, key) |
| 82 | + return true, nil |
| 83 | + } |
| 84 | + |
| 85 | + return false, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (l *localRemoteCache[T]) Invalidate(ctx context.Context, key string) { |
| 89 | + l.lock.Lock() |
| 90 | + defer l.lock.Unlock() |
| 91 | + |
| 92 | + // Nothing to do with reload |
| 93 | +} |
| 94 | + |
| 95 | +func (l *localRemoteCache[T]) List(ctx context.Context, size int, prefix string) (util.NextIterator[[]string], error) { |
| 96 | + l.lock.Lock() |
| 97 | + defer l.lock.Unlock() |
| 98 | + |
| 99 | + keys := util.MapKeys(l.objects) |
| 100 | + keys = util.FilterList(keys, func(key string) bool { |
| 101 | + return goStrings.HasPrefix(key, prefix) |
| 102 | + }) |
| 103 | + |
| 104 | + return util.NewStaticNextIterator(util.BatchList(size, keys)...), nil |
| 105 | +} |
0 commit comments