2014-08-03 07:00:42 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2014-08-18 21:47:20 +00:00
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
2014-08-03 07:00:42 +00:00
|
|
|
)
|
|
|
|
|
2014-08-18 21:47:20 +00:00
|
|
|
// 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)
|
2014-09-16 01:10:10 +00:00
|
|
|
|
|
|
|
// Replace will delete the contents of the store, using instead the
|
|
|
|
// given map. Store takes ownership of the map, you should not reference
|
|
|
|
// it after calling this function.
|
|
|
|
Replace(idToObj map[string]interface{})
|
2014-08-18 21:47:20 +00:00
|
|
|
}
|
|
|
|
|
2014-08-03 07:00:42 +00:00
|
|
|
type cache struct {
|
|
|
|
lock sync.RWMutex
|
|
|
|
items map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add inserts an item into the cache.
|
2014-08-18 21:47:20 +00:00
|
|
|
func (c *cache) Add(id string, obj interface{}) {
|
2014-08-03 07:00:42 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2014-08-18 21:47:20 +00:00
|
|
|
c.items[id] = obj
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update sets an item in the cache to its updated state.
|
2014-08-18 21:47:20 +00:00
|
|
|
func (c *cache) Update(id string, obj interface{}) {
|
2014-08-03 07:00:42 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2014-08-18 21:47:20 +00:00
|
|
|
c.items[id] = obj
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes an item from the cache.
|
2014-08-18 21:47:20 +00:00
|
|
|
func (c *cache) Delete(id string) {
|
2014-08-03 07:00:42 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
2014-08-18 21:47:20 +00:00
|
|
|
delete(c.items, id)
|
2014-08-03 07:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List returns a list of all the items.
|
2014-08-03 22:36:36 +00:00
|
|
|
// List is completely threadsafe as long as you treat all items as immutable.
|
2014-08-03 07:00:42 +00:00
|
|
|
func (c *cache) List() []interface{} {
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
list := make([]interface{}, 0, len(c.items))
|
|
|
|
for _, item := range c.items {
|
|
|
|
list = append(list, item)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2014-08-18 21:47:20 +00:00
|
|
|
// 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()
|
|
|
|
set := util.StringSet{}
|
|
|
|
for id := range c.items {
|
|
|
|
set.Insert(id)
|
|
|
|
}
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2014-08-03 07:00:42 +00:00
|
|
|
// Get returns the requested item, or sets exists=false.
|
2014-08-03 22:36:36 +00:00
|
|
|
// Get is completely threadsafe as long as you treat all items as immutable.
|
2014-08-18 21:47:20 +00:00
|
|
|
func (c *cache) Get(id string) (item interface{}, exists bool) {
|
2014-08-03 07:00:42 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2014-08-18 21:47:20 +00:00
|
|
|
item, exists = c.items[id]
|
2014-08-03 07:00:42 +00:00
|
|
|
return item, exists
|
|
|
|
}
|
|
|
|
|
2014-09-16 01:10:10 +00:00
|
|
|
// Replace will delete the contents of 'c', using instead the given map.
|
|
|
|
// 'c' takes ownership of the map, you should not reference the map again
|
|
|
|
// after calling this function.
|
|
|
|
func (c *cache) Replace(idToObj map[string]interface{}) {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
c.items = idToObj
|
|
|
|
}
|
|
|
|
|
2014-08-03 07:00:42 +00:00
|
|
|
// NewStore returns a Store implemented simply with a map and a lock.
|
|
|
|
func NewStore() Store {
|
|
|
|
return &cache{items: map[string]interface{}{}}
|
|
|
|
}
|