Skip to content

Commit

Permalink
Formatting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 25, 2021
1 parent b3f9be0 commit f173a81
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions background.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func (km *keyMutex) Lock(key string) bool {
}

func (m CacheMap) BackgroundUpdate(key string, updater func() (interface{}, error)) {
//Lock the key from writes
// Lock the key from writes
locked := backgroundMutex.Lock(key)
if locked {
//Defer release write lock
// Defer release write lock
defer backgroundMutex.Unlock(key)
value, err := updater()
if err == nil {
Expand Down
10 changes: 5 additions & 5 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ func TestGet(t *testing.T) {
t.Errorf("Expected cache to return `world` for `hello`")
}

//Check to see if cleanup is clearing unexpired items
// Check to see if cleanup is clearing unexpired items
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if !exists || data == nil {
t.Errorf("Expected cache to return data")
}

//Check Cache is re-touching after a get
// Check Cache is re-touching after a get
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if !exists || data == nil {
t.Errorf("Expected cache to return data")
}

//Check Cache is optionally re-touching after a get
// Check Cache is optionally re-touching after a get
time.Sleep(time.Millisecond * 200)
data, exists = cache.TouchGet("hello", false)
if !exists || data == nil {
t.Errorf("Expected cache to return data")
}

//Make sure cache clears after expiry
// Make sure cache clears after expiry
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if exists || data != nil {
Expand All @@ -69,7 +69,7 @@ func TestMaxLifetime(t *testing.T) {
t.Errorf("Expected cache to return `world` for `hello`")
}

//Check to see if max lifetime has killed the item
// Check to see if max lifetime has killed the item
time.Sleep(time.Millisecond * 200)
data, exists = cache.Get("hello")
if exists || data != nil {
Expand Down
2 changes: 1 addition & 1 deletion cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (ms *CacheMapShared) Flush() {
ms.Unlock()
}

//Cleanup removes any expired items from the cache map
// Cleanup removes any expired items from the cache map
func (ms *CacheMapShared) Cleanup() {
ms.Lock()
for key, item := range ms.items {
Expand Down
6 changes: 3 additions & 3 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func newItem(value interface{}, duration time.Duration, deadline time.Time) *Ite
return i
}

//Touch increases the expiry time on the item by the TTL
// Touch increases the expiry time on the item by the TTL
func (i *Item) Touch() {
i.Lock()
expiration := time.Now().Add(i.ttl)
i.expires = &expiration
i.Unlock()
}

//Expired returns if the item has passed its expiry time
// Expired returns if the item has passed its expiry time
func (i *Item) Expired() bool {
var value bool
i.RLock()
Expand All @@ -46,7 +46,7 @@ func (i *Item) Expired() bool {
return value
}

//GetValue represents the value of the item in the map
// GetValue represents the value of the item in the map
func (i *Item) GetValue() interface{} {
return i.data
}
Expand Down
8 changes: 4 additions & 4 deletions opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ func defaultCacheOptions() cacheOptions {
// CacheOption configures how we set up the cache map
type CacheOption func(options *cacheOptions)

//WithShardSize With a custom sub map shard size
// WithShardSize With a custom sub map shard size
func WithShardSize(shardSize int) CacheOption {
return func(o *cacheOptions) {
o.shardCount = shardSize
}
}

//WithDefaultTTL Sets the default duration for items stored
// WithDefaultTTL Sets the default duration for items stored
func WithDefaultTTL(ttl time.Duration) CacheOption {
return func(o *cacheOptions) {
o.defaultCacheDuration = ttl
}
}

//WithCleanupDuration Sets how frequently to cleanup expired items
// WithCleanupDuration Sets how frequently to cleanup expired items
func WithCleanupDuration(ttl time.Duration) CacheOption {
return func(o *cacheOptions) {
o.cleanupDuration = ttl
}
}

//WithMaxLifetime Sets the maximum amount of time an item can exist within the cache
// WithMaxLifetime Sets the maximum amount of time an item can exist within the cache
func WithMaxLifetime(ttl time.Duration) CacheOption {
return func(o *cacheOptions) {
o.maxLifetime = ttl
Expand Down

0 comments on commit f173a81

Please sign in to comment.