当前位置:网站首页>Redis encapsulation instance
Redis encapsulation instance
2022-06-23 21:44:00 【Great inventor】
git clone https://github.com/ccf19881030/redisgoExample.git
Of course run go The premise of the project is to install golang development environment
Enter into redisgoExample Catalog , Execute the following command :
go mod init ybu.cn/iot
Use go mod init Command initializes a ybu.cn/iot Custom package for
And then again at redisgoExample Run in directory go get Command to install redisgo client :
go get github.com/gomodule/redigo/redis
At this time, there will be more go.mod and go.sum file , It contains redisgo Package Introduction .
go.mod The contents of the document are as follows :
module ybu.cn/iot
go 1.14
require github.com/gomodule/redigo v1.8.3
go.sum The contents of the document are as follows :
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gomodule/redigo v1.8.3 h1:HR0kYDX2RJZvAup8CsiJwxB4dTCSC0AaUq6S4SiLwUc=
github.com/gomodule/redigo v1.8.3/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/gomodule/redigo/redis v0.0.0-do-not-use h1:J7XIp6Kau0WoyT4JtXHT3Ei0gA1KkSc6bc87j9v9WIo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Self defined common package
stay redisgoExample Create a new one in the directory common Catalog , To create a array.go、define.go、interface.go These three go file , For some arrays 、redis To configure 、redis Basic operation of data structure ,
The contents are as follows :
1.array.go
package common
// ArrayOf does the array contain specified item
func ArrayOf(arr []string, dest string) bool { for i := 0; i < len(arr); i++ { if arr[i] == dest {return true
}
}
return false
}
// ArrayDuplice Array weight removal
func ArrayDuplice(arr []string) []string {var out []string
tmp := make(map[string]byte)
for _, v := range arr {tmplen := len(tmp)
tmp[v] = 0
if len(tmp) != tmplen {out = append(out, v)
}
}
return out
}
2.define.go
package common
// RedisConnOpt connect redis options
type RedisConnOpt struct {Enable bool
Host string
Port int32
Password string
Index int32
TTL int32
}
3.interface.go
package common
// RedisData Storage data structure
type RedisData struct {Key string
Field string
Value string
Expire int64
}
// RedisDataArray RedisData of array
type RedisDataArray []*RedisData
// IRedis redis client interface
type IRedis interface {// KEYS get patten key array
KEYS(patten string) ([]string, error)
// SCAN get patten key array
SCAN(patten string) ([]string, error)
// DEL delete k-v
DEL(key string) (int, error)
// DELALL delete key array
DELALL(key []string) (int, error)
// GET get k-v
GET(key string) (string, error)
// SET set k-v
//SET(key string, value string) (int64, error)
// SETEX set k-v expire seconds
SETEX(key string, sec int, value string) (int64, error)
// EXPIRE set key expire seconds
EXPIRE(key string, sec int64) (int64, error)
// HGETALL get map of key
HGETALL(key string) (map[string]string, error)
// HGET get value of key-field
HGET(key string, field string) (string, error)
// HSET set value of key-field
//HSET(key string, field string, value string) (int64, error)
// Write towards redis Write multiple sets of data in
Write(data RedisDataArray)
}
redisgo Encapsulation
stay redisgoExample Create a new one in the directory cache Catalog , Create a redis.go The file of , It is mainly used to encapsulate common redis command , It reads as follows :
package cache
import (
"fmt"
"log"
"time"
"github.com/gomodule/redigo/redis"
"ybu.cn/iot/common"
)
// https://godoc.org/github.com/gomodule/redigo/redis#pkg-examples
// https://github.com/gomodule/redigo
// RedisClient redis client instance
type RedisClient struct {pool *redis.Pool
connOpt common.RedisConnOpt
// Data reception
chanRx chan common.RedisDataArray
// Exit or not
isExit bool
}
// NewRedis new redis client
func NewRedis(opt common.RedisConnOpt) *RedisClient { return &RedisClient{connOpt: opt,
pool: newPool(opt),
chanRx: make(chan common.RedisDataArray, 100),
}
}
// newPool Thread pool
func newPool(opt common.RedisConnOpt) *redis.Pool { return &redis.Pool{MaxIdle: 3,
IdleTimeout: 240 * time.Second,
// MaxActive: 10,
// Wait: true,
Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", opt.Host, opt.Port)) if err != nil { log.Fatalf("Redis.Dial: %v", err)return nil, err
}
if _, err := c.Do("AUTH", opt.Password); err != nil {c.Close()
log.Fatalf("Redis.AUTH: %v", err)return nil, err
}
if _, err := c.Do("SELECT", opt.Index); err != nil {c.Close()
log.Fatalf("Redis.SELECT: %v", err)return nil, err
}
return c, nil
},
}
}
// Start Start the receive task coordinator
func (r *RedisClient) Start() {r.isExit = false
// Turn on the coroutine to receive data circularly
go r.loopRead()
}
// Stop Stop receiving tasks
func (r *RedisClient) Stop() {r.isExit = true
// Turn off the data receiving channel
close(r.chanRx)
// close redis Thread pool
r.pool.Close()
}
// Write towards redis Write multiple sets of data in
func (r *RedisClient) Write(data common.RedisDataArray) {r.chanRx <- data
}
// loopRead Loop receiving data
func (r *RedisClient) loopRead() { for !r.isExit { select {case rx := <-r.chanRx:
for _, it := range rx { if len(it.Key) > 0 { if len(it.Field) > 0 { if _, err := r.HSET(it.Key, it.Field, it.Value); err != nil { log.Printf("[%s, %s, %s]: %s\n", it.Key, it.Field, it.Value, err.Error())}
} else { if _, err := r.SET(it.Key, it.Value); err != nil { log.Printf("[%s, %s, %s]: %s\n", it.Key, it.Field, it.Value, err.Error())}
}
if it.Expire > 0 {r.EXPIRE(it.Key, it.Expire)
}
}
}
}
}
}
// Error get redis connect error
func (r *RedisClient) Error() error {conn := r.pool.Get()
defer conn.Close()
return conn.Err()
}
// Commonly used Redis Encapsulation of operation commands
// http://redis.io/commands
// KEYS get patten key array
func (r *RedisClient) KEYS(patten string) ([]string, error) {conn := r.pool.Get()
defer conn.Close()
return redis.Strings(conn.Do("KEYS", patten))}
// SCAN Get a lot of key
func (r *RedisClient) SCAN(patten string) ([]string, error) {conn := r.pool.Get()
defer conn.Close()
var out []string
var cursor uint64 = 0xffffff
isfirst := true
for cursor != 0 { if isfirst {cursor = 0
isfirst = false
}
arr, err := conn.Do("SCAN", cursor, "MATCH", patten, "COUNT", 100) if err != nil {return out, err
}
switch arr := arr.(type) { case []interface{}:cursor, _ = redis.Uint64(arr[0], nil)
it, _ := redis.Strings(arr[1], nil)
out = append(out, it...)
}
}
out = common.ArrayDuplice(out)
return out, nil
}
// DEL delete k-v
func (r *RedisClient) DEL(key string) (int, error) {conn := r.pool.Get()
defer conn.Close()
return redis.Int(conn.Do("DEL", key))}
// DELALL delete key array
func (r *RedisClient) DELALL(key []string) (int, error) {conn := r.pool.Get()
defer conn.Close()
arr := make([]interface{}, len(key)) for i, v := range key {arr[i] = v
}
return redis.Int(conn.Do("DEL", arr...))}
// GET get k-v
func (r *RedisClient) GET(key string) (string, error) {conn := r.pool.Get()
defer conn.Close()
return redis.String(conn.Do("GET", key))}
// SET set k-v
func (r *RedisClient) SET(key string, value string) (int64, error) {conn := r.pool.Get()
defer conn.Close()
return redis.Int64(conn.Do("SET", key, value))}
// SETEX set k-v expire seconds
func (r *RedisClient) SETEX(key string, sec int, value string) (int64, error) {conn := r.pool.Get()
defer conn.Close()
return redis.Int64(conn.Do("SETEX", key, sec, value))}
// EXPIRE set key expire seconds
func (r *RedisClient) EXPIRE(key string, sec int64) (int64, error) {conn := r.pool.Get()
defer conn.Close()
return redis.Int64(conn.Do("EXPIRE", key, sec))}
// HGETALL get map of key
func (r *RedisClient) HGETALL(key string) (map[string]string, error) {conn := r.pool.Get()
defer conn.Close()
return redis.StringMap(conn.Do("HGETALL", key))}
// HGET get value of key-field
func (r *RedisClient) HGET(key string, field string) (string, error) {conn := r.pool.Get()
defer conn.Close()
return redis.String(conn.Do("HGET", key, field))}
// HSET set value of key-field
func (r *RedisClient) HSET(key string, field string, value string) (int64, error) {conn := r.pool.Get()
defer conn.Close()
return redis.Int64(conn.Do("HSET", key, field, value))}
test redis client
stay redisgoExample Create a new one in the directory redisgoExample.go File for testing ,
It reads as follows :
package main
import (
"fmt"
"time"
"ybu.cn/iot/cache"
"ybu.cn/iot/common"
)
func main() { fmt.Println("redisgo client demo")// redis Configuration of
redisOpt := common.RedisConnOpt{true,
"127.0.0.1",
6379,
"123456",
3,
240,
}
_redisCli := cache.NewRedis(redisOpt)
// KEYS Example
keys, err := _redisCli.KEYS("0_last_gb212_2011:*") if err != nil { fmt.Println("KEYS failed, err: %v", err)}
for index, val := range keys { fmt.Printf(" The first %d The values are :%s\n", index + 1, val)}
// GET Example
key, err := _redisCli.GET("username") if err != nil { fmt.Println("GET failed, err: %v", err)}
fmt.Println("key: ", key)// SET Example
//i1, err := _redisCli.SET("month", "12") //if err != nil { // fmt.Println("SET failed, err: %v, %v", err, i1)//}
// HGET Example
name, err := _redisCli.HGET("animals", "name") age, err := _redisCli.HGET("animals", "age") sex, err := _redisCli.HGET("animals", "sex") color, err := _redisCli.HGET("animals", "color") fmt.Printf("animals: [name:%v], [age: %v], [sex: %v], [color: %v]\n", name, age, sex, color)// HGETALL Example
animalsMap, err := _redisCli.HGETALL("animals") for k, v := range animalsMap { fmt.Printf("k : %v, v: %v\t", k, v)}
// redis client
_redisCli.Start()
defer _redisCli.Stop()
t1 := time.Now().UnixNano() / 1e6
a1, _ := _redisCli.SCAN("GB212_20*")t2 := time.Now().UnixNano() / 1e6
a2, _ := _redisCli.KEYS("GB212_20*")t3 := time.Now().UnixNano() / 1e6
fmt.Printf("SCAN time: %d\tlen: %d\nKEYS time: %d\tlen: %d\n", t2-t1, len(a1), t3-t2, len(a2))}
边栏推荐
- Microservice architecture | how to solve the problem of fragment uploading of large attachments?
- 股票开户要找谁?网上开户安全么?
- Shanghai benchmarking enterprise · Schneider Electric visited benchmarking learning lean production, smart logistics supply chain and digital transformation
- Open source C # WPF control library ---newbeecoder UI drop down box
- Drawing STM32 minimum system schematic diagram with AD
- Share a super Mary source code
- Minimisé lorsque Outlook est allumé + éteint
- What about the cloud disk service status error? How to format the cloud disk service?
- Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text
- How does the video platform deployment give corresponding user permissions to the software package files?
猜你喜欢

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon

数据可视化之:没有西瓜的夏天不叫夏天

Minimize outlook startup + shutdown

HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder

《scikit-learn机器学习实战》简介

Facing the problem of lock waiting, how to realize the second level positioning and analysis of data warehouse

Smart cockpit SOC competition upgraded, and domestic 7Nm chips ushered in an important breakthrough

Sending network request in wechat applet

Outlook開機自啟+關閉時最小化
随机推荐
Bcdedit, used to adjust the machine startup parameters (safe mode, BootMenu display name, CPU, memory, etc.)
Those programmers wrote super funny 56 code comments (worth collecting)!!
Start optimization - directed acyclic graph
Global and Chinese market for hydropower plants 2022-2028: Research Report on technology, participants, trends, market size and share
Bluetooth chip | Renesas and Ti launch new Bluetooth chip, try Lenz st17h65 Bluetooth ble5.2 chip
Smart cockpit SOC competition upgraded, and domestic 7Nm chips ushered in an important breakthrough
HDLBits-&gt; Circuits-&gt; Arithmetic Circuitd-&gt; 3-bit binary adder
Initial experience of nodejs express framework
Stm32 w5500 implements TCP, DHCP and web server
Xgboost implements text classification and sklearn NLP library tfidfvectorizer
How to gradually improve PMO's own ability and management level
How to calculate individual income tax? You know what?
Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text
Cloud database smooth disassembly scheme
How do I clean the ECS hard disk? Why do I clean the hard disk regularly?
Troubleshooting the problem that the channel cannot play after easycvr cascades to the upper platform
SAP retail transaction code mp38 can perform forecasts for multiple items
Lightweight, dynamic and smooth listening, hero earphone hands-on experience, can really create
Analysis of visual analysis technology
《阿里云天池大赛赛题解析》——O2O优惠卷预测