This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding stuff for deleting transactions. Reducing resource requests.
- Loading branch information
1 parent
e2f2f79
commit bd7096a
Showing
7 changed files
with
162 additions
and
39 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,59 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/getsentry/sentry-go" | ||
"github.com/monetrapp/rest-api/pkg/models" | ||
"github.com/sirupsen/logrus" | ||
"time" | ||
) | ||
|
||
type RemovedTransactionsCache interface { | ||
CacheDeletedTransaction(ctx context.Context, transaction models.Transaction) bool | ||
LookupDeletedTransaction(ctx context.Context, transactionId string) (*models.Transaction, bool) | ||
Close() error | ||
} | ||
|
||
var ( | ||
_ RemovedTransactionsCache = &noopRemovedTransactionsCache{} | ||
_ RemovedTransactionsCache = &redisRemovedTransactionsCache{} | ||
) | ||
|
||
type noopRemovedTransactionsCache struct{} | ||
|
||
func (n noopRemovedTransactionsCache) CacheDeletedTransaction(ctx context.Context, transaction models.Transaction) bool { | ||
return false | ||
} | ||
|
||
func (n noopRemovedTransactionsCache) LookupDeletedTransaction(ctx context.Context, transactionId string) (*models.Transaction, bool) { | ||
return nil, false | ||
} | ||
|
||
func (n noopRemovedTransactionsCache) Close() error { | ||
return nil | ||
} | ||
|
||
type redisRemovedTransactionsCache struct { | ||
log *logrus.Entry | ||
client Cache | ||
} | ||
|
||
func (r *redisRemovedTransactionsCache) CacheDeletedTransaction(ctx context.Context, transaction models.Transaction) bool { | ||
span := sentry.StartSpan(ctx, "CacheDeletedTransaction") | ||
defer span.Finish() | ||
|
||
return r.client.SetEzTTL(span.Context(), r.getKey(transaction), transaction, 30*time.Minute) == nil | ||
} | ||
|
||
func (r *redisRemovedTransactionsCache) LookupDeletedTransaction(ctx context.Context, transactionId string) (*models.Transaction, bool) { | ||
panic("implement me") | ||
} | ||
|
||
func (r *redisRemovedTransactionsCache) Close() error { | ||
panic("implement me") | ||
} | ||
|
||
func (r *redisRemovedTransactionsCache) getKey(transaction models.Transaction) string { | ||
return fmt.Sprintf("plaid:deleted_transactions:%d:%s", transaction.AccountId, transaction.PlaidTransactionId) | ||
} |
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 ( | ||
"context" | ||
"encoding/json" | ||
"github.com/getsentry/sentry-go" | ||
"github.com/gomodule/redigo/redis" | ||
"github.com/pkg/errors" | ||
"time" | ||
) | ||
|
||
type Cache interface { | ||
Set(ctx context.Context, key string, value []byte) error | ||
SetTTL(ctx context.Context, key string, value []byte, lifetime time.Duration) error | ||
SetEz(ctx context.Context, key string, object interface{}) error | ||
SetEzTTL(ctx context.Context, key string, object interface{}, lifetime time.Duration) error | ||
Get(ctx context.Context, key string) ([]byte, error) | ||
GetEz(ctx context.Context, key string, output interface{}) error | ||
Delete(ctx context.Context, key string) error | ||
Close() error | ||
} | ||
|
||
var ( | ||
_ Cache = &redisCache{} | ||
) | ||
|
||
type redisCache struct { | ||
client redis.Conn | ||
} | ||
|
||
func (r *redisCache) Set(ctx context.Context, key string, value []byte) error { | ||
span := sentry.StartSpan(ctx, "Redis - Set") | ||
defer span.Finish() | ||
return errors.Wrap(r.client.Send("SET", key, value), "failed to store item in cache") | ||
} | ||
|
||
func (r *redisCache) SetTTL(ctx context.Context, key string, value []byte, lifetime time.Duration) error { | ||
span := sentry.StartSpan(ctx, "Redis - SetTTL") | ||
defer span.Finish() | ||
return errors.Wrap(r.client.Send("SET", key, value, "EXAT", time.Now().Add(lifetime)), "failed to store item in cache") | ||
} | ||
|
||
func (r *redisCache) SetEz(ctx context.Context, key string, object interface{}) error { | ||
span := sentry.StartSpan(ctx, "Redis - SetEz") | ||
defer span.Finish() | ||
|
||
data, err := json.Marshal(object) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to marshal item to be cached") | ||
} | ||
|
||
return r.Set(span.Context(), key, data) | ||
} | ||
|
||
func (r *redisCache) SetEzTTL(ctx context.Context, key string, object interface{}, lifetime time.Duration) error { | ||
panic("implement me") | ||
} | ||
|
||
func (r *redisCache) Get(ctx context.Context, key string) ([]byte, error) { | ||
panic("implement me") | ||
} | ||
|
||
func (r *redisCache) GetEz(ctx context.Context, key string, output interface{}) error { | ||
panic("implement me") | ||
} | ||
|
||
func (r *redisCache) Delete(ctx context.Context, key string) error { | ||
panic("implement me") | ||
} | ||
|
||
func (r *redisCache) Close() error { | ||
panic("implement me") | ||
} |
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 |
---|---|---|
|
@@ -32,7 +32,7 @@ resources: | |
cpu: 100m | ||
memory: 128Mi | ||
requests: | ||
cpu: 100m | ||
cpu: 50m | ||
memory: 128Mi | ||
|
||
nodeSelector: | ||
|
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 |
---|---|---|
|
@@ -32,7 +32,7 @@ resources: | |
cpu: 100m | ||
memory: 128Mi | ||
requests: | ||
cpu: 100m | ||
cpu: 50m | ||
memory: 128Mi | ||
|
||
nodeSelector: | ||
|