init
This commit is contained in:
177
serve/cache/logic/memory.go
vendored
Normal file
177
serve/cache/logic/memory.go
vendored
Normal file
@ -0,0 +1,177 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"Edu/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),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user