mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-05 23:47:50 +00:00
Add poller to cache.
This commit is contained in:
48
pkg/client/cache/store.go
vendored
48
pkg/client/cache/store.go
vendored
@@ -18,32 +18,47 @@ package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||
)
|
||||
|
||||
// Store is a generic object storage interface. Reflector knows how to watch a server
|
||||
// and update a store. A generic store is provided, which allows Reflector to be used
|
||||
// as a local caching system, and an LRU store, which allows Reflector to work like a
|
||||
// queue of items yet to be processed.
|
||||
type Store interface {
|
||||
Add(id string, obj interface{})
|
||||
Update(id string, obj interface{})
|
||||
Delete(id string)
|
||||
List() []interface{}
|
||||
Contains() util.StringSet
|
||||
Get(id string) (item interface{}, exists bool)
|
||||
}
|
||||
|
||||
type cache struct {
|
||||
lock sync.RWMutex
|
||||
items map[string]interface{}
|
||||
}
|
||||
|
||||
// Add inserts an item into the cache.
|
||||
func (c *cache) Add(ID string, obj interface{}) {
|
||||
func (c *cache) Add(id string, obj interface{}) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.items[ID] = obj
|
||||
c.items[id] = obj
|
||||
}
|
||||
|
||||
// Update sets an item in the cache to its updated state.
|
||||
func (c *cache) Update(ID string, obj interface{}) {
|
||||
func (c *cache) Update(id string, obj interface{}) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
c.items[ID] = obj
|
||||
c.items[id] = obj
|
||||
}
|
||||
|
||||
// Delete removes an item from the cache.
|
||||
func (c *cache) Delete(ID string, obj interface{}) {
|
||||
func (c *cache) Delete(id string) {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
delete(c.items, ID)
|
||||
delete(c.items, id)
|
||||
}
|
||||
|
||||
// List returns a list of all the items.
|
||||
@@ -58,12 +73,25 @@ func (c *cache) List() []interface{} {
|
||||
return list
|
||||
}
|
||||
|
||||
// Get returns the requested item, or sets exists=false.
|
||||
// Get is completely threadsafe as long as you treat all items as immutable.
|
||||
func (c *cache) Get(ID string) (item interface{}, exists bool) {
|
||||
// Contains returns a util.StringSet containing all IDs of stored the items.
|
||||
// This is a snapshot of a moment in time, and one should keep in mind that
|
||||
// other go routines can add or remove items after you call this.
|
||||
func (c *cache) Contains() util.StringSet {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
item, exists = c.items[ID]
|
||||
set := util.StringSet{}
|
||||
for id := range c.items {
|
||||
set.Insert(id)
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// Get returns the requested item, or sets exists=false.
|
||||
// Get is completely threadsafe as long as you treat all items as immutable.
|
||||
func (c *cache) Get(id string) (item interface{}, exists bool) {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
item, exists = c.items[id]
|
||||
return item, exists
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user