feat:完善项目

This commit is contained in:
henry
2021-09-28 11:47:19 +08:00
commit da7b3130fe
167 changed files with 456676 additions and 0 deletions

28
serve/cache/logic/cache.go vendored Normal file
View File

@ -0,0 +1,28 @@
package logic
type ICache interface {
Set(key string, value interface{}, expiration int) error
Get(key string) (string, error)
Del(key string) error
ZAdd(key string, members ...*ScoreParams) error
ZRangebyscore(key string, opt *ScoreRangeBy) ([]string, error)
ZRem(key string, members ...interface{}) error
HExists(key, field string) (bool, error)
HSet(key, field string, value interface{}) error
HGet(key, field string) (string, error)
HDel(key string, fields ...string) error
SAdd(key string, members ...interface{}) error
SIsMember(key string, members interface{}) (bool, error)
SRem(key string, members ...interface{}) error
Run() error
}
type ScoreParams struct {
Score float64
Member interface{}
}
type ScoreRangeBy struct {
Min, Max string
Offset, Count int64
}

177
serve/cache/logic/memory.go vendored Normal file
View File

@ -0,0 +1,177 @@
package logic
import (
"SciencesServer/utils"
"strings"
"sync"
)
type Memory struct {
cache map[string]interface{}
cacheList []interface{}
cacheSorted map[string][]*ScoreParams
locker *sync.RWMutex
}
func (this *Memory) Set(key string, value interface{}, expiration int) error {
this.locker.Lock()
defer this.locker.Unlock()
this.cache[key] = value
return nil
}
// Get
func (this *Memory) Get(key string) (string, error) {
this.locker.RLock()
defer this.locker.RUnlock()
data, has := this.cache[key]
if !has {
return "", nil
}
return utils.AnyToJSON(data), nil
}
// Del
func (this *Memory) Del(key string) error {
this.locker.Lock()
defer this.locker.Unlock()
delete(this.cache, key)
return nil
}
func (this *Memory) ZAdd(key string, members ...*ScoreParams) error {
this.locker.Lock()
defer this.locker.Unlock()
if _, has := this.cacheSorted[key]; !has {
this.cacheSorted[key] = members
} else {
this.cacheSorted[key] = append(this.cacheSorted[key], members...)
}
return nil
}
func (this *Memory) ZRangebyscore(key string, opt *ScoreRangeBy) ([]string, error) {
this.locker.Lock()
defer this.locker.Unlock()
if _, has := this.cacheSorted[key]; !has {
return []string{}, nil
}
out := make([]string, 0)
function := func(src string) (float64, bool) {
contain := false
if strings.Contains(opt.Min, "(") {
src = strings.Replace(src, "(", "", 1)
contain = true
}
f, _ := utils.StringToFloat(src)
return f, contain
}
min, minContain := function(opt.Min)
max, maxContain := function(opt.Max)
for _, v := range this.cacheSorted[key] {
if ((v.Score >= min && minContain) || v.Score > min) &&
((v.Score <= max && maxContain) || v.Score < max) {
out = append(out, utils.AnyToJSON(v.Member))
if opt.Count > 0 && int64(len(out)) > opt.Count {
return out, nil
}
}
}
return out, nil
}
func (this *Memory) ZRem(key string, members ...interface{}) error {
this.locker.Lock()
defer this.locker.Unlock()
if _, has := this.cacheSorted[key]; !has {
return nil
}
for k, v := range this.cacheSorted[key] {
for _, member := range members {
if v.Member == member {
this.cacheSorted[key] = append(this.cacheSorted[key][:k], this.cacheSorted[key][k+1:]...)
break
}
}
}
return nil
}
// HExists HASH Exist
func (this *Memory) HExists(key, field string) (bool, error) {
return this.cache[key] != nil, nil
}
// HSet HASH Set
func (this *Memory) HSet(key, field string, value interface{}) error {
this.locker.Lock()
defer this.locker.Unlock()
this.cache[key] = value
return nil
}
// HGet Hash Get
func (this *Memory) HGet(key, field string) (string, error) {
this.locker.RLock()
defer this.locker.RUnlock()
data, has := this.cache[key]
if !has {
return "", nil
}
return utils.AnyToJSON(data), nil
}
// HDel HASH Del
func (this *Memory) HDel(key string, fields ...string) error {
this.locker.Lock()
defer this.locker.Unlock()
delete(this.cache, key)
return nil
}
// SAdd
func (this *Memory) SAdd(key string, members ...interface{}) error {
this.locker.Lock()
defer this.locker.Unlock()
this.cacheList = append(this.cacheList, members...)
return nil
}
// SIsMember
func (this *Memory) SIsMember(key string, members interface{}) (bool, error) {
this.locker.RLock()
defer this.locker.RUnlock()
return utils.InArray(members, this.cacheList), nil
}
// SRem
func (this *Memory) SRem(key string, members ...interface{}) error {
this.locker.Lock()
defer this.locker.Unlock()
for k, v := range this.cacheList {
if utils.InArray(v, members) {
this.cacheList = append(this.cacheList[:k], this.cacheList[k+1:]...)
}
}
return nil
}
func (this *Memory) Run() error {
return nil
}
func NewMemory() *Memory {
return &Memory{
cache: make(map[string]interface{}, 0),
cacheList: make([]interface{}, 0),
cacheSorted: make(map[string][]*ScoreParams, 0),
locker: new(sync.RWMutex),
}
}

114
serve/cache/logic/redis.go vendored Normal file
View File

@ -0,0 +1,114 @@
package logic
import (
"time"
"github.com/pkg/errors"
"github.com/go-redis/redis"
)
type Redis struct {
*RedisOption
}
type RedisOption struct {
Addr string
Password string
DB int
MinIdleConns int
IdleTimeout int
}
var RedisClient *redis.Client
// Set
func (this *Redis) Set(key string, value interface{}, expiration int) error {
return RedisClient.Set(key, value, time.Duration(expiration)*time.Second).Err()
}
// Get
func (this *Redis) Get(key string) (string, error) {
return RedisClient.Get(key).Result()
}
// Del
func (this *Redis) Del(key string) error {
return RedisClient.Del(key).Err()
}
func (this *Redis) ZAdd(key string, members ...*ScoreParams) error {
redisZ := make([]redis.Z, 0)
for _, v := range members {
redisZ = append(redisZ, redis.Z{Score: v.Score, Member: v.Member})
}
return RedisClient.ZAdd(key, redisZ...).Err()
}
func (this *Redis) ZRangebyscore(key string, opt *ScoreRangeBy) ([]string, error) {
return RedisClient.ZRangeByScore(key, redis.ZRangeBy{Min: opt.Min, Max: opt.Max, Offset: opt.Offset, Count: opt.Count}).Result()
}
func (this *Redis) ZRem(key string, members ...interface{}) error {
return RedisClient.ZRem(key, members...).Err()
}
// HExists HASH Exist
func (this *Redis) HExists(key, field string) (bool, error) {
return RedisClient.HExists(key, field).Result()
}
// HSet HASH Set
func (this *Redis) HSet(key, field string, value interface{}) error {
return RedisClient.HSet(key, field, value).Err()
}
// HGet Hash Get
func (this *Redis) HGet(key, field string) (string, error) {
return RedisClient.HGet(key, field).Result()
}
// HDel HASH Del
func (this *Redis) HDel(key string, fields ...string) error {
return RedisClient.HDel(key, fields...).Err()
}
// SAdd
func (this *Redis) SAdd(key string, members ...interface{}) error {
return RedisClient.SAdd(key, members...).Err()
}
// SIsMember
func (this *Redis) SIsMember(key string, members interface{}) (bool, error) {
return RedisClient.SIsMember(key, members).Result()
}
// SRem
func (this *Redis) SRem(key string, members ...interface{}) error {
return RedisClient.SRem(key, members...).Err()
}
// Run 开启
func (this *Redis) Run() error {
option := &redis.Options{
Network: "",
Addr: this.Addr,
Password: this.Password,
DB: this.DB,
MinIdleConns: this.MinIdleConns,
IdleTimeout: time.Duration(this.IdleTimeout),
}
RedisClient = redis.NewClient(option)
ping, err := RedisClient.Ping().Result()
if err != nil {
return errors.New(ping + err.Error())
}
return nil
}
func NewRedis(option *RedisOption) *Redis {
return &Redis{option}
}

32
serve/cache/logic/redis_test.go vendored Normal file
View File

@ -0,0 +1,32 @@
package logic
import (
"github.com/go-redis/redis"
"testing"
)
var redisClient *redis.Client
func InitRedis() {
option := &redis.Options{
Network: "",
//Addr: this.Addr,
//Password: this.Password,
//DB: this.DB,
//MinIdleConns: this.MinIdleConns,
//IdleTimeout: time.Duration(this.IdleTimeout),
}
redisClient = redis.NewClient(option)
ping, err := redisClient.Ping().Result()
if err != nil {
panic(ping + err.Error())
}
}
func TestNewRedis(t *testing.T) {
InitRedis()
//redisClient.
}