-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # go.mod
- Loading branch information
Showing
43 changed files
with
1,813 additions
and
42 deletions.
There are no files selected for viewing
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
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,8 @@ | ||
package redis | ||
|
||
type Config struct { | ||
Enable bool `json:"enable"` | ||
Addrs []string `json:"addrs" label:"redis 节点列表"` | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} |
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,95 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"github.com/eolinker/apinto/resources" | ||
"github.com/eolinker/eosc" | ||
"github.com/eolinker/eosc/env" | ||
"github.com/eolinker/eosc/log" | ||
"github.com/go-redis/redis/v8" | ||
"reflect" | ||
) | ||
|
||
type Controller struct { | ||
current *_Cacher | ||
config Config | ||
} | ||
|
||
func (m *Controller) ConfigType() reflect.Type { | ||
return configType | ||
} | ||
func (m *Controller) shutdown() { | ||
oldClient := m.current | ||
if oldClient != nil { | ||
m.current = nil | ||
resources.ReplaceCacher() | ||
oldClient.client.Close() | ||
} | ||
} | ||
func (m *Controller) Set(conf interface{}) (err error) { | ||
config, ok := conf.(*Config) | ||
if ok && config != nil { | ||
old := m.config | ||
m.config = *config | ||
|
||
if reflect.DeepEqual(old, m.config) { | ||
return nil | ||
} | ||
|
||
if m.config.Enable && len(m.config.Addrs) > 0 { | ||
|
||
client := redis.NewClusterClient(&redis.ClusterOptions{ | ||
Addrs: m.config.Addrs, | ||
Username: m.config.Username, | ||
Password: m.config.Password, | ||
}) | ||
|
||
if res, errPing := client.Ping(context.Background()).Result(); errPing != nil { | ||
log.Info("ping redis:", res, " error:", err) | ||
client.Close() | ||
return errPing | ||
} | ||
|
||
if env.Process() == eosc.ProcessWorker { | ||
if m.current == nil { | ||
m.current = newCacher(client) | ||
resources.ReplaceCacher(m.current) | ||
} else { | ||
m.current.client = client | ||
} | ||
} else { | ||
client.Close() | ||
} | ||
return nil | ||
} | ||
} | ||
oldClient := m.current | ||
if oldClient != nil { | ||
resources.ReplaceCacher() | ||
m.current = nil | ||
oldClient.client.Close() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Controller) Get() interface{} { | ||
return m.config | ||
} | ||
|
||
func (m *Controller) Mode() eosc.SettingMode { | ||
return eosc.SettingModeSingleton | ||
} | ||
|
||
func (m *Controller) Check(cfg interface{}) (profession, name, driver, desc string, err error) { | ||
err = eosc.ErrorUnsupportedKind | ||
return | ||
} | ||
|
||
func (m *Controller) AllWorkers() []string { | ||
return []string{"redis@setting"} | ||
} | ||
|
||
func NewController() *Controller { | ||
return &Controller{} | ||
} |
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,21 @@ | ||
package redis | ||
|
||
import ( | ||
"github.com/eolinker/eosc" | ||
"github.com/eolinker/eosc/setting" | ||
"reflect" | ||
) | ||
|
||
var ( | ||
singleton *Controller | ||
_ eosc.ISetting = singleton | ||
configType = reflect.TypeOf(new(Config)) | ||
) | ||
|
||
func init() { | ||
singleton = NewController() | ||
} | ||
|
||
func Register(register eosc.IExtenderDriverRegister) { | ||
setting.RegisterSetting("redis", singleton) | ||
} |
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,65 @@ | ||
package redis | ||
|
||
import ( | ||
"context" | ||
"github.com/eolinker/apinto/resources" | ||
"github.com/go-redis/redis/v8" | ||
"time" | ||
) | ||
|
||
var ( | ||
_ resources.ICache = (*_Cacher)(nil) | ||
) | ||
|
||
type _Cacher struct { | ||
client *redis.ClusterClient | ||
} | ||
|
||
func (r *_Cacher) Close() error { | ||
if r.client == nil { | ||
e := r.client.Close() | ||
r.client = nil | ||
return e | ||
} | ||
return nil | ||
} | ||
|
||
func (r *_Cacher) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error { | ||
|
||
return r.client.Set(ctx, key, value, expiration).Err() | ||
} | ||
|
||
func (r *_Cacher) SetNX(ctx context.Context, key string, value []byte, expiration time.Duration) (bool, error) { | ||
|
||
return r.client.SetNX(ctx, key, value, expiration).Result() | ||
} | ||
|
||
func (r *_Cacher) DecrBy(ctx context.Context, key string, decrement int64) (int64, error) { | ||
|
||
return r.client.DecrBy(ctx, key, decrement).Result() | ||
} | ||
|
||
func (r *_Cacher) IncrBy(ctx context.Context, key string, decrement int64) (int64, error) { | ||
return r.client.IncrBy(ctx, key, decrement).Result() | ||
} | ||
|
||
func (r *_Cacher) Get(ctx context.Context, key string) ([]byte, error) { | ||
return r.client.Get(ctx, key).Bytes() | ||
|
||
} | ||
|
||
func (r *_Cacher) GetDel(ctx context.Context, key string) ([]byte, error) { | ||
return r.client.GetDel(ctx, key).Bytes() | ||
|
||
} | ||
|
||
func (r *_Cacher) Del(ctx context.Context, keys ...string) (int64, error) { | ||
return r.client.Del(ctx, keys...).Result() | ||
} | ||
|
||
func newCacher(client *redis.ClusterClient) *_Cacher { | ||
if client == nil { | ||
return nil | ||
} | ||
return &_Cacher{client: client} | ||
} |
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
Oops, something went wrong.