This is a creation in Article, where the information may have evolved or changed.
Developing a Redis Data monitor program using the Go language
Brief introduction
The go language is a brand-new programming language launched by Google, with simple syntax but powerful features, which are highly acclaimed once launched.
In recent work, it is often necessary to monitor and manage data in Redis in real-time, just to develop some monitoring management tools using go.
This article demonstrates how to use go to develop a REDIS client program in order to develop an online user information for real-time monitoring and management of Redis.
Select Redis Go Client
The industry has a lot of good go client program, this article chooses to use Redigo, address: Github.com/garyburd/redigo/redis
Installation dependencies in Go are simple, with a GET command:
go get github.com/garyburd/redigo/redis
Parsing parameters with flag
Flag. String (name string, value string, usage string) *string
Flag. Parse ()
Simple to use, the only thing to note is that the type returned is a character pointer
You can also use
Flag. Stringvar (P *string, name string, value string, usage string)
Using the Redis pool
type pool struct {dial func () (conn , Error ) testonborrow func (c conn , t time . time ) error maxidle int maxactive int idletimeout time . duration ...}
Constructs a Redis pool object, mainly provides dial,testonborrow,maxidle,maxactive,idletimeout several parameters
Code logic is relatively simple, directly look at the code can
Full code
PackageMainImport("Flag" "FMT" "Github.com/garyburd/redigo/redis" "Time")var(CacheKey ="Onlineclient"Redisserver = flag. String ("Redisserver","10.1.1.10:6379","") Redispassword = flag. String ("Redispassword","","") UserId = flag. String ("UserId","","") Kickcmd = flag. String ("Kick","","") Clearcmd = flag. String ("Clear","",""))typeRedisclientstruct{Pool *redis. Pool}//Get all online usersfunc(c *redisclient) GetAll () {conn: = C.pool.get () clients, err: = Redis. STRINGMAP (Conn. Do ("Hgetall", CacheKey))ifErr! =Nil{Panic(ERR)} Fmt. Printf ("Online client:%d \ n",Len(clients)) forUId, Client: =RangeClients {FMT. Printf ("%s--%s\n", UId, Client)}}//Get a single user based on user IDfunc(c *redisclient) GetOne (IDstring{client, err: = Redis. String (C.pool.get (). Do ("Hget", CacheKey, id))ifErr! =Nil{Panic(ERR)} Fmt. PRINTLN (Client)}//Kick out a userfunc(c *redisclient) Kick (IDstring{result, err: = C.pool.get (). Do ("Hdel", CacheKey, id)ifErr! =Nil{Panic(ERR)} Fmt. PRINTLN (Result)}//Clear all online user informationfunc(c *redisclient) ClearAll () {result, err: = C.pool.get (). Do ("DEL", CacheKey)ifErr! =Nil{Panic(ERR)} Fmt. PRINTLN (Result)}//Close Redis Connection poolfunc(c *redisclient) Close () {ifC.pool! =Nil{C.pool.close ()}}funcNewclient (server, passwordstring) *redisclient {return&redisclient{pool:newpool (server, password),}}//Create Redis Connection poolfuncNewpool (server, passwordstring) *redis. Pool {return&redis. pool{Maxidle:3, IdleTimeout: -* Time. Second, Dial:func() (Redis. Conn, error) {c, err: = Redis. Dial ("TCP", server)ifErr! =Nil{return Nil, err}ifPassword! =""{if_, Err: = C.do ("AUTH", password); Err! =Nil{C.close ()return Nil, err}}returnCNil}, Testonborrow:func(c Redis.) Conn, T time. Time) Error {_, Err: = C.do ("PING")returnErr},}}funcMain () {flag. Parse () Client: = Newclient (*redisserver, *redispassword)deferClient. Close ()if*kickcmd! =""{Client. Kick (*userid)}if*clearcmd = ="All"{Client. ClearAll ()}if*userid = =""{Client. GetAll ()}Else{Client. GetOne (*userid)}}
Generate local programs that can be used in conjunction with other Linux tools
Compiling cost-to-place programs
Go build-o RC Redisclient.go
chmod a+x RC
To run the client program:
./RC
View Help information
./rc-h
Search Online User Information
./RC | grep ' username '
View individual online users
./rc-userid ' 10030 '
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.