-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): add redis cache to go server
- Loading branch information
Showing
9 changed files
with
122 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,4 +139,5 @@ npm-debug.log* | |
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
**/tmp/** | ||
**/llm_env/** |
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
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,73 @@ | ||
package cache | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"io" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v8" | ||
) | ||
|
||
type Cacher interface { | ||
Get(ctx context.Context, key string) ([]byte, error) | ||
Set(ctx context.Context, key string, value []byte, expiration time.Duration) error | ||
GetCompressed(ctx context.Context, key string) ([]byte, error) | ||
SetCompressed(ctx context.Context, key string, value []byte, expiration time.Duration) error | ||
HashKey(key string) string | ||
} | ||
|
||
type RedisCache struct { | ||
client *redis.Client | ||
} | ||
|
||
func NewRedisCache(addr string) *RedisCache { | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: addr, | ||
}) | ||
return &RedisCache{client: client} | ||
} | ||
|
||
func (rc *RedisCache) Get(ctx context.Context, key string) ([]byte, error) { | ||
return rc.client.Get(ctx, key).Bytes() | ||
} | ||
|
||
func (rc *RedisCache) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error { | ||
return rc.client.Set(ctx, key, value, expiration).Err() | ||
} | ||
|
||
func (rc *RedisCache) GetCompressed(ctx context.Context, key string) ([]byte, error) { | ||
compressed, err := rc.Get(ctx, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader, err := gzip.NewReader(bytes.NewReader(compressed)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer reader.Close() | ||
|
||
return io.ReadAll(reader) | ||
} | ||
|
||
func (rc *RedisCache) SetCompressed(ctx context.Context, key string, value []byte, expiration time.Duration) error { | ||
var compressedBuf bytes.Buffer | ||
gzipWriter := gzip.NewWriter(&compressedBuf) | ||
_, err := gzipWriter.Write(value) | ||
if err != nil { | ||
return err | ||
} | ||
gzipWriter.Close() | ||
|
||
return rc.Set(ctx, key, compressedBuf.Bytes(), expiration) | ||
} | ||
|
||
func (rc *RedisCache) HashKey(key string) string { | ||
hasher := sha256.New() | ||
hasher.Write([]byte(key)) | ||
return hex.EncodeToString(hasher.Sum(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
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