1 /* 2 Copyright 2016 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 garbagecollector 18 19 import ( 20 "sync" 21 22 "github.com/golang/groupcache/lru" 23 ) 24 25 // ReferenceCache is an LRU cache for uid. 26 type ReferenceCache struct { 27 mutex sync.Mutex 28 cache *lru.Cache 29 } 30 31 // NewReferenceCache returns a ReferenceCache. 32 func NewReferenceCache(maxCacheEntries int) *ReferenceCache { 33 return &ReferenceCache{ 34 cache: lru.New(maxCacheEntries), 35 } 36 } 37 38 // Add adds a uid to the cache. 39 func (c *ReferenceCache) Add(reference objectReference) { 40 c.mutex.Lock() 41 defer c.mutex.Unlock() 42 c.cache.Add(reference, nil) 43 } 44 45 // Has returns if a uid is in the cache. 46 func (c *ReferenceCache) Has(reference objectReference) bool { 47 c.mutex.Lock() 48 defer c.mutex.Unlock() 49 _, found := c.cache.Get(reference) 50 return found 51 } 52