eth-tracker/internal/cache/xmap.go

42 lines
675 B
Go
Raw Normal View History

2024-09-05 08:48:59 +02:00
package cache
import (
"context"
"github.com/puzpuzpuz/xsync/v3"
)
type mapCache struct {
xmap *xsync.Map
}
func NewMapCache() Cache {
return &mapCache{
xmap: xsync.NewMap(),
}
}
func (c *mapCache) Add(_ context.Context, key string) error {
c.xmap.Store(key, true)
return nil
}
func (c *mapCache) Remove(_ context.Context, key string) error {
c.xmap.Delete(key)
return nil
}
func (c *mapCache) Exists(_ context.Context, key ...string) (bool, error) {
for _, v := range key {
_, ok := c.xmap.Load(v)
if ok {
return true, nil
}
}
return false, nil
2024-09-05 08:48:59 +02:00
}
func (c *mapCache) Size(_ context.Context) (int64, error) {
return int64(c.xmap.Size()), nil
}