Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
lele committed Oct 24, 2023
0 parents commit 6a28087
Show file tree
Hide file tree
Showing 16 changed files with 959 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-NOW lives<happs.lives@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
34 changes: 34 additions & 0 deletions bit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package redigo_pack

import "github.com/gomodule/redigo/redis"

type bitRds struct {
}

func (b *bitRds) SetBit(key string, offset, value int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("setbit", key, offset, value))
}

func (b *bitRds) GetBit(key string, offset int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("getbit", key, offset))
}

func (b *bitRds) BitCount(key string, interval ...int64) *Reply {
c := pool.Get()
defer c.Close()
if len(interval) == 2 {
return getReply(c.Do("bitcount", key, interval[0], interval[1]))
}
return getReply(c.Do("bitcount", key))
}

// opt 包含 and、or、xor、not
func (b *bitRds) BitTop(opt, destKey string, keys ...string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("bitop", opt, redis.Args{}.Add(keys).AddFlat(keys)))
}
29 changes: 29 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package redigo_pack

import (
"github.com/gomodule/redigo/redis"
)

type RedigoPack struct {
String stringRds
List listRds
Hash hashRds
Key keyRds
Set setRds
ZSet zSetRds
Bit bitRds
Db dbRds
}

var RedigoConn = new(RedigoPack)

func NewConnectionWithFile(addr, password string) error {

initPool(addr, password)
return nil
}

func NewConnectionByPool(pool2 *redis.Pool) error {
initPoolByOld(pool2)
return nil
}
16 changes: 16 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package redigo_pack

type dbRds struct {
}

func (d *dbRds) SelectDb(db int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("select", db))
}

func (d *dbRds) Do(commend string, args ...interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do(commend, args...))
}
5 changes: 5 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
基于redigo的封装
*/
package redigo_pack

13 changes: 13 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package redigo_pack

import (
"fmt"

"github.com/gomodule/redigo/redis"
)

var (
ErrorConfig error = fmt.Errorf("请先初始化redis配置")
)

var NilError = redis.ErrNil
100 changes: 100 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package redigo_pack

import "github.com/gomodule/redigo/redis"

type hashRds struct {
}

//exist 为true 表示字段不存则设置其值
func (h *hashRds) HSet(key string, filed, value interface{}, exist ...bool) *Reply {
c := pool.Get()
defer c.Close()
if len(exist) > 0 && exist[0] {
return getReply(c.Do("hsetex", key, filed, value))
}
return getReply(c.Do("hset", key, filed, value))
}

//获取指定字段值
func (h *hashRds) HGet(key string, filed interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hget", key, filed))
}

//获取所有字段及值
func (h *hashRds) HGetAll(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hgetall", key))
}

//设置多个字段及值 [map]
func (h *hashRds) HMSetFromMap(key string, mp map[interface{}]interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hmset", redis.Args{}.Add(key).AddFlat(mp)...))
}

//设置多个字段及值 [struct]
func (h *hashRds) HMSetFromStruct(key string, obj interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hmset", redis.Args{}.Add(key).AddFlat(obj)...))
}

//返回多个字段值
func (h *hashRds) HMGet(key string, fileds interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hmget", redis.Args{}.Add(key).AddFlat(fileds)...))
}

//字段删除
func (h *hashRds) HDel(key string, fileds interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hdel", redis.Args{}.Add(key).AddFlat(fileds)...))
}

//判断字段是否存在
func (h *hashRds) HExists(key string, filed interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hexists", key, filed))
}

//返回所有字段
func (h *hashRds) HKeys(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hkeys", key))
}

//返回字段数量
func (h *hashRds) HLen(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hlen", key))
}

//返回所有字段值
func (h *hashRds) HVals(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hvals", key))
}

//为指定字段值增加
func (h *hashRds) HIncrBy(key string, filed interface{}, increment interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hincrby", key, filed, increment))
}

//为指定字段值增加浮点数
func (h *hashRds) HIncrByFloat(key string, filed interface{}, increment float64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("hincrbyfloat", key, filed, increment))
}
118 changes: 118 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package redigo_pack

import "github.com/gomodule/redigo/redis"

type keyRds struct {
}

// 查找键 [*模糊查找]
func (k *keyRds) Keys(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("keys", key))
}

// 判断key是否存在
func (k *keyRds) Exists(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("exists", key))
}

// 随机返回一个key
func (k *keyRds) RandomKey() *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("randomkey"))
}

// 返回值类型
func (k *keyRds) Type(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("type", key))
}

// 删除key
func (k *keyRds) Del(keys ...string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("del", redis.Args{}.AddFlat(keys)...))
}

//重命名
func (k *keyRds) Rename(key, newKey string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("rename", key, newKey))
}

// 仅当newkey不存在时重命名
func (k *keyRds) RenameNX(key, newKey string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("renamenx", key, newKey))
}

// 序列化key
func (k *keyRds) Dump(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("dump", key))
}

// 反序列化
func (k *keyRds) Restore(key string, ttl, serializedValue interface{}) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("restore", key, ttl, serializedValue))
}

// 秒
func (k *keyRds) Expire(key string, seconds int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("expire", key, seconds))
}

// 秒
func (k *keyRds) ExpireAt(key string, timestamp int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("expireat", key, timestamp))
}

// 毫秒
func (k *keyRds) Persist(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("persist", key))
}

// 毫秒
func (k *keyRds) PersistAt(key string, milliSeconds int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("persistat", key, milliSeconds))
}

// 秒
func (k *keyRds) TTL(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("ttl", key))
}

// 毫秒
func (k *keyRds) PTTL(key string) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("pttl", key))
}

// 同实例不同库间的键移动
func (k *keyRds) Move(key string, db int64) *Reply {
c := pool.Get()
defer c.Close()
return getReply(c.Do("move", key, db))
}
Loading

0 comments on commit 6a28087

Please sign in to comment.