2015-01-09 22:06:30 +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 util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// T stands in for any type in TimeCache
|
|
|
|
// Should make it easy to use this as a template for an autogenerator
|
|
|
|
// if we ever start doing that.
|
|
|
|
type T interface{}
|
|
|
|
|
|
|
|
type TimeCache interface {
|
|
|
|
// Get will fetch an item from the cache if
|
|
|
|
// it is present and recent enough.
|
|
|
|
Get(key string) T
|
|
|
|
}
|
|
|
|
|
|
|
|
type timeCacheEntry struct {
|
|
|
|
item T
|
|
|
|
lastUpdate time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
type timeCache struct {
|
|
|
|
clock Clock
|
|
|
|
fillFunc func(string) T
|
|
|
|
ttl time.Duration
|
2015-01-09 23:33:58 +00:00
|
|
|
|
|
|
|
inFlight map[string]chan T
|
|
|
|
inFlightLock sync.Mutex
|
|
|
|
|
|
|
|
cache map[string]timeCacheEntry
|
|
|
|
lock sync.RWMutex
|
2015-01-09 22:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTimeCache returns a cache which calls fill to fill its entries, and
|
|
|
|
// forgets entries after ttl has passed.
|
|
|
|
func NewTimeCache(clock Clock, ttl time.Duration, fill func(key string) T) TimeCache {
|
|
|
|
return &timeCache{
|
|
|
|
clock: clock,
|
|
|
|
fillFunc: fill,
|
2015-01-09 23:33:58 +00:00
|
|
|
inFlight: map[string]chan T{},
|
2015-01-09 22:06:30 +00:00
|
|
|
cache: map[string]timeCacheEntry{},
|
|
|
|
ttl: ttl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the value of key from the cache, if it is present
|
|
|
|
// and recent enough; otherwise, it blocks while it gets the value.
|
|
|
|
func (c *timeCache) Get(key string) T {
|
2015-01-09 23:33:58 +00:00
|
|
|
if item, ok := c.get(key); ok {
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to fill the cache. Calling the function could be
|
|
|
|
// expensive, so do it while unlocked.
|
|
|
|
wait := c.fillOrWait(key)
|
|
|
|
item := <-wait
|
|
|
|
|
|
|
|
// Put it back in the channel in case there's multiple waiters
|
|
|
|
// (this channel is non-blocking)
|
|
|
|
wait <- item
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the item and true if it is found and not expired, otherwise nil and false.
|
2015-01-14 00:53:50 +00:00
|
|
|
// If this returns false, it has locked c.inFlightLock and it is caller's responsibility
|
|
|
|
// to unlock that.
|
2015-01-09 23:33:58 +00:00
|
|
|
func (c *timeCache) get(key string) (T, bool) {
|
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
2015-01-09 22:06:30 +00:00
|
|
|
data, ok := c.cache[key]
|
|
|
|
now := c.clock.Now()
|
|
|
|
if !ok || now.Sub(data.lastUpdate) > c.ttl {
|
2015-01-14 00:53:50 +00:00
|
|
|
// We must lock this while we hold c.lock-- otherwise, a writer could
|
|
|
|
// write to c.cache and remove the channel from c.inFlight before we
|
|
|
|
// manage to read c.inFlight.
|
|
|
|
c.inFlightLock.Lock()
|
2015-01-09 23:33:58 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return data.item, true
|
|
|
|
}
|
|
|
|
|
2015-01-14 00:53:50 +00:00
|
|
|
// c.inFlightLock MUST be locked before calling this. fillOrWait will unlock it.
|
2015-01-09 23:33:58 +00:00
|
|
|
func (c *timeCache) fillOrWait(key string) chan T {
|
|
|
|
defer c.inFlightLock.Unlock()
|
|
|
|
|
|
|
|
// Already a call in progress?
|
|
|
|
if current, ok := c.inFlight[key]; ok {
|
|
|
|
return current
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are the first, so we have to make the call.
|
|
|
|
result := make(chan T, 1) // non-blocking
|
|
|
|
c.inFlight[key] = result
|
|
|
|
go func() {
|
2015-01-14 00:53:50 +00:00
|
|
|
// Make potentially slow call.
|
|
|
|
// While this call is in flight, fillOrWait will
|
|
|
|
// presumably exit.
|
2015-01-09 23:33:58 +00:00
|
|
|
data := timeCacheEntry{
|
2015-01-09 22:06:30 +00:00
|
|
|
item: c.fillFunc(key),
|
2015-01-09 23:33:58 +00:00
|
|
|
lastUpdate: c.clock.Now(),
|
2015-01-09 22:06:30 +00:00
|
|
|
}
|
2015-01-09 23:33:58 +00:00
|
|
|
result <- data.item
|
|
|
|
|
|
|
|
// Store in cache
|
|
|
|
c.lock.Lock()
|
2015-01-14 00:53:50 +00:00
|
|
|
defer c.lock.Unlock()
|
2015-01-09 22:06:30 +00:00
|
|
|
c.cache[key] = data
|
2015-01-09 23:33:58 +00:00
|
|
|
|
|
|
|
// Remove in flight entry
|
|
|
|
c.inFlightLock.Lock()
|
2015-01-14 00:53:50 +00:00
|
|
|
defer c.inFlightLock.Unlock()
|
2015-01-09 23:33:58 +00:00
|
|
|
delete(c.inFlight, key)
|
|
|
|
}()
|
|
|
|
return result
|
2015-01-09 22:06:30 +00:00
|
|
|
}
|