Redis 中文文档 Redis 中文文档
指南
redis.io (opens new window)
指南
redis.io (opens new window)
  • 关于
    • Redis 开源治理
    • Redis 发布周期
    • Redis 赞助商
  • 入门
  • 数据类型
  • Redis Stack
  • 命令
  • 手册

Redeo


The high-performance Swiss Army Knife for building redis-protocol compatible servers/services.

Parts


This repository is organised into multiple components:

root package contains the framework for building redis-protocol compatible, high-performance servers.
resp implements low-level primitives for dealing with RESP (REdis Serialization Protocol), client and server-side. It contains basic wrappers for readers and writers to read/write requests and responses.
client contains a minimalist pooled client.

For full documentation and examples, please see the individual packages and the official API documentation: https://godoc.org/github.com/bsm/redeo.

Examples


A simple server example with two commands:

  1. ``` go
  2. package main

  3. import (
  4.   "net"

  5.   "github.com/bsm/redeo/v2"
  6. )

  7. func main() {
  8. srv := redeo.NewServer(nil)

  9. // Define handlers
  10. srv.HandleFunc("ping", func(w resp.ResponseWriter, _ *resp.Command) {
  11.   w.AppendInlineString("PONG")
  12. })
  13. srv.HandleFunc("info", func(w resp.ResponseWriter, _ *resp.Command) {
  14.   w.AppendBulkString(srv.Info().String())
  15. })

  16. // More handlers; demo usage of redeo.WrapperFunc
  17. srv.Handle("echo", redeo.WrapperFunc(func(c *resp.Command) interface{} {
  18.   if c.ArgN() != 1 {
  19.    return redeo.ErrWrongNumberOfArgs(c.Name)
  20.   }
  21.   return c.Arg(0)
  22. }))

  23. // Open a new listener
  24. lis, err := net.Listen("tcp", ":9736")
  25. if err != nil {
  26.   panic(err)
  27. }
  28. defer lis.Close()

  29. // Start serving (blocking)
  30. srv.Serve(lis)
  31. }
  32. ```

More complex handlers:

  1. ``` go
  2. func main() {
  3. mu := sync.RWMutex{}
  4. data := make(map[string]string)
  5. srv := redeo.NewServer(nil)

  6. srv.HandleFunc("set", func(w resp.ResponseWriter, c *resp.Command) {
  7.   if c.ArgN() != 2 {
  8.    w.AppendError(redeo.WrongNumberOfArgs(c.Name))
  9.    return
  10.   }

  11.   key := c.Arg(0).String()
  12.   val := c.Arg(1).String()

  13.   mu.Lock()
  14.   data[key] = val
  15.   mu.Unlock()

  16.   w.AppendInt(1)
  17. })

  18. srv.HandleFunc("get", func(w resp.ResponseWriter, c *resp.Command) {
  19.   if c.ArgN() != 1 {
  20.    w.AppendError(redeo.WrongNumberOfArgs(c.Name))
  21.    return
  22.   }

  23.   key := c.Arg(0).String()
  24.   mu.RLock()
  25.   val, ok := data[key]
  26.   mu.RUnlock()

  27.   if ok {
  28.    w.AppendBulkString(val)
  29.    return
  30.   }
  31.   w.AppendNil()
  32. })
  33. }
  34. ```

Redeo also supports command wrappers:

  1. ``` go
  2. func main() {
  3. mu := sync.RWMutex{}
  4. data := make(map[string]string)
  5. srv := redeo.NewServer(nil)

  6. srv.Handle("set", redeo.WrapperFunc(func(c *resp.Command) interface{} {
  7.   if c.ArgN() != 2 {
  8.    return redeo.ErrWrongNumberOfArgs(c.Name)
  9.   }

  10.   key := c.Arg(0).String()
  11.   val := c.Arg(1).String()

  12.   mu.Lock()
  13.   data[key] = val
  14.   mu.Unlock()

  15.   return 1
  16. }))

  17. srv.Handle("get", redeo.WrapperFunc(func(c *resp.Command) interface{} {
  18.   if c.ArgN() != 1 {
  19.    return redeo.ErrWrongNumberOfArgs(c.Name)
  20.   }

  21.   key := c.Arg(0).String()
  22.   mu.RLock()
  23.   val, ok := data[key]
  24.   mu.RUnlock()

  25.   if ok {
  26.    return val
  27.   }
  28.   return nil
  29. }))
  30. }
  31. ```
Last Updated: 2023-09-03 19:17:54