You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
3.6 KiB
147 lines
3.6 KiB
package rediscontroll
|
|
|
|
import (
|
|
"fmt"
|
|
"main_exam_server/common"
|
|
"main_exam_server/loges"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
/**
|
|
*列表(list)类型操作
|
|
*/
|
|
/*
|
|
将元素压入链表
|
|
@listName 列表名称
|
|
@listVal 列表值
|
|
call back 列表值数量,是否获取成功
|
|
*/
|
|
func ListPush(rdb *redis.Client, listName, listVal string) (int64, bool) {
|
|
//判断键是否存在
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return 0, false
|
|
}
|
|
// rdb := RedisInit()
|
|
num, err := rdb.LPush(ctx, listName, listVal).Result()
|
|
if err != nil {
|
|
errStr := fmt.Sprintf("值%v压入%v列表操作错误:%v", listVal, listName, err)
|
|
loges.LongInit(errStr)
|
|
return 0, false
|
|
}
|
|
return num, true
|
|
}
|
|
|
|
/*
|
|
在某个位置插入新元素
|
|
@listName 列表名称
|
|
@insertType 操作类型 before:在指定元素前插入;after:在指定元素后插入
|
|
@listConfirmVal 指定元素
|
|
@listVal 待插入值
|
|
*/
|
|
func ListInsert(rdb *redis.Client, listName, insertType, listConfirmVal, listVal string) bool {
|
|
//判断键是否存在
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return false
|
|
}
|
|
// rdb := RedisInit()
|
|
err := rdb.LInsert(ctx, listName, insertType, listConfirmVal, listVal).Err()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
/*
|
|
获取链表元素个数
|
|
@listName 列表名称
|
|
*/
|
|
func GetListLenght(rdb *redis.Client, listName string) int64 {
|
|
//判断键是否存在
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return 0
|
|
}
|
|
// rdb := RedisInit()
|
|
length, err := rdb.LLen(ctx, listName).Result()
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return length
|
|
}
|
|
|
|
/*
|
|
*获取链表下标对应的元素
|
|
@listName 列表名称
|
|
@listIndex 列表下标
|
|
call bakc 获取值,是否获取成功
|
|
*/
|
|
func GetListIndexVal(rdb *redis.Client, listName string, listIndex int64) (string, bool) {
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return "", false
|
|
}
|
|
// rdb := RedisInit()
|
|
val, err := rdb.LIndex(ctx, listName, listIndex).Result()
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
return val, true
|
|
}
|
|
|
|
/*
|
|
获取某个选定范围的元素集
|
|
@listName 列表名称
|
|
@startSubscript 下标索引的开始位置
|
|
@endtSubscript 下标索引的结束位置(不是要截取的长度)
|
|
call bakc 获取值,是否获取成功
|
|
*/
|
|
func GetListRange(rdb *redis.Client, listName string, startSubscript, endtSubscript int64) (map[int]interface{}, bool) {
|
|
varMap := common.MapOutint()
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return varMap, false
|
|
}
|
|
// rdb := RedisInit()
|
|
val, err := rdb.LRange(ctx, listName, startSubscript, endtSubscript).Result()
|
|
if err != nil {
|
|
return varMap, false
|
|
}
|
|
for index, val := range val {
|
|
varMap[index] = val
|
|
}
|
|
return varMap, true
|
|
}
|
|
|
|
/*
|
|
从链表左侧弹出数据
|
|
@listName 列表名称
|
|
*/
|
|
func ListPop(rdb *redis.Client, listName string) (string, bool) {
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return "", false
|
|
}
|
|
// rdb := RedisInit()
|
|
val, err := rdb.LPop(ctx, "list").Result()
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
return val, true
|
|
}
|
|
|
|
/*
|
|
根据值移除元素
|
|
@listName 列表名称
|
|
@listIndex listIndex > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 listIndex 。
|
|
listIndex < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 listIndex 的绝对值。
|
|
listIndex = 0 : 移除表中所有与 VALUE 相等的值。
|
|
@listVal 值元素
|
|
*/
|
|
func ListRem(rdb *redis.Client, listName, listVal string, listIndex int64) int64 {
|
|
if ExistsIsTrue(rdb, listName) != true {
|
|
return 0
|
|
}
|
|
// rdb := RedisInit()
|
|
val, err := rdb.LRem(ctx, listName, listIndex, listVal).Result()
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return val
|
|
}
|
|
|