summaryrefslogtreecommitdiff
path: root/plugins/kv/memory
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2021-01-07 13:29:07 +0300
committerValery Piashchynski <[email protected]>2021-01-07 13:29:07 +0300
commit21689afbc5f717274cd608358ffca3bdacb38f69 (patch)
tree159ddd2b154a5afbec5dedc75a1a94c5a83ebfae /plugins/kv/memory
parentc1465d3bcdf24a78440300aa51e7cfc92ce874a8 (diff)
Fix compatibility issues, add tests to the CI
Diffstat (limited to 'plugins/kv/memory')
-rw-r--r--plugins/kv/memory/config.go3
-rw-r--r--plugins/kv/memory/plugin.go23
-rw-r--r--plugins/kv/memory/storage_test.go17
3 files changed, 26 insertions, 17 deletions
diff --git a/plugins/kv/memory/config.go b/plugins/kv/memory/config.go
index 329e7fff..0816f734 100644
--- a/plugins/kv/memory/config.go
+++ b/plugins/kv/memory/config.go
@@ -4,9 +4,12 @@ package memory
type Config struct {
// Enabled or disabled (true or false)
Enabled bool
+ // Interval for the check
+ Interval int
}
// InitDefaults by default driver is turned off
func (c *Config) InitDefaults() {
c.Enabled = false
+ c.Interval = 60 // seconds
}
diff --git a/plugins/kv/memory/plugin.go b/plugins/kv/memory/plugin.go
index 2c65f14c..d2d3721b 100644
--- a/plugins/kv/memory/plugin.go
+++ b/plugins/kv/memory/plugin.go
@@ -11,27 +11,18 @@ import (
"github.com/spiral/roadrunner/v2/plugins/logger"
)
+// PluginName is user friendly name for the plugin
const PluginName = "memory"
type Plugin struct {
- heap *sync.Map
+ // heap is user map for the key-value pairs
+ heap sync.Map
stop chan struct{}
log logger.Logger
cfg *Config
}
-func NewInMemoryStorage() kv.Storage {
- p := &Plugin{
- heap: &sync.Map{},
- stop: make(chan struct{}),
- }
-
- go p.gc()
-
- return p
-}
-
func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
const op = errors.Op("in-memory storage init")
s.cfg = &Config{}
@@ -42,8 +33,7 @@ func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
return errors.E(op, err)
}
s.log = log
- // init in-memory
- s.heap = &sync.Map{}
+
s.stop = make(chan struct{}, 1)
return nil
}
@@ -119,7 +109,7 @@ func (s *Plugin) MGet(keys ...string) (map[string]interface{}, error) {
for i := range keys {
if value, ok := s.heap.Load(keys[i]); ok {
- m[keys[i]] = value
+ m[keys[i]] = value.(kv.Item).Value
}
}
@@ -242,7 +232,7 @@ func (s *Plugin) Name() string {
func (s *Plugin) gc() {
// TODO check
- ticker := time.NewTicker(time.Millisecond * 500)
+ ticker := time.NewTicker(time.Duration(s.cfg.Interval) * time.Second)
for {
select {
case <-s.stop:
@@ -262,6 +252,7 @@ func (s *Plugin) gc() {
}
if now.After(t) {
+ s.log.Debug("key deleted", "key", key)
s.heap.Delete(key)
}
return true
diff --git a/plugins/kv/memory/storage_test.go b/plugins/kv/memory/storage_test.go
index 4b30460d..d3b24860 100644
--- a/plugins/kv/memory/storage_test.go
+++ b/plugins/kv/memory/storage_test.go
@@ -7,11 +7,26 @@ import (
"time"
"github.com/spiral/roadrunner/v2/plugins/kv"
+ "github.com/spiral/roadrunner/v2/plugins/logger"
"github.com/stretchr/testify/assert"
+ "go.uber.org/zap"
)
func initStorage() kv.Storage {
- return NewInMemoryStorage()
+ p := &Plugin{
+ stop: make(chan struct{}),
+ }
+ p.cfg = &Config{
+ Enabled: true,
+ Interval: 1,
+ }
+
+ l, _ := zap.NewDevelopment()
+ p.log = logger.NewZapAdapter(l)
+
+ go p.gc()
+
+ return p
}
func cleanup(t *testing.T, s kv.Storage, keys ...string) {