Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Change the billing cache to in memory

Acho Arnold3f3578dbc9c7
diff --git a/api/go.mod b/api/go.mod
index 02fd9e29..36ab5ca6 100644
--- a/api/go.mod
+++ b/api/go.mod
@@ -31,6 +31,7 @@ require (
github.com/matcornic/hermes/v2 v2.1.0
github.com/nyaruka/phonenumbers v1.3.0
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177
+ github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/extra/redisotel/v9 v9.0.5
github.com/redis/go-redis/v9 v9.3.1
diff --git a/api/go.sum b/api/go.sum
index 4d2f8301..d6fafa3c 100644
--- a/api/go.sum
+++ b/api/go.sum
@@ -275,6 +275,8 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177 h1:nRlQD0u1871kaznCnn1EvYiMbum36v7hw1DLPEjds4o=
github.com/palantir/stacktrace v0.0.0-20161112013806-78658fd2d177/go.mod h1:ao5zGxj8Z4x60IOVYZUbDSmt3R8Ddo080vEgPosHpak=
+github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
+github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
diff --git a/api/pkg/cache/memory_cache.go b/api/pkg/cache/memory_cache.go
new file mode 100644
index 00000000..1cfc5b4b
--- /dev/null
+++ b/api/pkg/cache/memory_cache.go
@@ -0,0 +1,47 @@
+package cache
+
+import (
+ "context"
+ "fmt"
+ "time"
+
+ "github.com/NdoleStudio/httpsms/pkg/telemetry"
+ "github.com/palantir/stacktrace"
+ ttlCache "github.com/patrickmn/go-cache"
+)
+
+// memoryCache is the Cache implementation in memory
+type memoryCache struct {
+ tracer telemetry.Tracer
+ store *ttlCache.Cache
+}
+
+// NewMemoryCache creates a new instance of memoryCache
+func NewMemoryCache(tracer telemetry.Tracer, store *ttlCache.Cache) Cache {
+ return &memoryCache{
+ tracer: tracer,
+ store: store,
+ }
+}
+
+// Get an item from the redis cache
+func (cache *memoryCache) Get(ctx context.Context, key string) (value string, err error) {
+ ctx, span := cache.tracer.Start(ctx)
+ defer span.End()
+
+ response, ok := cache.store.Get(key)
+ if !ok {
+ return "", stacktrace.Propagate(err, fmt.Sprintf("no item found in cache with key [%s]", key))
+ }
+
+ return response.(string), nil
+}
+
+// Set an item in the redis cache
+func (cache *memoryCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
+ ctx, span := cache.tracer.Start(ctx)
+ defer span.End()
+
+ cache.store.Set(key, value, ttl)
+ return nil
+}
diff --git a/api/pkg/cache/redis_cache.go b/api/pkg/cache/redis_cache.go
index ad2398fa..18a6a887 100644
--- a/api/pkg/cache/redis_cache.go
+++ b/api/pkg/cache/redis_cache.go
@@ -2,6 +2,7 @@ package cache
import (
"context"
+ "errors"
"fmt"
"time"
@@ -10,27 +11,27 @@ import (
"github.com/redis/go-redis/v9"
)
-// RedisCache is the Cache implementation in redis
-type RedisCache struct {
+// redisCache is the Cache implementation in redis
+type redisCache struct {
tracer telemetry.Tracer
client *redis.Client
}
// NewRedisCache creates a new instance of RedisCache
func NewRedisCache(tracer telemetry.Tracer, client *redis.Client) Cache {
- return &RedisCache{
+ return &redisCache{
tracer: tracer,
client: client,
}
}
// Get an item from the redis cache
-func (cache *RedisCache) Get(ctx context.Context, key string) (value string, err error) {
+func (cache *redisCache) Get(ctx context.Context, key string) (value string, err error) {
ctx, span := cache.tracer.Start(ctx)
defer span.End()
response, err := cache.client.Get(ctx, key).Result()
- if err == redis.Nil {
+ if errors.Is(err, redis.Nil) {
return "", stacktrace.Propagate(err, fmt.Sprintf("no item found in redis with key [%s]", key))
}
if err != nil {
@@ -40,7 +41,7 @@ func (cache *RedisCache) Get(ctx context.Context, key string) (value string, err
}
// Set an item in the redis cache
-func (cache *RedisCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
+func (cache *redisCache) Set(ctx context.Context, key string, value string, ttl time.Duration) error {
ctx, span := cache.tracer.Start(ctx)
defer span.End()
diff --git a/api/pkg/di/container.go b/api/pkg/di/container.go
index 9e0eb785..b68461af 100644
--- a/api/pkg/di/container.go
+++ b/api/pkg/di/container.go
@@ -61,6 +61,7 @@ import (
fiberLogger "github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/swagger"
"github.com/palantir/stacktrace"
+ ttlCache "github.com/patrickmn/go-cache"
"gorm.io/gorm"
"github.com/NdoleStudio/httpsms/pkg/handlers"
@@ -318,6 +319,13 @@ func (container *Container) FirebaseApp() (app *firebase.App) {
return app
}
+// InMemoryCache creates a new instance of the in memory cache.Cache
+func (container *Container) InMemoryCache() cache.Cache {
+ container.logger.Debug("creating an in memory cache")
+ c := ttlCache.New(time.Hour, time.Hour*2)
+ return cache.NewMemoryCache(container.Tracer(), c)
+}
+
// Cache creates a new instance of cache.Cache
func (container *Container) Cache() cache.Cache {
container.logger.Debug("creating cache.Cache")
@@ -729,7 +737,7 @@ func (container *Container) BillingService() (service *services.BillingService)
return services.NewBillingService(
container.Logger(),
container.Tracer(),
- container.Cache(),
+ container.InMemoryCache(),
container.Mailer(),
container.UserEmailFactory(),
container.BillingUsageRepository(),