-
Notifications
You must be signed in to change notification settings - Fork 0
feat(cache): implement dynamic interface caching abstraction #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d56ae7
feat(cache): define interface for caching storage
Pyr33x f620c74
feat(cache): implement redis over caching interface
Pyr33x c5581cc
feat(proxy): merge redis cache abstraction with caching layer
Pyr33x ba4e363
feat(config): use configurable ttl using env
Pyr33x 0d62ff0
feat(cache): implement memory abstraction for caching
Pyr33x e5810dc
fix(cache): check if raw is nil due to memory nil return when no key …
Pyr33x 0894285
feat(config): use dynamic env to choose caching options
Pyr33x cb84160
chore(proxio): update readme to notice new cache abstraction
Pyr33x 4336d80
fix(proxio): triggered by coderabbit
Pyr33x f9baf65
fix(proxy): triggered by coderabbit
Pyr33x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,58 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type memoryEntry struct { | ||
| value []byte | ||
| expiration time.Time | ||
| } | ||
|
|
||
| type memoryStore struct { | ||
| data sync.Map | ||
| } | ||
|
|
||
| func NewMemoryStore() *memoryStore { | ||
| return &memoryStore{} | ||
| } | ||
|
|
||
| func (m *memoryStore) Get(ctx context.Context, key string) ([]byte, error) { | ||
| raw, ok := m.data.Load(key) | ||
| if !ok { | ||
| return nil, nil | ||
| } | ||
|
|
||
| entry := raw.(memoryEntry) | ||
|
|
||
| if !entry.expiration.IsZero() && time.Now().After(entry.expiration) { | ||
| m.data.Delete(key) | ||
| return nil, nil | ||
| } | ||
|
|
||
| return entry.value, nil | ||
| } | ||
|
|
||
| func (m *memoryStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error { | ||
| var exp time.Time | ||
| if ttl > 0 { | ||
| exp = time.Now().Add(ttl) | ||
| } | ||
|
|
||
| m.data.Store(key, memoryEntry{ | ||
| value: value, | ||
| expiration: exp, | ||
| }) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (m *memoryStore) Clear(ctx context.Context) error { | ||
| m.data.Range(func(key, _ any) bool { | ||
| m.data.Delete(key) | ||
| return true | ||
| }) | ||
| return nil | ||
| } | ||
This file contains hidden or 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,28 @@ | ||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/redis/go-redis/v9" | ||
| ) | ||
|
|
||
| type redisStore struct { | ||
| client *redis.Client | ||
| } | ||
|
|
||
| func NewRedisStore(client *redis.Client) *redisStore { | ||
| return &redisStore{client: client} | ||
| } | ||
|
|
||
| func (r *redisStore) Get(ctx context.Context, key string) ([]byte, error) { | ||
| return r.client.Get(ctx, key).Bytes() | ||
| } | ||
|
|
||
| func (r *redisStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error { | ||
| return r.client.Set(ctx, key, value, ttl).Err() | ||
| } | ||
|
|
||
| func (r *redisStore) Clear(ctx context.Context) error { | ||
| return r.client.FlushDB(ctx).Err() | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.