Skip to content

Commit 3f3578d

Browse files
committed
Change the billing cache to in memory
1 parent 742edd7 commit 3f3578d

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

api/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ require (
3131
github.com/matcornic/hermes/v2 v2.1.0
3232
github.com/nyaruka/phonenumbers v1.3.0
3333
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177
34+
github.com/patrickmn/go-cache v2.1.0+incompatible
3435
github.com/pkg/errors v0.9.1
3536
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5
3637
github.com/redis/go-redis/v9 v9.3.1

api/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
275275
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
276276
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177 h1:nRlQD0u1871kaznCnn1EvYiMbum36v7hw1DLPEjds4o=
277277
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177/go.mod h1:ao5zGxj8Z4x60IOVYZUbDSmt3R8Ddo080vEgPosHpak=
278+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
279+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
278280
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
279281
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
280282
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

api/pkg/cache/memory_cache.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cache
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/NdoleStudio/httpsms/pkg/telemetry"
9+
"github.com/palantir/stacktrace"
10+
ttlCache "github.com/patrickmn/go-cache"
11+
)
12+
13+
// memoryCache is the Cache implementation in memory
14+
type memoryCache struct {
15+
tracer telemetry.Tracer
16+
store *ttlCache.Cache
17+
}
18+
19+
// NewMemoryCache creates a new instance of memoryCache
20+
func NewMemoryCache(tracer telemetry.Tracer, store *ttlCache.Cache) Cache {
21+
return &memoryCache{
22+
tracer: tracer,
23+
store: store,
24+
}
25+
}
26+
27+
// Get an item from the redis cache
28+
func (cache *memoryCache) Get(ctx context.Context, key string) (value string, err error) {
29+
ctx, span := cache.tracer.Start(ctx)
30+
defer span.End()
31+
32+
response, ok := cache.store.Get(key)
33+
if !ok {
34+
return "", stacktrace.Propagate(err, fmt.Sprintf("no item found in cache with key [%s]", key))
35+
}
36+
37+
return response.(string), nil
38+
}
39+
40+
// Set an item in the redis cache
41+
func (cache *memoryCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
42+
ctx, span := cache.tracer.Start(ctx)
43+
defer span.End()
44+
45+
cache.store.Set(key, value, ttl)
46+
return nil
47+
}

api/pkg/cache/redis_cache.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cache
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"time"
78

@@ -10,27 +11,27 @@ import (
1011
"github.com/redis/go-redis/v9"
1112
)
1213

13-
// RedisCache is the Cache implementation in redis
14-
type RedisCache struct {
14+
// redisCache is the Cache implementation in redis
15+
type redisCache struct {
1516
tracer telemetry.Tracer
1617
client *redis.Client
1718
}
1819

1920
// NewRedisCache creates a new instance of RedisCache
2021
func NewRedisCache(tracer telemetry.Tracer, client *redis.Client) Cache {
21-
return &RedisCache{
22+
return &redisCache{
2223
tracer: tracer,
2324
client: client,
2425
}
2526
}
2627

2728
// Get an item from the redis cache
28-
func (cache *RedisCache) Get(ctx context.Context, key string) (value string, err error) {
29+
func (cache *redisCache) Get(ctx context.Context, key string) (value string, err error) {
2930
ctx, span := cache.tracer.Start(ctx)
3031
defer span.End()
3132

3233
response, err := cache.client.Get(ctx, key).Result()
33-
if err == redis.Nil {
34+
if errors.Is(err, redis.Nil) {
3435
return "", stacktrace.Propagate(err, fmt.Sprintf("no item found in redis with key [%s]", key))
3536
}
3637
if err != nil {
@@ -40,7 +41,7 @@ func (cache *RedisCache) Get(ctx context.Context, key string) (value string, err
4041
}
4142

4243
// Set an item in the redis cache
43-
func (cache *RedisCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
44+
func (cache *redisCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
4445
ctx, span := cache.tracer.Start(ctx)
4546
defer span.End()
4647

api/pkg/di/container.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import (
6161
fiberLogger "github.com/gofiber/fiber/v2/middleware/logger"
6262
"github.com/gofiber/swagger"
6363
"github.com/palantir/stacktrace"
64+
ttlCache "github.com/patrickmn/go-cache"
6465
"gorm.io/gorm"
6566

6667
"github.com/NdoleStudio/httpsms/pkg/handlers"
@@ -318,6 +319,13 @@ func (container *Container) FirebaseApp() (app *firebase.App) {
318319
return app
319320
}
320321

322+
// InMemoryCache creates a new instance of the in memory cache.Cache
323+
func (container *Container) InMemoryCache() cache.Cache {
324+
container.logger.Debug("creating an in memory cache")
325+
c := ttlCache.New(time.Hour, time.Hour*2)
326+
return cache.NewMemoryCache(container.Tracer(), c)
327+
}
328+
321329
// Cache creates a new instance of cache.Cache
322330
func (container *Container) Cache() cache.Cache {
323331
container.logger.Debug("creating cache.Cache")
@@ -729,7 +737,7 @@ func (container *Container) BillingService() (service *services.BillingService)
729737
return services.NewBillingService(
730738
container.Logger(),
731739
container.Tracer(),
732-
container.Cache(),
740+
container.InMemoryCache(),
733741
container.Mailer(),
734742
container.UserEmailFactory(),
735743
container.BillingUsageRepository(),

0 commit comments

Comments
 (0)