Skip to content

Commit

Permalink
+
Browse files Browse the repository at this point in the history
  • Loading branch information
latitov committed Oct 28, 2022
1 parent e3f738b commit 626e550
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ func BenchmarkCacheSetWithoutTTL(b *testing.B) {
cache := ttlcache.New[string, string]()

for n := 0; n < b.N; n++ {
cache.Set(fmt.Sprint(n%1000000), "value", ttlcache.NoTTL)
cache.SetWithTTL(fmt.Sprint(n%1000000), "value", ttlcache.NoTTL)
}
}

func BenchmarkCacheSetWithGlobalTTL(b *testing.B) {
cache := ttlcache.New[string, string](
cache := ttlcache.New(
ttlcache.WithTTL[string, string](50 * time.Millisecond),
)

for n := 0; n < b.N; n++ {
cache.Set(fmt.Sprint(n%1000000), "value", ttlcache.DefaultTTL)
cache.Set(fmt.Sprint(n%1000000), "value")
}
}
4 changes: 0 additions & 4 deletions cache-private.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ func (c *Cache[K, V]) updateExpirations(fresh bool, elem *list.Element) {
// set creates a new item, adds it to the cache and then returns it.
// Not concurrently safe.
func (c *Cache[K, V]) set(key K, value V, ttl time.Duration, touch bool) *Item[K, V] {
if ttl == DefaultTTL {
ttl = c.options.ttl
}

elem := c.get(key, false)
if elem != nil {
// update/overwrite an existing item
Expand Down
14 changes: 13 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V] {
// Set creates a new item from the provided key and value, adds
// it to the cache and then returns it. If an item associated with the
// provided key already exists, the new item overwrites the existing one.
func (c *Cache[K, V]) Set(key K, value V, ttl time.Duration) *Item[K, V] {
func (c *Cache[K, V]) Set(key K, value V) *Item[K, V] {
if !c.options.lockingFromOutside {
c.CacheItems.Mu.Lock()
defer c.CacheItems.Mu.Unlock()
}

return c.set(key, value, c.options.ttl, true)
}

// Set creates a new item from the provided key and value, adds
// it to the cache and then returns it. If an item associated with the
// provided key already exists, the new item overwrites the existing one.
func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) *Item[K, V] {
if !c.options.lockingFromOutside {
c.CacheItems.Mu.Lock()
defer c.CacheItems.Mu.Unlock()
Expand Down
18 changes: 7 additions & 11 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,10 @@ func Test_Cache_set(t *testing.T) {
Key: existingKey,
TTL: NoTTL,
},
"Set with existing key and DefaultTTL": {
Key: existingKey,
TTL: DefaultTTL,
},
"Set with new key and eviction caused by small capacity": {
Capacity: 3,
Key: newKey,
TTL: DefaultTTL,
TTL: 0,
Metrics: Metrics{
Insertions: 1,
Evictions: 1,
Expand All @@ -202,7 +198,7 @@ func Test_Cache_set(t *testing.T) {
"Set with new key and no eviction caused by large capacity": {
Capacity: 10,
Key: newKey,
TTL: DefaultTTL,
TTL: 0,
Metrics: Metrics{
Insertions: 1,
},
Expand All @@ -226,7 +222,7 @@ func Test_Cache_set(t *testing.T) {
},
"Set with new key and DefaultTTL": {
Key: newKey,
TTL: DefaultTTL,
TTL: 0,
Metrics: Metrics{
Insertions: 1,
},
Expand Down Expand Up @@ -287,11 +283,11 @@ func Test_Cache_set(t *testing.T) {
}

switch {
case c.TTL == DefaultTTL:
case c.TTL == 0:
assert.Equal(t, cache.options.ttl, item.ttl)
assert.WithinDuration(t, time.Now(), item.expiresAt, cache.options.ttl)
assert.Equal(t, c.Key, cache.CacheItems.expQueue[0].Value.(*Item[string, string]).key)
case c.TTL > DefaultTTL:
case c.TTL > 0:
assert.Equal(t, c.TTL, item.ttl)
assert.WithinDuration(t, time.Now(), item.expiresAt, c.TTL)
assert.Equal(t, c.Key, cache.CacheItems.expQueue[0].Value.(*Item[string, string]).key)
Expand Down Expand Up @@ -435,11 +431,11 @@ func Test_Cache_evict(t *testing.T) {

func Test_Cache_Set(t *testing.T) {
cache := prepCache(time.Hour, "test1", "test2", "test3")
item := cache.Set("hello", "value123", time.Minute)
item := cache.SetWithTTL("hello", "value123", time.Minute)
require.NotNil(t, item)
assert.Same(t, item, cache.CacheItems.values["hello"].Value)

item = cache.Set("test1", "value123", time.Minute)
item = cache.SetWithTTL("test1", "value123", time.Minute)
require.NotNil(t, item)
assert.Same(t, item, cache.CacheItems.values["test1"].Value)
}
Expand Down
4 changes: 0 additions & 4 deletions item.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
const (
// NoTTL indicates that an item should never expire.
NoTTL time.Duration = -1

// DefaultTTL indicates that the default TTL
// value should be used.
DefaultTTL time.Duration = 0
)

// Item holds all the information that is associated with a single
Expand Down

0 comments on commit 626e550

Please sign in to comment.