mirror of
https://github.com/gofiber/storage.git
synced 2025-10-06 00:57:38 +08:00
📦 add memcache
This commit is contained in:
1
go.mod
1
go.mod
@@ -3,6 +3,7 @@ module github.com/gofiber/storage
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
|
||||
github.com/go-redis/redis/v8 v8.3.3
|
||||
github.com/gofiber/utils v0.1.0
|
||||
github.com/lib/pq v1.8.0
|
||||
|
2
go.sum
2
go.sum
@@ -3,6 +3,8 @@ github.com/aws/aws-sdk-go v1.34.28 h1:sscPpn/Ns3i0F4HPEWAVcwdIRaZZCuL7llJ2/60yPI
|
||||
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
|
||||
github.com/aws/aws-sdk-go v1.35.19 h1:vdIqQnOIqTNtvnOdt9r3Bf/FiCJ7KV/7O2BIj4TPx2w=
|
||||
github.com/aws/aws-sdk-go v1.35.19/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k=
|
||||
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0=
|
||||
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA=
|
||||
github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
|
1
memcache/README.md
Normal file
1
memcache/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# memcache
|
60
memcache/config.go
Normal file
60
memcache/config.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package memcache
|
||||
|
||||
import "time"
|
||||
|
||||
// Config defines the config for storage.
|
||||
type Config struct {
|
||||
GCInterval time.Duration
|
||||
|
||||
// Prefix key
|
||||
KeyPrefix string
|
||||
|
||||
// Server list
|
||||
ServerList []string
|
||||
|
||||
// The socket read/write timeout.
|
||||
//
|
||||
// Optional. Default is 100 * time.Millisecond
|
||||
Timeout time.Duration
|
||||
|
||||
// The maximum number of idle connections that will be maintained per address.
|
||||
//
|
||||
// Consider your expected traffic rates and latency carefully. This should
|
||||
// be set to a number higher than your peak parallel requests.
|
||||
//
|
||||
// Optional. Default is 2
|
||||
MaxIdleConns int
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
GCInterval: 10 * time.Second,
|
||||
Timeout: 100 * time.Millisecond,
|
||||
MaxIdleConns: 2,
|
||||
}
|
||||
|
||||
// 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 len(cfg.ServerList) < 1 {
|
||||
panic("ServerList must not be empty")
|
||||
}
|
||||
if int(cfg.GCInterval) == 0 {
|
||||
cfg.GCInterval = ConfigDefault.GCInterval
|
||||
}
|
||||
if int(cfg.Timeout) == 0 {
|
||||
cfg.Timeout = ConfigDefault.Timeout
|
||||
}
|
||||
if cfg.MaxIdleConns == 0 {
|
||||
cfg.MaxIdleConns = ConfigDefault.MaxIdleConns
|
||||
}
|
||||
return cfg
|
||||
}
|
@@ -1,21 +1,25 @@
|
||||
package memcached
|
||||
package memcache
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
|
||||
mc "github.com/bradfitz/gomemcache/memcache"
|
||||
)
|
||||
|
||||
// Storage interface that is implemented by storage providers
|
||||
type Storage struct {
|
||||
db *mc.Client
|
||||
gcInterval time.Duration
|
||||
}
|
||||
|
||||
// New creates a new storage
|
||||
func New(config ...Config) *Storage {
|
||||
// Set default config
|
||||
cfg := ConfigDefault
|
||||
cfg := configDefault(config...)
|
||||
|
||||
// Override config if provided
|
||||
if len(config) > 0 {
|
||||
cfg = configDefault(config[0])
|
||||
}
|
||||
db := mc.New(cfg.ServerList...)
|
||||
db.Timeout = cfg.Timeout
|
||||
db.MaxIdleConns = cfg.MaxIdleConns
|
||||
|
||||
// Create storage
|
||||
store := &Storage{
|
1
memcache/memcache_test.go
Normal file
1
memcache/memcache_test.go
Normal file
@@ -0,0 +1 @@
|
||||
package memcache
|
@@ -1 +0,0 @@
|
||||
# memcached
|
@@ -1,30 +0,0 @@
|
||||
package memcached
|
||||
|
||||
import "time"
|
||||
|
||||
// Config defines the config for storage.
|
||||
type Config struct {
|
||||
GCInterval time.Duration
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
var ConfigDefault = Config{
|
||||
GCInterval: 10 * time.Second,
|
||||
}
|
||||
|
||||
// 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) == 0 {
|
||||
cfg.GCInterval = ConfigDefault.GCInterval
|
||||
}
|
||||
return cfg
|
||||
}
|
@@ -1 +0,0 @@
|
||||
package memcached
|
Reference in New Issue
Block a user