-
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.
- Loading branch information
Showing
5 changed files
with
129 additions
and
71 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,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,42 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
gocache "github.com/patrickmn/go-cache" | ||
) | ||
|
||
const ( | ||
deleteExpiredCacheItemsInternal = 10 * time.Minute | ||
) | ||
|
||
type MemoryCache struct { | ||
memoryClient *gocache.Cache | ||
memoryOptions *MemoryOptions | ||
} | ||
|
||
// MemoryOptions holds in memory cache configuration parameters. | ||
type MemoryOptions struct { | ||
TTL time.Duration | ||
} | ||
|
||
func NewMemoryCache(memoryOptions *MemoryOptions) *MemoryCache { | ||
return &MemoryCache{ | ||
memoryClient: gocache.New(memoryOptions.TTL, deleteExpiredCacheItemsInternal), | ||
memoryOptions: memoryOptions, | ||
} | ||
} | ||
|
||
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 | ||
} else { | ||
return false, false, nil | ||
} | ||
} | ||
|
||
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,55 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/go-redis/redis/v9" | ||
) | ||
|
||
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 | ||
} | ||
|
||
func NewRedisCache(redisOpts *RedisOptions) *RedisCache { | ||
return &RedisCache{ | ||
redisClient: redis.NewClient(&redis.Options{ | ||
Addr: redisOpts.Addr, | ||
Password: redisOpts.Password, | ||
DB: redisOpts.DB, | ||
}), | ||
redisOptions: redisOpts, | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
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 out in cacheGet | ||
return r.redisClient.Set(ctx, key, strconv.FormatBool(value), r.redisOptions.TTL).Err() | ||
} |
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