🦴 update skeleton

This commit is contained in:
Fenny
2020-10-31 11:58:00 +01:00
parent ef04d509b9
commit 0692b9048e
10 changed files with 117 additions and 23 deletions

View File

@@ -1,13 +1,21 @@
package memcached package memcached
import "time"
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { type Config struct {
GCInterval time.Duration
} }
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{} var ConfigDefault = Config{
GCInterval: 10 * time.Second,
}
// Helper function to set default values // Helper function to set default values
func configDefault(cfg Config) Config { func configDefault(cfg Config) Config {
if int(cfg.GCInterval) == 0 {
cfg.GCInterval = ConfigDefault.GCInterval
}
return cfg return cfg
} }

View File

@@ -1,11 +1,10 @@
package memcached package memcached
import ( import "time"
"time"
)
// Storage interface that is implemented by storage providers // Storage interface that is implemented by storage providers
type Storage struct { type Storage struct {
gcInterval time.Duration
} }
// New creates a new storage // New creates a new storage
@@ -18,10 +17,15 @@ func New(config ...Config) *Storage {
cfg = configDefault(config[0]) cfg = configDefault(config[0])
} }
// TODO // Create storage
_ = cfg store := &Storage{
gcInterval: cfg.GCInterval,
}
return &Storage{} // Start garbage collector
go store.gc()
return store
} }
// Get value by key // Get value by key
@@ -43,3 +47,12 @@ func (s *Storage) Delete(key string) error {
func (s *Storage) Clear() error { func (s *Storage) Clear() error {
return nil return nil
} }
// Garbage collector to delete expired keys
func (s *Storage) gc() {
tick := time.NewTicker(s.gcInterval)
for {
<-tick.C
// clean entries
}
}

View File

@@ -2,7 +2,7 @@ package memory
import "time" import "time"
// Config defines the config for memory storage. // Config defines the config for storage.
type Config struct { type Config struct {
GCInterval time.Duration GCInterval time.Duration
} }

View File

@@ -33,7 +33,7 @@ func New(config ...Config) *Storage {
gcInterval: cfg.GCInterval, gcInterval: cfg.GCInterval,
} }
// start garbage collector // Start garbage collector
go store.gc() go store.gc()
return store return store

View File

@@ -1,13 +1,21 @@
package mysql package mysql
import "time"
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { type Config struct {
GCInterval time.Duration
} }
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{} var ConfigDefault = Config{
GCInterval: 10 * time.Second,
}
// Helper function to set default values // Helper function to set default values
func configDefault(cfg Config) Config { func configDefault(cfg Config) Config {
if int(cfg.GCInterval) == 0 {
cfg.GCInterval = ConfigDefault.GCInterval
}
return cfg return cfg
} }

View File

@@ -1,9 +1,12 @@
package mysql package mysql
import "time" import (
"time"
)
// Storage interface that is implemented by storage providers // Storage interface that is implemented by storage providers
type Storage struct { type Storage struct {
gcInterval time.Duration
} }
// New creates a new storage // New creates a new storage
@@ -16,10 +19,15 @@ func New(config ...Config) *Storage {
cfg = configDefault(config[0]) cfg = configDefault(config[0])
} }
// TODO // Create storage
_ = cfg store := &Storage{
gcInterval: cfg.GCInterval,
}
return &Storage{} // Start garbage collector
go store.gc()
return store
} }
// Get value by key // Get value by key
@@ -41,3 +49,12 @@ func (s *Storage) Delete(key string) error {
func (s *Storage) Clear() error { func (s *Storage) Clear() error {
return nil return nil
} }
// Garbage collector to delete expired keys
func (s *Storage) gc() {
tick := time.NewTicker(s.gcInterval)
for {
<-tick.C
// clean entries
}
}

View File

@@ -1,13 +1,21 @@
package postgres package postgres
import "time"
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { type Config struct {
GCInterval time.Duration
} }
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{} var ConfigDefault = Config{
GCInterval: 10 * time.Second,
}
// Helper function to set default values // Helper function to set default values
func configDefault(cfg Config) Config { func configDefault(cfg Config) Config {
if int(cfg.GCInterval) == 0 {
cfg.GCInterval = ConfigDefault.GCInterval
}
return cfg return cfg
} }

View File

@@ -1,9 +1,12 @@
package postgres package postgres
import "time" import (
"time"
)
// Storage interface that is implemented by storage providers // Storage interface that is implemented by storage providers
type Storage struct { type Storage struct {
gcInterval time.Duration
} }
// New creates a new storage // New creates a new storage
@@ -16,10 +19,15 @@ func New(config ...Config) *Storage {
cfg = configDefault(config[0]) cfg = configDefault(config[0])
} }
// TODO // Create storage
_ = cfg store := &Storage{
gcInterval: cfg.GCInterval,
}
return &Storage{} // Start garbage collector
go store.gc()
return store
} }
// Get value by key // Get value by key
@@ -41,3 +49,12 @@ func (s *Storage) Delete(key string) error {
func (s *Storage) Clear() error { func (s *Storage) Clear() error {
return nil return nil
} }
// Garbage collector to delete expired keys
func (s *Storage) gc() {
tick := time.NewTicker(s.gcInterval)
for {
<-tick.C
// clean entries
}
}

View File

@@ -1,13 +1,21 @@
package sqlite3 package sqlite3
import "time"
// Config defines the config for storage. // Config defines the config for storage.
type Config struct { type Config struct {
GCInterval time.Duration
} }
// ConfigDefault is the default config // ConfigDefault is the default config
var ConfigDefault = Config{} var ConfigDefault = Config{
GCInterval: 10 * time.Second,
}
// Helper function to set default values // Helper function to set default values
func configDefault(cfg Config) Config { func configDefault(cfg Config) Config {
if int(cfg.GCInterval) == 0 {
cfg.GCInterval = ConfigDefault.GCInterval
}
return cfg return cfg
} }

View File

@@ -4,6 +4,7 @@ import "time"
// Storage interface that is implemented by storage providers // Storage interface that is implemented by storage providers
type Storage struct { type Storage struct {
gcInterval time.Duration
} }
// New creates a new storage // New creates a new storage
@@ -16,10 +17,15 @@ func New(config ...Config) *Storage {
cfg = configDefault(config[0]) cfg = configDefault(config[0])
} }
// TODO // Create storage
_ = cfg store := &Storage{
gcInterval: cfg.GCInterval,
}
return &Storage{} // Start garbage collector
go store.gc()
return store
} }
// Get value by key // Get value by key
@@ -41,3 +47,12 @@ func (s *Storage) Delete(key string) error {
func (s *Storage) Clear() error { func (s *Storage) Clear() error {
return nil return nil
} }
// Garbage collector to delete expired keys
func (s *Storage) gc() {
tick := time.NewTicker(s.gcInterval)
for {
<-tick.C
// clean entries
}
}