当前位置:网站首页>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))}
边栏推荐
- Supplement to fusionui form component
- What causes the applet SSL certificate to expire? How to solve the problem when the applet SSL certificate expires?
- Open source C # WPF control library ---newbeecoder UI drop down box
- 发现一个大佬云集的宝藏硕博社群!
- 《阿里云天池大赛赛题解析》——O2O优惠卷预测
- Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon
- 同花顺股票开户是安全的吗?
- It's very interesting. Make an app to decorate the Christmas hat on Christmas!
- How to download offline versions of Firefox and chrome
- Basic concepts and common methods of syntactic dependency analysis
猜你喜欢

Introduction to scikit learn machine learning practice

Four aspects of PMO Department value assessment

Find My资讯|苹果可能会推出第二代AirTag,试试伦茨科技Find My方案
![Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text](/img/b1/71cc36c45102bdb9c06e099eb42267.jpg)
Harmonyos application development -- mynotepad[memo][api v6] based on textfield and image pseudo rich text

Bluetooth chip | Renesas and Ti launch new Bluetooth chip, try Lenz st17h65 Bluetooth ble5.2 chip

Simple code and design concept of "back to top"

Gradle asked seven times. You should know that~

Selenium批量查询运动员技术等级

实验五 模块、包和库

个税怎么算?你知道吗
随机推荐
CAD图在线Web测量工具代码实现(测量距离、面积、角度等)
Do you really understand the cache penetration, cache breakdown and cache avalanche in rotten street?
Cloud database smooth disassembly scheme
Shanghai benchmarking enterprise · Schneider Electric visited benchmarking learning lean production, smart logistics supply chain and digital transformation
What are the advantages of attaching a virtual machine to a hard disk cloud server
Basic concepts and common methods of syntactic dependency analysis
Build DNS server in Intranet
Simple code and design concept of "back to top"
微信小程序中发送网络请求
Prometheus primary body test
What is the process of opening a mobile card account? Is online account opening safe?
The transaction code mp83 at the initial level of SAP retail displays a prediction parameter file
I am 30 years old, no longer young, and have nothing
Phpkf CMS 3.00 beta y6 remote code execution
《阿里云天池大赛赛题解析》——O2O优惠卷预测
Wechat smart operation 3.0+ Alipay digital transformation 3.0
Who do you want to open a stock account? Is it safe to open an account online?
我在深圳,到哪里开户比较好?在线开户安全么?
Data visualization: summer without watermelon is not summer
Bing 404? Microsoft suspended "search suggestions" in Bing mainland for 30 days