From 0692b9048ef6c53e73348ffc1873f8f996481f4b Mon Sep 17 00:00:00 2001 From: Fenny <25108519+Fenny@users.noreply.github.com> Date: Sat, 31 Oct 2020 11:58:00 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=B4=20update=20skeleton?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- memcached/config.go | 10 +++++++++- memcached/memcached.go | 25 +++++++++++++++++++------ memory/config.go | 2 +- memory/memory.go | 2 +- mysql/config.go | 10 +++++++++- mysql/mysql.go | 25 +++++++++++++++++++++---- postgres/config.go | 10 +++++++++- postgres/postgres.go | 25 +++++++++++++++++++++---- sqlite3/config.go | 10 +++++++++- sqlite3/sqlite3.go | 21 ++++++++++++++++++--- 10 files changed, 117 insertions(+), 23 deletions(-) diff --git a/memcached/config.go b/memcached/config.go index 967038eb..7bd5071d 100644 --- a/memcached/config.go +++ b/memcached/config.go @@ -1,13 +1,21 @@ package memcached +import "time" + // Config defines the config for storage. type Config struct { + GCInterval time.Duration } // ConfigDefault is the default config -var ConfigDefault = Config{} +var ConfigDefault = Config{ + GCInterval: 10 * time.Second, +} // Helper function to set default values func configDefault(cfg Config) Config { + if int(cfg.GCInterval) == 0 { + cfg.GCInterval = ConfigDefault.GCInterval + } return cfg } diff --git a/memcached/memcached.go b/memcached/memcached.go index a021e151..a8a8b138 100644 --- a/memcached/memcached.go +++ b/memcached/memcached.go @@ -1,11 +1,10 @@ package memcached -import ( - "time" -) +import "time" // Storage interface that is implemented by storage providers type Storage struct { + gcInterval time.Duration } // New creates a new storage @@ -18,10 +17,15 @@ func New(config ...Config) *Storage { cfg = configDefault(config[0]) } - // TODO - _ = cfg + // Create storage + store := &Storage{ + gcInterval: cfg.GCInterval, + } - return &Storage{} + // Start garbage collector + go store.gc() + + return store } // Get value by key @@ -43,3 +47,12 @@ func (s *Storage) Delete(key string) error { func (s *Storage) Clear() error { return nil } + +// Garbage collector to delete expired keys +func (s *Storage) gc() { + tick := time.NewTicker(s.gcInterval) + for { + <-tick.C + // clean entries + } +} diff --git a/memory/config.go b/memory/config.go index aefaa2c5..d6f28bec 100644 --- a/memory/config.go +++ b/memory/config.go @@ -2,7 +2,7 @@ package memory import "time" -// Config defines the config for memory storage. +// Config defines the config for storage. type Config struct { GCInterval time.Duration } diff --git a/memory/memory.go b/memory/memory.go index 86633ae3..cbade5be 100644 --- a/memory/memory.go +++ b/memory/memory.go @@ -33,7 +33,7 @@ func New(config ...Config) *Storage { gcInterval: cfg.GCInterval, } - // start garbage collector + // Start garbage collector go store.gc() return store diff --git a/mysql/config.go b/mysql/config.go index b93d8a52..35ce7c48 100644 --- a/mysql/config.go +++ b/mysql/config.go @@ -1,13 +1,21 @@ package mysql +import "time" + // Config defines the config for storage. type Config struct { + GCInterval time.Duration } // ConfigDefault is the default config -var ConfigDefault = Config{} +var ConfigDefault = Config{ + GCInterval: 10 * time.Second, +} // Helper function to set default values func configDefault(cfg Config) Config { + if int(cfg.GCInterval) == 0 { + cfg.GCInterval = ConfigDefault.GCInterval + } return cfg } diff --git a/mysql/mysql.go b/mysql/mysql.go index cc027223..67c8682f 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -1,9 +1,12 @@ package mysql -import "time" +import ( + "time" +) // Storage interface that is implemented by storage providers type Storage struct { + gcInterval time.Duration } // New creates a new storage @@ -16,10 +19,15 @@ func New(config ...Config) *Storage { cfg = configDefault(config[0]) } - // TODO - _ = cfg + // Create storage + store := &Storage{ + gcInterval: cfg.GCInterval, + } - return &Storage{} + // Start garbage collector + go store.gc() + + return store } // Get value by key @@ -41,3 +49,12 @@ func (s *Storage) Delete(key string) error { func (s *Storage) Clear() error { return nil } + +// Garbage collector to delete expired keys +func (s *Storage) gc() { + tick := time.NewTicker(s.gcInterval) + for { + <-tick.C + // clean entries + } +} diff --git a/postgres/config.go b/postgres/config.go index 9820dc8e..252ea323 100644 --- a/postgres/config.go +++ b/postgres/config.go @@ -1,13 +1,21 @@ package postgres +import "time" + // Config defines the config for storage. type Config struct { + GCInterval time.Duration } // ConfigDefault is the default config -var ConfigDefault = Config{} +var ConfigDefault = Config{ + GCInterval: 10 * time.Second, +} // Helper function to set default values func configDefault(cfg Config) Config { + if int(cfg.GCInterval) == 0 { + cfg.GCInterval = ConfigDefault.GCInterval + } return cfg } diff --git a/postgres/postgres.go b/postgres/postgres.go index 9f96854c..2818d6c1 100644 --- a/postgres/postgres.go +++ b/postgres/postgres.go @@ -1,9 +1,12 @@ package postgres -import "time" +import ( + "time" +) // Storage interface that is implemented by storage providers type Storage struct { + gcInterval time.Duration } // New creates a new storage @@ -16,10 +19,15 @@ func New(config ...Config) *Storage { cfg = configDefault(config[0]) } - // TODO - _ = cfg + // Create storage + store := &Storage{ + gcInterval: cfg.GCInterval, + } - return &Storage{} + // Start garbage collector + go store.gc() + + return store } // Get value by key @@ -41,3 +49,12 @@ func (s *Storage) Delete(key string) error { func (s *Storage) Clear() error { return nil } + +// Garbage collector to delete expired keys +func (s *Storage) gc() { + tick := time.NewTicker(s.gcInterval) + for { + <-tick.C + // clean entries + } +} diff --git a/sqlite3/config.go b/sqlite3/config.go index 52030b3d..3ae480bd 100644 --- a/sqlite3/config.go +++ b/sqlite3/config.go @@ -1,13 +1,21 @@ package sqlite3 +import "time" + // Config defines the config for storage. type Config struct { + GCInterval time.Duration } // ConfigDefault is the default config -var ConfigDefault = Config{} +var ConfigDefault = Config{ + GCInterval: 10 * time.Second, +} // Helper function to set default values func configDefault(cfg Config) Config { + if int(cfg.GCInterval) == 0 { + cfg.GCInterval = ConfigDefault.GCInterval + } return cfg } diff --git a/sqlite3/sqlite3.go b/sqlite3/sqlite3.go index d8a6ad53..1d33489b 100644 --- a/sqlite3/sqlite3.go +++ b/sqlite3/sqlite3.go @@ -4,6 +4,7 @@ import "time" // Storage interface that is implemented by storage providers type Storage struct { + gcInterval time.Duration } // New creates a new storage @@ -16,10 +17,15 @@ func New(config ...Config) *Storage { cfg = configDefault(config[0]) } - // TODO - _ = cfg + // Create storage + store := &Storage{ + gcInterval: cfg.GCInterval, + } - return &Storage{} + // Start garbage collector + go store.gc() + + return store } // Get value by key @@ -41,3 +47,12 @@ func (s *Storage) Delete(key string) error { func (s *Storage) Clear() error { return nil } + +// Garbage collector to delete expired keys +func (s *Storage) gc() { + tick := time.NewTicker(s.gcInterval) + for { + <-tick.C + // clean entries + } +}