-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update table name and add Redis cache
- Loading branch information
1 parent
489c882
commit 70fc20a
Showing
4 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package store | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
type RedisCache struct { | ||
client *redis.Client | ||
} | ||
|
||
func NewRedisCache(address string, password string, db int) *RedisCache { | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: address, | ||
Password: password, | ||
DB: db, | ||
}) | ||
|
||
return &RedisCache{client: client} | ||
} | ||
|
||
func (r *RedisCache) Set(ctx context.Context, key string, value interface{}) error { | ||
return r.client.Set(ctx, key, value, time.Minute).Err() | ||
} | ||
|
||
func (r *RedisCache) Get(ctx context.Context, key string) (string, error) { | ||
val, err := r.client.Get(ctx, key).Result() | ||
if err == redis.Nil { | ||
return "", nil | ||
} | ||
return val, err | ||
} | ||
|
||
func (r *RedisCache) Delete(ctx context.Context, key string) error { | ||
return r.client.Del(ctx, key).Err() | ||
} | ||
|
||
func (r *RedisCache) Close() error { | ||
return r.client.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters