istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/ledger/trie_cache.go (about) 1 // Copyright 2019 Istio Authors 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ledger 16 17 import ( 18 "sync" 19 "time" 20 21 "istio.io/istio/pkg/cache" 22 ) 23 24 type cacheDB struct { 25 // updatedNodes that have will be flushed to disk 26 updatedNodes byteCache 27 // updatedMux is a lock for updatedNodes 28 updatedMux sync.RWMutex 29 } 30 31 // byteCache implements a modified ExpiringCache interface, returning byte arrays 32 // for ease of integration with smt calls. 33 type byteCache struct { 34 cache cache.ExpiringCache 35 } 36 37 // Set inserts an entry in the cache. This will replace any entry with 38 // the same key that is already in the cache. The entry may be automatically 39 // expunged from the cache at some point, depending on the eviction policies 40 // of the cache and the options specified when the cache was created. 41 func (b *byteCache) Set(key hash, value [][]byte) { 42 b.cache.Set(key, value) 43 } 44 45 // Get retrieves the value associated with the supplied key if the key 46 // is present in the cache. 47 func (b *byteCache) Get(key hash) (value [][]byte, ok bool) { 48 ivalue, ok := b.cache.Get(key) 49 if ok { 50 value, _ = ivalue.([][]byte) 51 } 52 return 53 } 54 55 // SetWithExpiration inserts an entry in the cache with a requested expiration time. 56 // This will replace any entry with the same key that is already in the cache. 57 // The entry will be automatically expunged from the cache at or slightly after the 58 // requested expiration time. 59 func (b *byteCache) SetWithExpiration(key hash, value [][]byte, expiration time.Duration) { 60 b.cache.SetWithExpiration(key, value, expiration) 61 }