redis/commands.go

166 lines
5.1 KiB
Go
Raw Normal View History

package redis
func stringsToAnys(in []string) []any {
a := make([]any, len(in))
for i, v := range in {
a[i] = v
}
return a
}
func (rd *Redis) DEL(keys ...string) int {
return rd.Do("DEL", stringsToAnys(keys)...).Int()
}
func (rd *Redis) EXISTS(key string) bool {
return rd.Do("EXISTS " + key).Bool()
}
func (rd *Redis) EXPIRE(key string, second int) bool {
if second > 315360000 {
return rd.Do("EXPIREAT "+key, second).Bool()
} else {
return rd.Do("EXPIRE "+key, second).Bool()
}
}
func (rd *Redis) KEYS(patten string) []string {
return rd.Do("KEYS " + patten).Strings()
}
func (rd *Redis) GET(key string) *Result {
return rd.Do("GET " + key)
}
func (rd *Redis) SET(key string, value any) bool {
return rd.Do("SET "+key, value).Bool()
}
func (rd *Redis) SETEX(key string, seconds int, value any) bool {
return rd.Do("SETEX "+key, seconds, value).Bool()
}
func (rd *Redis) SETNX(key string, value any) bool {
return rd.Do("SETNX "+key, value).Bool()
}
func (rd *Redis) GETSET(key string, value any) *Result {
return rd.Do("GETSET "+key, value)
}
func (rd *Redis) INCR(key string) int64 {
return rd.Do("INCR " + key).Int64()
}
func (rd *Redis) INCRBY(key string, delta int64) int64 {
return rd.Do("INCRBY "+key, delta).Int64()
}
func (rd *Redis) DECR(key string, delta int64) int64 {
return rd.Do("DECR "+key, delta).Int64()
}
func (rd *Redis) DECRBY(key string, delta int64) int64 {
return rd.Do("DECRBY "+key, delta).Int64()
}
func (rd *Redis) MGET(keys ...string) []Result {
return rd.Do("MGET", stringsToAnys(keys)...).Results()
}
func (rd *Redis) MSET(keyAndValues ...any) bool {
return rd.Do("MSET", keyAndValues...).Bool()
}
func (rd *Redis) HGET(key, field string) *Result {
return rd.Do("HGET "+key, field)
}
func (rd *Redis) HSET(key, field string, value any) bool {
return rd.Do("HSET "+key, field, value).Error == nil
}
func (rd *Redis) HSETNX(key, field string, value any) bool {
return rd.Do("HSETNX "+key, field, value).Error == nil
}
func (rd *Redis) HMGET(key string, fields ...string) []Result {
return rd.Do("HMGET", append(append([]any{}, key), stringsToAnys(fields)...)...).Results()
}
func (rd *Redis) HGETALL(key string) map[string]*Result {
return rd.Do("HGETALL " + key).ResultMap()
}
func (rd *Redis) HMSET(key string, fieldAndValues ...any) bool {
return rd.Do("HMSET", append(append([]any{}, key), fieldAndValues...)...).Bool()
}
func (rd *Redis) HKEYS(key string) []string {
return rd.Do("HKEYS " + key).Strings()
}
func (rd *Redis) HLEN(key string) int {
return rd.Do("HLEN " + key).Int()
}
func (rd *Redis) HDEL(key string, fields ...string) int {
return rd.Do("HDEL", append(append([]any{}, key), stringsToAnys(fields)...)...).Int()
}
func (rd *Redis) HEXISTS(key, field string) bool {
return rd.Do("HEXISTS "+key, field).Bool()
}
func (rd *Redis) HINCR(key, field string) int64 {
return rd.Do("HINCRBY "+key, field, 1).Int64()
}
func (rd *Redis) HINCRBY(key, field string, delta int64) int64 {
return rd.Do("HINCRBY "+key, field, delta).Int64()
}
func (rd *Redis) HDECR(key, field string) int64 {
return rd.Do("HDECRBY "+key, field, 1).Int64()
}
func (rd *Redis) HDECRBY(key, field string, delta int64) int64 {
return rd.Do("HDECRBY "+key, field, delta).Int64()
}
func (rd *Redis) LPUSH(key string, values ...string) int {
return rd.Do("LPUSH", append(append([]any{}, key), stringsToAnys(values)...)...).Int()
}
func (rd *Redis) RPUSH(key string, values ...string) int {
return rd.Do("RPUSH", append(append([]any{}, key), stringsToAnys(values)...)...).Int()
}
func (rd *Redis) LPOP(key string) *Result {
return rd.Do("LPOP " + key)
}
func (rd *Redis) RPOP(key string) *Result {
return rd.Do("RPOP " + key)
}
func (rd *Redis) LLEN(key string) int {
return rd.Do("LLEN " + key).Int()
}
func (rd *Redis) LRANGE(key string, start, stop int) []Result {
return rd.Do("LRANGE "+key, start, stop).Results()
}
func (rd *Redis) SADD(key string, values ...any) int {
return rd.Do("SADD", append([]any{key}, values...)...).Int()
}
func (rd *Redis) SREM(key string, values ...any) int {
return rd.Do("SREM", append([]any{key}, values...)...).Int()
}
func (rd *Redis) SCARD(key string) int {
return rd.Do("SCARD " + key).Int()
}
func (rd *Redis) SMEMBERS(key string) []Result {
return rd.Do("SMEMBERS " + key).Results()
}
func (rd *Redis) SISMEMBER(key string, value any) bool {
return rd.Do("SISMEMBER "+key, value).Bool()
}
func (rd *Redis) ZADD(key string, score float64, member any) bool {
return rd.Do("ZADD "+key, score, member).Bool()
}
func (rd *Redis) ZREM(key string, members ...any) int {
return rd.Do("ZREM", append([]any{key}, members...)...).Int()
}
func (rd *Redis) ZCARD(key string) int {
return rd.Do("ZCARD " + key).Int()
}
func (rd *Redis) ZRANGE(key string, start, stop int) []Result {
return rd.Do("ZRANGE "+key, start, stop).Results()
}
func (rd *Redis) ZREVRANGE(key string, start, stop int) []Result {
return rd.Do("ZREVRANGE "+key, start, stop).Results()
}
func (rd *Redis) ZRANK(key string, member any) int {
return rd.Do("ZRANK "+key, member).Int()
}
func (rd *Redis) ZREVRANK(key string, member any) int {
return rd.Do("ZREVRANK "+key, member).Int()
}
func (rd *Redis) ZSCORE(key string, member any) float64 {
return rd.Do("ZSCORE "+key, member).Float64()
}