Skip to content

Commit

Permalink
Add Del method
Browse files Browse the repository at this point in the history
  • Loading branch information
feymanlee committed May 9, 2024
1 parent 9141bce commit 2c055bb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cache.go
Expand Up @@ -57,6 +57,8 @@ type Driver[V any] interface {
Forever(key string, value V) error
// Forget Remove an item from the cache.
Forget(key string) error
// Del Remove an item from the cache alia Forget.
Del(key string) error
// Flush Remove all items from the cache.
Flush() error
// Get Retrieve an item from the cache by key.
Expand Down
4 changes: 4 additions & 0 deletions go_cache.go
Expand Up @@ -59,6 +59,10 @@ func (d *GoCacheDriver[V]) Forget(key string) error {
return nil
}

func (d *GoCacheDriver[V]) Del(key string) error {
return d.Forget(key)
}

func (d *GoCacheDriver[V]) Flush() error {
d.memCache.Flush()
return nil
Expand Down
7 changes: 6 additions & 1 deletion go_redis.go
Expand Up @@ -2,6 +2,7 @@ package cacheit

import (
"context"
"errors"
"fmt"
"reflect"
"time"
Expand Down Expand Up @@ -91,14 +92,18 @@ func (d *RedisDriver[V]) Forget(key string) error {
return d.redisClient.Del(d.ctx, d.getCacheKey(key)).Err()
}

func (d *RedisDriver[V]) Del(key string) error {
return d.Forget(key)
}

func (d *RedisDriver[V]) Flush() error {
return d.redisClient.FlushDB(d.ctx).Err()
}

func (d *RedisDriver[V]) Get(key string) (V, error) {
var result V
if value, err := d.redisClient.Get(d.ctx, d.getCacheKey(key)).Result(); err != nil {
if err == redis.Nil {
if errors.Is(err, redis.Nil) {
return result, ErrCacheMiss
}
return result, err
Expand Down

0 comments on commit 2c055bb

Please sign in to comment.