mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2025-02-23 00:02:00 +01:00
feat: optimize exists to check multiple keys in one call (#40)
* closes #32
This commit is contained in:
parent
fd59d286f5
commit
f1086fcdc1
2
internal/cache/cache.go
vendored
2
internal/cache/cache.go
vendored
@ -9,7 +9,7 @@ type (
|
|||||||
Cache interface {
|
Cache interface {
|
||||||
Add(context.Context, string) error
|
Add(context.Context, string) error
|
||||||
Remove(context.Context, string) error
|
Remove(context.Context, string) error
|
||||||
Exists(context.Context, string) (bool, error)
|
Exists(context.Context, ...string) (bool, error)
|
||||||
Size(context.Context) (int64, error)
|
Size(context.Context) (int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
4
internal/cache/redis.go
vendored
4
internal/cache/redis.go
vendored
@ -40,8 +40,8 @@ func (c *redisCache) Remove(ctx context.Context, key string) error {
|
|||||||
return c.client.Do(ctx, cmd).Error()
|
return c.client.Do(ctx, cmd).Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *redisCache) Exists(ctx context.Context, key string) (bool, error) {
|
func (c *redisCache) Exists(ctx context.Context, key ...string) (bool, error) {
|
||||||
cmd := c.client.B().Exists().Key(key).Build()
|
cmd := c.client.B().Exists().Key(key...).Build()
|
||||||
res, err := c.client.Do(ctx, cmd).AsBool()
|
res, err := c.client.Do(ctx, cmd).AsBool()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
11
internal/cache/xmap.go
vendored
11
internal/cache/xmap.go
vendored
@ -26,9 +26,14 @@ func (c *mapCache) Remove(_ context.Context, key string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *mapCache) Exists(_ context.Context, key string) (bool, error) {
|
func (c *mapCache) Exists(_ context.Context, key ...string) (bool, error) {
|
||||||
_, ok := c.xmap.Load(key)
|
for _, v := range key {
|
||||||
return ok, nil
|
_, ok := c.xmap.Load(v)
|
||||||
|
if ok {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *mapCache) Size(_ context.Context) (int64, error) {
|
func (c *mapCache) Size(_ context.Context) (int64, error) {
|
||||||
|
@ -147,16 +147,10 @@ func (hc *HandlerContainer) checkStables(ctx context.Context, from string, to st
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Pipeline this check on Redis with a new method
|
exists, err := hc.cache.Exists(ctx, from, to)
|
||||||
fromExists, err := hc.cache.Exists(ctx, from)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
toExists, err := hc.cache.Exists(ctx, to)
|
return exists, nil
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return fromExists || toExists, nil
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user