1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package resourceclaim 18 19 import ( 20 "sync" 21 22 "github.com/golang/groupcache/lru" 23 24 "k8s.io/apimachinery/pkg/types" 25 ) 26 27 // uidCache is an LRU cache for uid. 28 type uidCache struct { 29 mutex sync.Mutex 30 cache *lru.Cache 31 } 32 33 // newUIDCache returns a uidCache. 34 func newUIDCache(maxCacheEntries int) *uidCache { 35 return &uidCache{ 36 cache: lru.New(maxCacheEntries), 37 } 38 } 39 40 // Add adds a uid to the cache. 41 func (c *uidCache) Add(uid types.UID) { 42 c.mutex.Lock() 43 defer c.mutex.Unlock() 44 c.cache.Add(uid, nil) 45 } 46 47 // Has returns if a uid is in the cache. 48 func (c *uidCache) Has(uid types.UID) bool { 49 c.mutex.Lock() 50 defer c.mutex.Unlock() 51 _, found := c.cache.Get(uid) 52 return found 53 } 54