-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1009 from Fenny/master
🧹 housekeeping
- Loading branch information
Showing
38 changed files
with
2,298 additions
and
806 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package memory | ||
|
||
import "time" | ||
|
||
// Config defines the config for storage. | ||
type Config struct { | ||
// Time before deleting expired keys | ||
// | ||
// Default is 10 * time.Second | ||
GCInterval time.Duration | ||
} | ||
|
||
// ConfigDefault is the default config | ||
var ConfigDefault = Config{ | ||
GCInterval: 10 * time.Second, | ||
} | ||
|
||
// configDefault is a helper function to set default values | ||
func configDefault(config ...Config) Config { | ||
// Return default config if nothing provided | ||
if len(config) < 1 { | ||
return ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
// Set default values | ||
if int(cfg.GCInterval.Seconds()) <= 0 { | ||
cfg.GCInterval = ConfigDefault.GCInterval | ||
} | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package memory | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Storage interface that is implemented by storage providers | ||
type Storage struct { | ||
mux sync.RWMutex | ||
db map[string]entry | ||
gcInterval time.Duration | ||
done chan struct{} | ||
} | ||
|
||
// Common storage errors | ||
var ErrNotExist = errors.New("key does not exist") | ||
|
||
type entry struct { | ||
data []byte | ||
expiry int64 | ||
} | ||
|
||
// New creates a new memory storage | ||
func New(config ...Config) *Storage { | ||
// Set default config | ||
cfg := configDefault(config...) | ||
|
||
// Create storage | ||
store := &Storage{ | ||
db: make(map[string]entry), | ||
gcInterval: cfg.GCInterval, | ||
done: make(chan struct{}), | ||
} | ||
|
||
// Start garbage collector | ||
go store.gc() | ||
|
||
return store | ||
} | ||
|
||
// Get value by key | ||
func (s *Storage) Get(key string) ([]byte, error) { | ||
if len(key) <= 0 { | ||
return nil, ErrNotExist | ||
} | ||
s.mux.RLock() | ||
v, ok := s.db[key] | ||
s.mux.RUnlock() | ||
if !ok || v.expiry != 0 && v.expiry <= time.Now().Unix() { | ||
return nil, ErrNotExist | ||
} | ||
|
||
return v.data, nil | ||
} | ||
|
||
// Set key with value | ||
// Set key with value | ||
func (s *Storage) Set(key string, val []byte, exp time.Duration) error { | ||
// Ain't Nobody Got Time For That | ||
if len(key) <= 0 || len(val) <= 0 { | ||
return nil | ||
} | ||
|
||
var expire int64 | ||
if exp != 0 { | ||
expire = time.Now().Add(exp).Unix() | ||
} | ||
|
||
s.mux.Lock() | ||
s.db[key] = entry{val, expire} | ||
s.mux.Unlock() | ||
return nil | ||
} | ||
|
||
// Delete key by key | ||
func (s *Storage) Delete(key string) error { | ||
// Ain't Nobody Got Time For That | ||
if len(key) <= 0 { | ||
return nil | ||
} | ||
s.mux.Lock() | ||
delete(s.db, key) | ||
s.mux.Unlock() | ||
return nil | ||
} | ||
|
||
// Reset all keys | ||
func (s *Storage) Reset() error { | ||
s.mux.Lock() | ||
s.db = make(map[string]entry) | ||
s.mux.Unlock() | ||
return nil | ||
} | ||
|
||
// Close the memory storage | ||
func (s *Storage) Close() error { | ||
s.done <- struct{}{} | ||
return nil | ||
} | ||
|
||
func (s *Storage) gc() { | ||
ticker := time.NewTicker(s.gcInterval) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-s.done: | ||
return | ||
case t := <-ticker.C: | ||
now := t.Unix() | ||
s.mux.Lock() | ||
for id, v := range s.db { | ||
if v.expiry != 0 && v.expiry < now { | ||
delete(s.db, id) | ||
} | ||
} | ||
s.mux.Unlock() | ||
} | ||
} | ||
} |
Oops, something went wrong.