diff --git a/bench/bench_test.go b/bench/bench_test.go index 4ff2879..9b60d68 100644 --- a/bench/bench_test.go +++ b/bench/bench_test.go @@ -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") } } diff --git a/cache-private.go b/cache-private.go index 1bed79e..098ec3d 100644 --- a/cache-private.go +++ b/cache-private.go @@ -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 diff --git a/cache.go b/cache.go index bb5c132..6a3a028 100644 --- a/cache.go +++ b/cache.go @@ -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() diff --git a/cache_test.go b/cache_test.go index c627114..366fb9f 100644 --- a/cache_test.go +++ b/cache_test.go @@ -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, @@ -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, }, @@ -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, }, @@ -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) @@ -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) } diff --git a/item.go b/item.go index 58c9c5c..5f4d2e8 100644 --- a/item.go +++ b/item.go @@ -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