1
0

Pass battery of linting/vetting

including:

- gofmt -s to simplify some code
- Properly capture range variable (go vet)
- Update doc comments (fix golint errors)
This commit is contained in:
Stephen McQuay 2017-04-30 19:45:06 -07:00
parent f73e2280ec
commit 6b566c2c3a
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
3 changed files with 50 additions and 44 deletions

View File

@ -1,3 +1,4 @@
// Package cache implements an in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
package cache package cache
import ( import (
@ -10,13 +11,16 @@ import (
"time" "time"
) )
// Item represents an item stored in the cache.
//
// Item stores information relevant to expiring it from the cache.
type Item struct { type Item struct {
Object interface{} Object interface{}
Expiration int64 Expiration int64
Accessed int64 Accessed int64
} }
// Returns true if the item has expired. // Expired returns true if the item has expired.
func (item Item) Expired() bool { func (item Item) Expired() bool {
if item.Expiration == 0 { if item.Expiration == 0 {
return false return false
@ -24,20 +28,21 @@ func (item Item) Expired() bool {
return time.Now().UnixNano() > item.Expiration return time.Now().UnixNano() > item.Expiration
} }
// Return the time at which this item was last accessed. // LastAccessed returns the time at which this item was last accessed.
func (item Item) LastAccessed() time.Time { func (item Item) LastAccessed() time.Time {
return time.Unix(0, item.Accessed) return time.Unix(0, item.Accessed)
} }
const ( const (
// For use with functions that take an expiration time. // NoExpiration is for use with functions that take an expiration time.
NoExpiration time.Duration = -1 NoExpiration time.Duration = -1
// For use with functions that take an expiration time. Equivalent to // DefaultExpiration is for use with functions that take an expiration
// passing in the same expiration duration as was given to New() or // time. Equivalent to passing in the same expiration duration as was given
// NewFrom() when the cache was created (e.g. 5 minutes.) // to New() or NewFrom() when the cache was created (e.g. 5 minutes.)
DefaultExpiration time.Duration = 0 DefaultExpiration time.Duration = 0
) )
// Cache implements the in-memory key:value cache.
type Cache struct { type Cache struct {
*cache *cache
// If this is confusing, see the comment at the bottom of New() // If this is confusing, see the comment at the bottom of New()
@ -1186,7 +1191,7 @@ func (c *cache) deleteLRUAmount(numItems int) []keyAndValue {
return nil return nil
} }
var ( var (
lastTime int64 = 0 lastTime int64
lastItems = make([]string, numItems) // Ring buffer lastItems = make([]string, numItems) // Ring buffer
liCount = 0 liCount = 0
full = false full = false
@ -1410,24 +1415,24 @@ func newCacheWithJanitor(de time.Duration, ci time.Duration, m map[string]Item,
return C return C
} }
// Return a new cache with a given default expiration duration and cleanup // New returns a new cache with a given default expiration duration and cleanup
// interval. If the expiration duration is less than one (or NoExpiration), // interval. If the expiration duration is less than one (or NoExpiration), the
// the items in the cache never expire (by default), and must be deleted // items in the cache never expire (by default), and must be deleted manually.
// manually. If the cleanup interval is less than one, expired items are not // If the cleanup interval is less than one, expired items are not deleted from
// deleted from the cache before calling c.DeleteExpired(). // the cache before calling c.DeleteExpired().
func New(defaultExpiration, cleanupInterval time.Duration) *Cache { func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
items := make(map[string]Item) items := make(map[string]Item)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0)
} }
// Return a new cache with a given default expiration duration, cleanup // NewWithLRU returns a new cache with a given default expiration duration,
// interval, and maximum-ish number of items. If the expiration duration // cleanup interval, and maximum-ish number of items. If the expiration
// is less than one (or NoExpiration), the items in the cache never expire // duration is less than one (or NoExpiration), the items in the cache never
// (by default), and must be deleted manually. If the cleanup interval is // expire (by default), and must be deleted manually. If the cleanup interval
// less than one, expired items are not deleted from the cache before // is less than one, expired items are not deleted from the cache before
// calling c.DeleteExpired(), c.DeleteLRU(), or c.DeleteLRUAmount(). If maxItems // calling c.DeleteExpired(), c.DeleteLRU(), or c.DeleteLRUAmount(). If
// is not greater than zero, then there will be no soft cap on the number of // maxItems is not greater than zero, then there will be no soft cap on the
// items in the cache. // number of items in the cache.
// //
// Using the LRU functionality makes Get() a slower, write-locked operation. // Using the LRU functionality makes Get() a slower, write-locked operation.
func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache { func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int) *Cache {
@ -1435,11 +1440,11 @@ func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int)
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems)
} }
// Return a new cache with a given default expiration duration and cleanup // NewFrom returns a new cache with a given default expiration duration and
// interval. If the expiration duration is less than one (or NoExpiration), // cleanup interval. If the expiration duration is less than one (or
// the items in the cache never expire (by default), and must be deleted // NoExpiration), the items in the cache never expire (by default), and must be
// manually. If the cleanup interval is less than one, expired items are not // deleted manually. If the cleanup interval is less than one, expired items
// deleted from the cache before calling c.DeleteExpired(). // are not deleted from the cache before calling c.DeleteExpired().
// //
// NewFrom() also accepts an items map which will serve as the underlying map // NewFrom() also accepts an items map which will serve as the underlying map
// for the cache. This is useful for starting from a deserialized cache // for the cache. This is useful for starting from a deserialized cache
@ -1460,7 +1465,8 @@ func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0)
} }
// Similar to NewFrom, but creates a cache with LRU functionality enabled. // NewFromWithLRU is similar to NewFrom, but creates a cache with LRU
// functionality enabled.
func NewFromWithLRU(defaultExpiration, cleanupInterval time.Duration, items map[string]Item, maxItems int) *Cache { func NewFromWithLRU(defaultExpiration, cleanupInterval time.Duration, items map[string]Item, maxItems int) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems) return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems)
} }

View File

@ -108,11 +108,11 @@ func TestCacheTimes(t *testing.T) {
func TestNewFrom(t *testing.T) { func TestNewFrom(t *testing.T) {
m := map[string]Item{ m := map[string]Item{
"a": Item{ "a": {
Object: 1, Object: 1,
Expiration: 0, Expiration: 0,
}, },
"b": Item{ "b": {
Object: 2, Object: 2,
Expiration: 0, Expiration: 0,
}, },
@ -1302,14 +1302,14 @@ func testFillAndSerialize(t *testing.T, tc *Cache) {
{Num: 3}, {Num: 3},
}, DefaultExpiration) }, DefaultExpiration)
tc.Set("[]*struct", []*TestStruct{ tc.Set("[]*struct", []*TestStruct{
&TestStruct{Num: 4}, {Num: 4},
&TestStruct{Num: 5}, {Num: 5},
}, DefaultExpiration) }, DefaultExpiration)
tc.Set("structception", &TestStruct{ tc.Set("structception", &TestStruct{
Num: 42, Num: 42,
Children: []*TestStruct{ Children: []*TestStruct{
&TestStruct{Num: 6174}, {Num: 6174},
&TestStruct{Num: 4716}, {Num: 4716},
}, },
}, DefaultExpiration) }, DefaultExpiration)
@ -1644,12 +1644,12 @@ func benchmarkCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(n) wg.Add(n)
for _, v := range keys { for _, v := range keys {
go func() { go func(key string) {
for j := 0; j < each; j++ { for j := 0; j < each; j++ {
tc.Get(v) tc.Get(key)
} }
wg.Done() wg.Done()
}() }(v)
} }
b.StartTimer() b.StartTimer()
wg.Wait() wg.Wait()
@ -1680,12 +1680,12 @@ func benchmarkCacheWithLRUGetManyConcurrent(b *testing.B, exp time.Duration, max
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(n) wg.Add(n)
for _, v := range keys { for _, v := range keys {
go func() { go func(key string) {
for j := 0; j < each; j++ { for j := 0; j < each; j++ {
tc.Get(v) tc.Get(key)
} }
wg.Done() wg.Done()
}() }(v)
} }
b.StartTimer() b.StartTimer()
wg.Wait() wg.Wait()

View File

@ -73,12 +73,12 @@ func benchmarkShardedCacheGetManyConcurrent(b *testing.B, exp time.Duration) {
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)
wg.Add(n) wg.Add(n)
for _, v := range keys { for _, v := range keys {
go func() { go func(key string) {
for j := 0; j < each; j++ { for j := 0; j < each; j++ {
tsc.Get(v) tsc.Get(key)
} }
wg.Done() wg.Done()
}() }(v)
} }
b.StartTimer() b.StartTimer()
wg.Wait() wg.Wait()