uid_cache.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package garbagecollector
  14. import (
  15. "sync"
  16. "github.com/golang/groupcache/lru"
  17. "k8s.io/kubernetes/pkg/types"
  18. )
  19. // UIDCache is an LRU cache for uid.
  20. type UIDCache struct {
  21. mutex sync.Mutex
  22. cache *lru.Cache
  23. }
  24. // NewUIDCache returns a UIDCache.
  25. func NewUIDCache(maxCacheEntries int) *UIDCache {
  26. return &UIDCache{
  27. cache: lru.New(maxCacheEntries),
  28. }
  29. }
  30. // Add adds a uid to the cache.
  31. func (c *UIDCache) Add(uid types.UID) {
  32. c.mutex.Lock()
  33. defer c.mutex.Unlock()
  34. c.cache.Add(uid, nil)
  35. }
  36. // Has returns if a uid is in the cache.
  37. func (c *UIDCache) Has(uid types.UID) bool {
  38. c.mutex.Lock()
  39. defer c.mutex.Unlock()
  40. _, found := c.cache.Get(uid)
  41. return found
  42. }