Skip to content

Commit

Permalink
fix(all): Rename SetWithExpiry to SetWithTTL
Browse files Browse the repository at this point in the history
  • Loading branch information
bartventer committed Jun 19, 2024
1 parent 9b4d1cd commit 93b7fbd
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 29 deletions.
5 changes: 2 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ to determine the cache implementation to use. The URL format is:
The scheme is used to determine the cache implementation. The host and port are used
to connect to the cache service. The optional query parameters can be used to configure
the cache implementation. Each cache implementation supports different query parameters.
Refer to the documentation of each cache implementation for more information.
# Usage
Expand Down Expand Up @@ -80,8 +79,8 @@ type Cache interface {
// Set sets a key to a value in the cache.
Set(ctx context.Context, key string, value interface{}, modifiers ...keymod.Mod) error

// SetWithExpiry sets a key to a value in the cache with an expiry time.
SetWithExpiry(ctx context.Context, key string, value interface{}, expiry time.Duration, modifiers ...keymod.Mod) error
// SetWithTTL sets a key to a value in the cache with a time-to-live (TTL).
SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, modifiers ...keymod.Mod) error

// Exists checks if a key exists in the cache.
Exists(ctx context.Context, key string, modifiers ...keymod.Mod) (bool, error)
Expand Down
12 changes: 6 additions & 6 deletions drivertest/drivertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func RunConformanceTests(t *testing.T, newHarness HarnessMaker) {
t.Helper()

t.Run("Set", func(t *testing.T) { withCache(t, newHarness, testSet) })
t.Run("SetWithExpiry", func(t *testing.T) { withCache(t, newHarness, testSetWithExpiry) })
t.Run("SetWithTTL", func(t *testing.T) { withCache(t, newHarness, testSetWithTTL) })
t.Run("Exists", func(t *testing.T) { withCache(t, newHarness, testExists) })
t.Run("Count", func(t *testing.T) { withCache(t, newHarness, testCount) })
t.Run("Get", func(t *testing.T) { withCache(t, newHarness, testGet) })
Expand Down Expand Up @@ -101,14 +101,14 @@ func testSet(t *testing.T, c cache.Cache, opts Options) {
assert.Equal(t, value, string(got))
}

// testSetWithExpiry tests the SetWithExpiry method of the cache.
func testSetWithExpiry(t *testing.T, c cache.Cache, opts Options) {
// testSetWithTTL tests the SetWithTTL method of the cache.
func testSetWithTTL(t *testing.T, c cache.Cache, opts Options) {
t.Parallel()
key := uniqueKey(t)
value := "testValue"
expiry := 1 * time.Second
ttl := 1 * time.Second

err := c.SetWithExpiry(context.Background(), key, value, expiry)
err := c.SetWithTTL(context.Background(), key, value, ttl)
require.NoError(t, err)
t.Cleanup(func() {
c.Del(context.Background(), key)
Expand All @@ -119,7 +119,7 @@ func testSetWithExpiry(t *testing.T, c cache.Cache, opts Options) {
assert.Equal(t, value, string(got))

// Wait for the key to expire
time.Sleep(expiry)
time.Sleep(ttl)

_, err = c.Get(context.Background(), key)
require.Error(t, err)
Expand Down
8 changes: 4 additions & 4 deletions drivertest/drivertest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ func (r *MockCache) Set(ctx context.Context, key string, value interface{}, modi
return nil
}

// SetWithExpiry implements cache.Cache.
func (r *MockCache) SetWithExpiry(ctx context.Context, key string, value interface{}, expiry time.Duration, modifiers ...keymod.Mod) error {
// SetWithTTL implements cache.Cache.
func (r *MockCache) SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, modifiers ...keymod.Mod) error {
key = keymod.Modify(key, modifiers...)
r.mu.Lock()
defer r.mu.Unlock()
switch v := value.(type) {
case string:
r.store[key] = Item{Value: []byte(v), Expiry: time.Now().Add(expiry)}
r.store[key] = Item{Value: []byte(v), Expiry: time.Now().Add(ttl)}
case []byte:
r.store[key] = Item{Value: v, Expiry: time.Now().Add(expiry)}
r.store[key] = Item{Value: v, Expiry: time.Now().Add(ttl)}
default:
return fmt.Errorf("unsupported value type: %T", v)
}
Expand Down
6 changes: 3 additions & 3 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ func (m *memcacheCache) Set(_ context.Context, key string, value interface{}, mo
return nil
}

// SetWithExpiry implements cache.Cache.
func (m *memcacheCache) SetWithExpiry(_ context.Context, key string, value interface{}, expiry time.Duration, modifiers ...keymod.Mod) error {
// SetWithTTL implements cache.Cache.
func (m *memcacheCache) SetWithTTL(_ context.Context, key string, value interface{}, ttl time.Duration, modifiers ...keymod.Mod) error {
key = keymod.Modify(key, modifiers...)
item := &memcache.Item{
Key: key,
Value: []byte(value.(string)),
Expiration: int32(expiry.Seconds()),
Expiration: int32(ttl.Seconds()),
}
err := m.client.Set(item)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions ramcache/ramcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,13 @@ func (r *ramcache) Set(ctx context.Context, key string, value interface{}, modif
return r.set(key, value, 0)
}

// SetWithExpiry implements cache.Cache.
func (r *ramcache) SetWithExpiry(ctx context.Context, key string, value interface{}, expiry time.Duration, modifiers ...keymod.Mod) error {
if err := cache.ValidateTTL(expiry); err != nil {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("invalid expiry duration %q: %w", expiry, err))
// SetWithTTL implements cache.Cache.
func (r *ramcache) SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, modifiers ...keymod.Mod) error {
if err := cache.ValidateTTL(ttl); err != nil {
return gcerrors.NewWithScheme(Scheme, fmt.Errorf("invalid expiry duration %q: %w", ttl, err))
}
key = keymod.Modify(key, modifiers...)
return r.set(key, value, expiry)
return r.set(key, value, ttl)
}

func (r *ramcache) set(key string, value interface{}, expiry time.Duration) error {
Expand Down
4 changes: 2 additions & 2 deletions ramcache/ramcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ func Test_ramcache_removeExpiredItems(t *testing.T) {
}
}

func TestSetWithExpiry_InvalidExpiry(t *testing.T) {
func TestSetWithTTL_InvalidExpiry(t *testing.T) {
ctx := context.Background()
r := New(ctx, &Options{})

err := r.SetWithExpiry(ctx, "key", "value", -1*time.Second)
err := r.SetWithTTL(ctx, "key", "value", -1*time.Second)
if !errors.Is(err, cache.ErrInvalidTTL) {
t.Errorf("Expected error to be cache.ErrInvalidTTL, got %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ func (r *redisCache) Set(ctx context.Context, key string, value interface{}, mod
return r.client.Set(ctx, key, value, 0).Err()
}

// SetWithExpiry implements cache.Cache.
func (r *redisCache) SetWithExpiry(ctx context.Context, key string, value interface{}, expiry time.Duration, modifiers ...keymod.Mod) error {
// SetWithTTL implements cache.Cache.
func (r *redisCache) SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, modifiers ...keymod.Mod) error {
key = keymod.Modify(key, modifiers...)
return r.client.Set(ctx, key, value, expiry).Err()
return r.client.Set(ctx, key, value, ttl).Err()
}

// Close implements cache.Cache.
Expand Down
6 changes: 3 additions & 3 deletions rediscluster/rediscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ func (r *redisClusterCache) Set(ctx context.Context, key string, value interface
return r.client.Set(ctx, key, value, 0).Err()
}

// SetWithExpiry implements cache.Cache.
func (r *redisClusterCache) SetWithExpiry(ctx context.Context, key string, value interface{}, expiry time.Duration, modifiers ...keymod.Mod) error {
// SetWithTTL implements cache.Cache.
func (r *redisClusterCache) SetWithTTL(ctx context.Context, key string, value interface{}, ttl time.Duration, modifiers ...keymod.Mod) error {
key = keymod.Modify(key, modifiers...)
return r.client.Set(ctx, key, value, expiry).Err()
return r.client.Set(ctx, key, value, ttl).Err()
}

// Ping implements cache.Cache.
Expand Down

0 comments on commit 93b7fbd

Please sign in to comment.