This commit is contained in:
boyce
2021-04-23 09:28:04 +08:00
2 changed files with 86 additions and 1 deletions

View File

@@ -125,6 +125,42 @@ func (m *RedisModule) TestPingRedis() error {
return nil
}
func (m *RedisModule) HSetStruct(key string, val interface{}) error {
conn, err := m.getConn()
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Do("HSET", redis.Args{}.Add(key).AddFlat(val)...)
if err != nil {
return err
}
return nil
}
func (m *RedisModule) HGetStruct(key string, out_val interface{}) error {
conn, err := m.getConn()
if err != nil {
return err
}
defer conn.Close()
v, err := redis.Values(conn.Do("HGETALL", key))
if err != nil {
return err
}
err = redis.ScanStruct(v, out_val)
if err != nil {
return err
}
return nil
}
func (m *RedisModule) SetString(key, value interface{}) (err error) {
err = m.setStringByExpire(key, value, "-1")
@@ -185,6 +221,39 @@ func (m *RedisModule) setStringByExpire(key, value, expire interface{}) error {
return nil
}
func (m *RedisModule) HSetStruct(key string, val interface{}) error {
conn, err := m.getConn()
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Do("HSET", redis.Args{}.Add(key).AddFlat(val)...)
if err != nil {
return err
}
return nil
}
func (m *RedisModule) HGetStruct(key string, out_val interface{}) error {
conn, err := m.getConn()
if err != nil {
return err
}
defer conn.Close()
v, err := redis.Values(conn.Do("HGETALL", key))
if err != nil {
return err
}
err = redis.ScanStruct(v, out_val)
if err != nil {
return err
}
return nil
}
func (m *RedisModule) SetStringMap(mapInfo map[interface{}]interface{}) (err error) {
err = m.setMuchStringByExpire(mapInfo, "-1")
return

View File

@@ -24,13 +24,29 @@ func Test_Example(t *testing.T) {
module.SetString(1000,2*1000)
module.SetStringExpire("key2","value2","60")
//支持直接存储struct数据
//支持直接存储struct数据(json string)
jsonData := struct{
A string
B string
}{"Aaaa","Bbbb"}
module.SetStringJSON("key3",&jsonData)
// struct 2 redis hash
a := struct{
A string `redis:"a"`
B int `redis:"b"`
}{
"ccc",
1,
}
module.HSetStruct("skey", &a)
a.A = "xx"
module.HGetStruct("skey", &a)
if a.A == "xx"{
fmt.Println("struct 2 hash failed.")
}
//支持存储map数据结构map中的key与value与redis的key,value对象
mapData := map[interface{}]interface{}{}
mapData["key4"] = "value4"