This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: o.musin <[email protected]>
- Loading branch information
Oleg
and
o.musin
authored
Jul 6, 2020
1 parent
bf43fca
commit e926f2b
Showing
6 changed files
with
234 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/bxcodec/httpcache/cache" | ||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
// CacheOptions for storing data for Redis connections | ||
type CacheOptions struct { | ||
Addr string | ||
Password string | ||
DB int // 0 for default DB | ||
} | ||
|
||
type redisCache struct { | ||
ctx context.Context | ||
cache *redis.Client | ||
expiryTime time.Duration | ||
} | ||
|
||
// NewCache will return the redis cache handler | ||
func NewCache(ctx context.Context, c *redis.Client, exptime time.Duration) cache.ICacheInteractor { | ||
return &redisCache{ | ||
ctx: ctx, | ||
cache: c, | ||
expiryTime: exptime, | ||
} | ||
} | ||
|
||
func (i *redisCache) Set(key string, value cache.CachedResponse) (err error) { | ||
valueJSON, _ := json.Marshal(value) | ||
set := i.cache.Set(i.ctx, key, string(valueJSON), i.expiryTime*time.Second) | ||
if err := set.Err(); err != nil { | ||
fmt.Println(err) | ||
return cache.ErrStorageInternal | ||
} | ||
return nil | ||
} | ||
|
||
func (i *redisCache) Get(key string) (res cache.CachedResponse, err error) { | ||
get := i.cache.Do(i.ctx, "get", key) | ||
if err = get.Err(); err != nil { | ||
if err == redis.Nil { | ||
return cache.CachedResponse{}, cache.ErrCacheMissed | ||
} | ||
return cache.CachedResponse{}, cache.ErrStorageInternal | ||
} | ||
val := get.Val().(string) | ||
err = json.Unmarshal([]byte(val), &res) | ||
if err != nil { | ||
return cache.CachedResponse{}, cache.ErrStorageInternal | ||
} | ||
return | ||
} | ||
|
||
func (i *redisCache) Delete(key string) (err error) { | ||
// deleting in redis equal to setting expiration time for key to 0 | ||
set := i.cache.Set(i.ctx, key, nil, 0) | ||
if err := set.Err(); err != nil { | ||
return cache.ErrStorageInternal | ||
} | ||
return nil | ||
} | ||
|
||
func (i *redisCache) Origin() string { | ||
return cache.CacheRedis | ||
} | ||
|
||
func (i *redisCache) Flush() error { | ||
flush := i.cache.FlushAll(i.ctx) | ||
if err := flush.Err(); err != nil { | ||
return cache.ErrStorageInternal | ||
} | ||
return nil | ||
} |
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,66 @@ | ||
package redis_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/alicebob/miniredis" | ||
"github.com/bxcodec/httpcache/cache" | ||
rediscache "github.com/bxcodec/httpcache/cache/redis" | ||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
func TestCacheRedis(t *testing.T) { | ||
s, err := miniredis.Run() | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer s.Close() | ||
c := redis.NewClient(&redis.Options{ | ||
Addr: s.Addr(), | ||
Password: "", // no password set | ||
DB: 0, // use default DB | ||
}) | ||
|
||
cacheObj := rediscache.NewCache(context.Background(), c, 15) | ||
testKey := "KEY" | ||
testVal := cache.CachedResponse{ | ||
DumpedResponse: nil, | ||
RequestURI: "http://bxcodec.io", | ||
RequestMethod: "GET", | ||
CachedTime: time.Now(), | ||
} | ||
|
||
// Try to SET item | ||
err = cacheObj.Set(testKey, testVal) | ||
if err != nil { | ||
t.Fatalf("expected %v, got %v", nil, err) | ||
} | ||
|
||
// try to GET item from cache | ||
res, err := cacheObj.Get(testKey) | ||
if err != nil { | ||
t.Fatalf("expected %v, got %v", nil, err) | ||
} | ||
// assert the content | ||
if res.RequestURI != testVal.RequestURI { | ||
t.Fatalf("expected %v, got %v", testVal.RequestURI, res.RequestURI) | ||
} | ||
// assert the content | ||
if res.RequestMethod != testVal.RequestMethod { | ||
t.Fatalf("expected %v, got %v", testVal.RequestMethod, res.RequestMethod) | ||
} | ||
|
||
// try to DELETE the item | ||
err = cacheObj.Delete(testKey) | ||
if err != nil { | ||
t.Fatalf("expected %v, got %v", nil, err) | ||
} | ||
|
||
// try to re-GET item from cache after deleted | ||
res, err = cacheObj.Get(testKey) | ||
if err == nil { | ||
t.Fatalf("expected %v, got %v", err, nil) | ||
} | ||
} |
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