db/redis
The db/redis module is a small Redis client built over net/tcp.
Import
Section titled “Import”import "db/redis";type Client struct { .conn: net::TcpConn};Client owns a TCP connection resource.
- Non-copyable
- Value passing/assignment moves ownership
Close()uses a value receiver and consumes the client
fn Connect(addr: str) -> str ! Clientfn (c: Client) Close()
fn (c: &mut Client) CommandArgs(args: []str) -> str ! strfn (c: &mut Client) CommandStr(args: ...str) -> str ! strfn (c: &mut Client) CommandInt(args: ...str) -> str ! i64
fn (c: &mut Client) Ping() -> str ! strfn (c: &mut Client) Get(key: str) -> str ! ?strfn (c: &mut Client) Set(key: str, value: str) -> str ! boolfn (c: &mut Client) Del(key: str) -> str ! i64fn (c: &mut Client) Incr(key: str) -> str ! i64fn (c: &mut Client) IncrBy(key: str, delta: i64) -> str ! i64Example
Section titled “Example”import "db/redis";import "std/io";
fn main() { let client := redis::Connect("127.0.0.1:6379") catch err { io::Println("connect error:", err); return; }; defer client.Close();
let ok := client.Set("counter", "10") catch err { io::Println("set error:", err); return; };
if ok { let value := client.Get("counter") catch err { io::Println("get error:", err); return; }; io::Println("counter:", value ?? "<none>"); }
let next := client.Incr("counter") catch err { io::Println("incr error:", err); return; }; io::Println("counter after incr:", next);}Ownership Example
Section titled “Ownership Example”let c1 := redis::Connect("127.0.0.1:6379") catch err { return; };let c2 := c1; // ownership moved to c2// c1.Close(); // ❌ use of moved valuec2.Close();