mirror of
https://github.com/grassrootseconomics/eth-tracker.git
synced 2026-09-03 16:03:15 +02:00
feat: add cache implementation
This commit is contained in:
38
internal/cache/cache.go
vendored
Normal file
38
internal/cache/cache.go
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/grassrootseconomics/celo-tracker/internal/chain"
|
||||
)
|
||||
|
||||
type (
|
||||
Cache interface {
|
||||
Purge() error
|
||||
Exists(string) bool
|
||||
Add(string) bool
|
||||
Size() int
|
||||
}
|
||||
|
||||
CacheOpts struct {
|
||||
Logg *slog.Logger
|
||||
Chain *chain.Chain
|
||||
CacheType string
|
||||
}
|
||||
)
|
||||
|
||||
func New(o CacheOpts) Cache {
|
||||
var (
|
||||
cache Cache
|
||||
)
|
||||
|
||||
switch o.CacheType {
|
||||
case "map":
|
||||
cache = NewMapCache()
|
||||
default:
|
||||
cache = NewMapCache()
|
||||
}
|
||||
o.Logg.Debug("bootstrapping cache")
|
||||
|
||||
return cache
|
||||
}
|
||||
39
internal/cache/map.go
vendored
Normal file
39
internal/cache/map.go
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"github.com/puzpuzpuz/xsync/v3"
|
||||
)
|
||||
|
||||
type (
|
||||
MapCache struct {
|
||||
mapCache *xsync.Map
|
||||
logg *slog.Logger
|
||||
}
|
||||
)
|
||||
|
||||
func NewMapCache() *MapCache {
|
||||
return &MapCache{
|
||||
mapCache: xsync.NewMap(),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *MapCache) Purge() error {
|
||||
c.mapCache.Clear()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *MapCache) Exists(key string) bool {
|
||||
_, ok := c.mapCache.Load(key)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (c *MapCache) Add(key string) bool {
|
||||
c.mapCache.Store(key, nil)
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *MapCache) Size() int {
|
||||
return c.mapCache.Size()
|
||||
}
|
||||
Reference in New Issue
Block a user