在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:Redcon开源软件地址:https://gitee.com/mirrors/Redcon开源软件介绍:Redis compatible server framework for Go Features
Installinggo get -u github.com/tidwall/redcon ExampleHere's a full example of a Redis clone that accepts:
You can run this example from a terminal: go run example/clone.go package mainimport ( "log" "strings" "sync" "github.com/tidwall/redcon")var addr = ":6380"func main() { var mu sync.RWMutex var items = make(map[string][]byte) var ps redcon.PubSub go log.Printf("started server at %s", addr) err := redcon.ListenAndServe(addr, func(conn redcon.Conn, cmd redcon.Command) { switch strings.ToLower(string(cmd.Args[0])) { default: conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'") case "ping": conn.WriteString("PONG") case "quit": conn.WriteString("OK") conn.Close() case "set": if len(cmd.Args) != 3 { conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command") return } mu.Lock() items[string(cmd.Args[1])] = cmd.Args[2] mu.Unlock() conn.WriteString("OK") case "get": if len(cmd.Args) != 2 { conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command") return } mu.RLock() val, ok := items[string(cmd.Args[1])] mu.RUnlock() if !ok { conn.WriteNull() } else { conn.WriteBulk(val) } case "del": if len(cmd.Args) != 2 { conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command") return } mu.Lock() _, ok := items[string(cmd.Args[1])] delete(items, string(cmd.Args[1])) mu.Unlock() if !ok { conn.WriteInt(0) } else { conn.WriteInt(1) } case "publish": if len(cmd.Args) != 3 { conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command") return } conn.WriteInt(ps.Publish(string(cmd.Args[1]), string(cmd.Args[2]))) case "subscribe", "psubscribe": if len(cmd.Args) < 2 { conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command") return } command := strings.ToLower(string(cmd.Args[0])) for i := 1; i < len(cmd.Args); i++ { if command == "psubscribe" { ps.Psubscribe(conn, string(cmd.Args[i])) } else { ps.Subscribe(conn, string(cmd.Args[i])) } } } }, func(conn redcon.Conn) bool { // Use this function to accept or deny the connection. // log.Printf("accept: %s", conn.RemoteAddr()) return true }, func(conn redcon.Conn, err error) { // This is called when the connection has been closed // log.Printf("closed: %s, err: %v", conn.RemoteAddr(), err) }, ) if err != nil { log.Fatal(err) }} TLS ExampleRedcon has full TLS support through the The same example is also provided for serving Redcon over TLS. go run example/tls/clone.go BenchmarksRedis: Single-threaded, no disk persistence. $ redis-server --port 6379 --appendonly no redis-benchmark -p 6379 -t set,get -n 10000000 -q -P 512 -c 512SET: 941265.12 requests per secondGET: 1189909.50 requests per second Redcon: Single-threaded, no disk persistence. $ GOMAXPROCS=1 go run example/clone.go redis-benchmark -p 6380 -t set,get -n 10000000 -q -P 512 -c 512SET: 2018570.88 requests per secondGET: 2403846.25 requests per second Redcon: Multi-threaded, no disk persistence. $ GOMAXPROCS=0 go run example/clone.go $ redis-benchmark -p 6380 -t set,get -n 10000000 -q -P 512 -c 512SET: 1944390.38 requests per secondGET: 3993610.25 requests per second Running on a MacBook Pro 15" 2.8 GHz Intel Core i7 using Go 1.7 ContactJosh Baker @tidwall LicenseRedcon source code is available under the MIT License. |
请发表评论