-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Draft: refactor cache to use an interface (#1)
* refactor cache to use an interface * bump action versions Signed-off-by: circa10a <[email protected]> * fix api response types Signed-off-by: circa10a <[email protected]> --------- Signed-off-by: circa10a <[email protected]>
- Loading branch information
Showing
12 changed files
with
375 additions
and
197 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 |
---|---|---|
|
@@ -15,7 +15,7 @@ jobs: | |
with: | ||
fetch-depth: '0' | ||
- name: Install Go | ||
uses: actions/setup-go@v3 | ||
uses: actions/setup-go@v5 | ||
- name: Bump version and push tag | ||
uses: anothrNick/[email protected] | ||
id: tagging | ||
|
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,11 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// Cache is an interface for caching ip addresses | ||
type Cache interface { | ||
Get(context.Context, string) (bool, bool, error) | ||
Set(context.Context, string, bool) error | ||
} |
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,45 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
gocache "github.com/patrickmn/go-cache" | ||
) | ||
|
||
const ( | ||
deleteExpiredCacheItemsInternal = 10 * time.Minute | ||
) | ||
|
||
// MemoryCache is used to store/fetch ip proximity from an in-memory cache. | ||
type MemoryCache struct { | ||
memoryClient *gocache.Cache | ||
memoryOptions *MemoryOptions | ||
} | ||
|
||
// MemoryOptions holds in-memory cache configuration parameters. | ||
type MemoryOptions struct { | ||
TTL time.Duration | ||
} | ||
|
||
// NewRedisCache provides a new in-memory cache client. | ||
func NewMemoryCache(memoryOptions *MemoryOptions) *MemoryCache { | ||
return &MemoryCache{ | ||
memoryClient: gocache.New(memoryOptions.TTL, deleteExpiredCacheItemsInternal), | ||
memoryOptions: memoryOptions, | ||
} | ||
} | ||
|
||
// Get gets value from the in-memory cache. | ||
func (m *MemoryCache) Get(ctx context.Context, key string) (bool, bool, error) { | ||
if isIPAddressNear, found := m.memoryClient.Get(key); found { | ||
return isIPAddressNear.(bool), found, nil | ||
} | ||
return false, false, nil | ||
} | ||
|
||
// Set sets k/v in the in-memory cache. | ||
func (m *MemoryCache) Set(ctx context.Context, key string, value bool) error { | ||
m.memoryClient.Set(key, value, m.memoryOptions.TTL) | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
// RedisCache is used to store/fetch ip proximity from redis. | ||
type RedisCache struct { | ||
redisClient *redis.Client | ||
redisOptions *RedisOptions | ||
} | ||
|
||
// RedisOptions holds redis configuration parameters. | ||
type RedisOptions struct { | ||
Addr string | ||
Password string | ||
DB int | ||
TTL time.Duration | ||
} | ||
|
||
// NewRedisCache provides a new redis cache client. | ||
func NewRedisCache(redisOpts *RedisOptions) *RedisCache { | ||
return &RedisCache{ | ||
redisClient: redis.NewClient(&redis.Options{ | ||
Addr: redisOpts.Addr, | ||
Password: redisOpts.Password, | ||
DB: redisOpts.DB, | ||
}), | ||
redisOptions: redisOpts, | ||
} | ||
} | ||
|
||
// Get gets value from redis. | ||
func (r *RedisCache) Get(ctx context.Context, key string) (bool, bool, error) { | ||
val, err := r.redisClient.Get(ctx, key).Result() | ||
if err != nil { | ||
// If key is not in redis | ||
if err == redis.Nil { | ||
return false, false, nil | ||
} | ||
return false, false, err | ||
} | ||
isIPAddressNear, err := strconv.ParseBool(val) | ||
if err != nil { | ||
return false, false, err | ||
} | ||
|
||
return isIPAddressNear, true, nil | ||
} | ||
|
||
// Set sets k/v in redis. | ||
func (r *RedisCache) Set(ctx context.Context, key string, value bool) error { | ||
// Redis stores false as 0 for whatever reason, so we'll store as a string and parse it out | ||
return r.redisClient.Set(ctx, key, strconv.FormatBool(value), r.redisOptions.TTL).Err() | ||
} |
Oops, something went wrong.