{
  "id": "ruby",
  "title": "redis-rb guide (Ruby)",
  "url": "https://redis.io/docs/latest/develop/clients/ruby/",
  "summary": "Connect your Ruby application to a Redis database",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-04-01T08:10:08-05:00",
  "children": [],
  "page_type": "content",
  "content_hash": "eb1570f4d1ac9db1432fc338b7469188a7b42dfefd2efdbce7b07a71c9beef67",
  "sections": [
    {
      "id": "install",
      "title": "Install",
      "role": "setup",
      "text": "To install `redis-rb`, run the following command:\n\n[code example]"
    },
    {
      "id": "connect-and-test",
      "title": "Connect and test",
      "role": "content",
      "text": "Connect to localhost on port 6379:\n\n#### Code Examples\n\n**Redis CLI:**\n\n[code example]\n\n**Available in:** Redis CLI, C, C# (Asynchronous), C# (Synchronous), Go, Java, Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Ruby, Rust (Asynchronous), Rust (Synchronous)\n\n**C:**\n\n[code example]\n\n**C# (Asynchronous):**\n\n[code example]\n\n**C# (Synchronous):**\n\n[code example]\n\n**Go:**\n\n[code example]\n\n**Java:**\n\n[code example]\n\n**Java (Synchronous - Jedis):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**PHP:**\n\n[code example]\n\n**Python:**\n\n[code example]\n\n**Ruby:**\n\n[code example]\n\n**Rust (Asynchronous):**\n\n[code example]\n\n**Rust (Synchronous):**\n\n[code example]\n\n\n\nStore and retrieve a simple string.\n\n#### Code Examples\n\n**Redis CLI:**\n\n[code example]\n\n**Available in:** Redis CLI, C, C# (Asynchronous), C# (Synchronous), Go, Java, Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Ruby, Rust (Asynchronous), Rust (Synchronous)\n\n**C:**\n\n[code example]\n\n**C# (Asynchronous):**\n\n[code example]\n\n**C# (Synchronous):**\n\n[code example]\n\n**Go:**\n\n[code example]\n\n**Java:**\n\n[code example]\n\n**Java (Synchronous - Jedis):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**PHP:**\n\n[code example]\n\n**Python:**\n\n[code example]\n\n**Ruby:**\n\n[code example]\n\n**Rust (Asynchronous):**\n\n[code example]\n\n**Rust (Synchronous):**\n\n[code example]\n\n\n\nStore and retrieve a dict.\n\n#### Code Examples\n\n**Redis CLI:**\n\n[code example]\n\n**Available in:** Redis CLI, C, C# (Asynchronous), C# (Synchronous), Go, Java, Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Ruby, Rust (Asynchronous), Rust (Synchronous)\n\n**C:**\n\n[code example]\n\n**C# (Asynchronous):**\n\n[code example]\n\n**C# (Synchronous):**\n\n[code example]\n\n**Go:**\n\n[code example]\n\n**Java:**\n\n[code example]\n\n**Java (Synchronous - Jedis):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**PHP:**\n\n[code example]\n\n**Python:**\n\n[code example]\n\n**Ruby:**\n\n[code example]\n\n**Rust (Asynchronous):**\n\n[code example]\n\n**Rust (Synchronous):**\n\n[code example]\n\n\n\nClose the connection when you're done.\n\n#### Code Examples\n\n**Redis CLI:**\n\n[code example]\n\n**Available in:** Redis CLI, C, C# (Asynchronous), C# (Synchronous), Go, Java, Java (Synchronous - Jedis), JavaScript (Node.js), JavaScript (Node.js), PHP, Python, Ruby, Rust (Asynchronous), Rust (Synchronous)\n\n**C:**\n\n[code example]\n\n**C# (Asynchronous):**\n\n[code example]\n\n**C# (Synchronous):**\n\n[code example]\n\n**Go:**\n\n[code example]\n\n**Java:**\n\n[code example]\n\n**Java (Synchronous - Jedis):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**JavaScript (Node.js):**\n\n[code example]\n\n**PHP:**\n\n[code example]\n\n**Python:**\n\n[code example]\n\n**Ruby:**\n\n[code example]\n\n**Rust (Asynchronous):**\n\n[code example]\n\n**Rust (Synchronous):**\n\n[code example]"
    },
    {
      "id": "more-information",
      "title": "More information",
      "role": "content",
      "text": "The\n[GitHub repository](https://github.com/redis/redis-rb) for `redis-rb` has a\nset of [examples](https://github.com/redis/redis-rb/tree/master/examples)\nand further information about using redis-rb."
    }
  ],
  "examples": [
    {
      "id": "install-ex0",
      "language": "bash",
      "code": "gem install redis",
      "section_id": "install"
    },
    {
      "id": "connect-and-test-ex0",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n\nint main() {\n    // The `redisContext` type represents the connection\n    // to the Redis server. Here, we connect to the\n    // default host and port.\n    redisContext *c = redisConnect(\"127.0.0.1\", 6379);\n\n    // Check if the context is null or if a specific\n    // error occurred.\n    if (c == NULL || c->err) {\n        if (c != NULL) {\n            printf(\"Error: %s\\n\", c->errstr);\n            // handle error\n        } else {\n            printf(\"Can't allocate redis context\\n\");\n        }\n\n        exit(1);\n    }\n\n    // Set a string key.\n    redisReply *reply = redisCommand(c, \"SET foo bar\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: OK\n    freeReplyObject(reply);\n\n    // Get the key we have just stored.\n    reply = redisCommand(c, \"GET foo\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: bar\n    freeReplyObject(reply);\n\n    // Close the connection.\n    redisFree(c);\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex1",
      "language": "csharp",
      "code": "var muxer = await ConnectionMultiplexer.ConnectAsync(\"localhost:6379\");\n        var db = muxer.GetDatabase();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex2",
      "language": "csharp",
      "code": "var muxer = ConnectionMultiplexer.Connect(\"localhost:6379\");\n        var db = muxer.GetDatabase();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex3",
      "language": "go",
      "code": "rdb := redis.NewClient(&redis.Options{\n\t\tAddr:     \"localhost:6379\",\n\t\tPassword: \"\", // no password\n\t\tDB:       0,  // use default DB\n\t\tProtocol: 2,\n\t})\n\n\tctx := context.Background()",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex4",
      "language": "java",
      "code": "RedisURI uri = RedisURI.Builder\n                .redis(\"localhost\", 6379)\n                .build();\n\n        RedisClient client = RedisClient.create(uri);\n        StatefulRedisConnection<String, String> connection = client.connect();\n        RedisCommands<String, String> commands = connection.sync();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex5",
      "language": "java",
      "code": "RedisClient jedis = new RedisClient(\"redis://localhost:6379\");",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex6",
      "language": "javascript",
      "code": "import { createClient } from 'redis';\n\nconst client = createClient();\n\nclient.on('error', err => console.log('Redis Client Error', err));\n\nawait client.connect();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex7",
      "language": "javascript",
      "code": "import { Redis } from 'ioredis';\n\nconst redis = new Redis();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex8",
      "language": "javascript",
      "code": "import { createClient } from 'redis';\n\nconst client = createClient();\n\nclient.on('error', err => console.log('Redis Client Error', err));\n\nawait client.connect();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex9",
      "language": "javascript",
      "code": "import { Redis } from 'ioredis';\n\nconst redis = new Redis();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex10",
      "language": "php",
      "code": "<?php\n\nrequire 'vendor/autoload.php';\n\nuse Predis\\Client as PredisClient;\n\n$r = new PredisClient([\n                'scheme'   => 'tcp',\n                'host'     => '127.0.0.1',\n                'port'     => 6379,\n                'password' => '',\n                'database' => 0,\n            ]);",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex11",
      "language": "python",
      "code": "import redis\nr = redis.Redis(host='localhost', port=6379, decode_responses=True)",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex12",
      "language": "ruby",
      "code": "r = Redis.new",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex13",
      "language": "rust",
      "code": "let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_multiplexed_async_connection().await {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex14",
      "language": "rust",
      "code": "let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_connection() {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex15",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n\nint main() {\n    // The `redisContext` type represents the connection\n    // to the Redis server. Here, we connect to the\n    // default host and port.\n    redisContext *c = redisConnect(\"127.0.0.1\", 6379);\n\n    // Check if the context is null or if a specific\n    // error occurred.\n    if (c == NULL || c->err) {\n        if (c != NULL) {\n            printf(\"Error: %s\\n\", c->errstr);\n            // handle error\n        } else {\n            printf(\"Can't allocate redis context\\n\");\n        }\n\n        exit(1);\n    }\n\n    // Set a string key.\n    redisReply *reply = redisCommand(c, \"SET foo bar\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: OK\n    freeReplyObject(reply);\n\n    // Get the key we have just stored.\n    reply = redisCommand(c, \"GET foo\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: bar\n    freeReplyObject(reply);\n\n    // Close the connection.\n    redisFree(c);\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex16",
      "language": "csharp",
      "code": "await db.StringSetAsync(\"foo\", \"bar\");\n        string? fooResult = await db.StringGetAsync(\"foo\");\n        Console.WriteLine(fooResult); // >>> bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex17",
      "language": "csharp",
      "code": "db.StringSet(\"foo\", \"bar\");\n        Console.WriteLine(db.StringGet(\"foo\")); // >>> bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex18",
      "language": "go",
      "code": "err := rdb.Set(ctx, \"foo\", \"bar\", 0).Err()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err := rdb.Get(ctx, \"foo\").Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"foo\", val) // >>> foo bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex19",
      "language": "java",
      "code": "commands.set(\"foo\", \"bar\");\n        String result = commands.get(\"foo\");\n        System.out.println(result); // >>> bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex20",
      "language": "java",
      "code": "String res1 = jedis.set(\"bike:1\", \"Deimos\");\n        System.out.println(res1); // >>> OK\n\n        String res2 = jedis.get(\"bike:1\");\n        System.out.println(res2); // >>> Deimos",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex21",
      "language": "javascript",
      "code": "await client.set('key', 'value');\nconst value = await client.get('key');\nconsole.log(value); // >>> value",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex22",
      "language": "javascript",
      "code": "await redis.set('key', 'value');\nconst value = await redis.get('key');\nconsole.log(value); // >>> value",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex23",
      "language": "javascript",
      "code": "await client.set('key', 'value');\nconst value = await client.get('key');\nconsole.log(value); // >>> value",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex24",
      "language": "javascript",
      "code": "await redis.set('key', 'value');\nconst value = await redis.get('key');\nconsole.log(value); // >>> value",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex25",
      "language": "php",
      "code": "echo $r->set('foo', 'bar'), PHP_EOL;\n// >>> OK\n\necho $r->get('foo'), PHP_EOL;\n// >>> bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex26",
      "language": "python",
      "code": "r.set('foo', 'bar')\n# True\nr.get('foo')\n# bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex27",
      "language": "ruby",
      "code": "r.set 'foo', 'bar'\nvalue = r.get('foo')\nputs value # >>> bar",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex28",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"foo\", \"bar\").await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n\n        match r.get(\"foo\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex29",
      "language": "rust",
      "code": "if let Ok(res) = r.set(\"foo\", \"bar\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n        \n        match r.get(\"foo\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\");   // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex30",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n\nint main() {\n    // The `redisContext` type represents the connection\n    // to the Redis server. Here, we connect to the\n    // default host and port.\n    redisContext *c = redisConnect(\"127.0.0.1\", 6379);\n\n    // Check if the context is null or if a specific\n    // error occurred.\n    if (c == NULL || c->err) {\n        if (c != NULL) {\n            printf(\"Error: %s\\n\", c->errstr);\n            // handle error\n        } else {\n            printf(\"Can't allocate redis context\\n\");\n        }\n\n        exit(1);\n    }\n\n    // Set a string key.\n    redisReply *reply = redisCommand(c, \"SET foo bar\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: OK\n    freeReplyObject(reply);\n\n    // Get the key we have just stored.\n    reply = redisCommand(c, \"GET foo\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: bar\n    freeReplyObject(reply);\n\n    // Close the connection.\n    redisFree(c);\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex31",
      "language": "csharp",
      "code": "using NRedisStack;\nusing NRedisStack.RedisStackCommands;\nusing StackExchange.Redis;\n\npublic class AsyncLandingExample\n{\n    public async Task Run()\n    {\n        var muxer = await ConnectionMultiplexer.ConnectAsync(\"localhost:6379\");\n        var db = muxer.GetDatabase();\n\n        await db.StringSetAsync(\"foo\", \"bar\");\n        string? fooResult = await db.StringGetAsync(\"foo\");\n        Console.WriteLine(fooResult); // >>> bar\n\n        var hash = new HashEntry[] { \n            new HashEntry(\"name\", \"John\"), \n            new HashEntry(\"surname\", \"Smith\"),\n            new HashEntry(\"company\", \"Redis\"),\n            new HashEntry(\"age\", \"29\"),\n            };\n        await db.HashSetAsync(\"user-session:123\", hash);\n\n        var hashFields = await db.HashGetAllAsync(\"user-session:123\");\n        Console.WriteLine(String.Join(\"; \", hashFields));\n        // >>> name: John; surname: Smith; company: Redis; age: 29\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex32",
      "language": "csharp",
      "code": "using NRedisStack;\nusing NRedisStack.RedisStackCommands;\nusing StackExchange.Redis;\n\npublic class SyncLandingExample\n{\n    public void Run()\n    {\n        var muxer = ConnectionMultiplexer.Connect(\"localhost:6379\");\n        var db = muxer.GetDatabase();\n\n        db.StringSet(\"foo\", \"bar\");\n        Console.WriteLine(db.StringGet(\"foo\")); // >>> bar\n\n        var hash = new HashEntry[] {\n            new HashEntry(\"name\", \"John\"),\n            new HashEntry(\"surname\", \"Smith\"),\n            new HashEntry(\"company\", \"Redis\"),\n            new HashEntry(\"age\", \"29\"),\n        };\n        db.HashSet(\"user-session:123\", hash);\n\n        var hashFields = db.HashGetAll(\"user-session:123\");\n        Console.WriteLine(String.Join(\"; \", hashFields));\n        // >>> name: John; surname: Smith; company: Redis; age: 29\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex33",
      "language": "go",
      "code": "package main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\t\"github.com/redis/go-redis/v9\"\n)\n\n\nfunc main() {\n\trdb := redis.NewClient(&redis.Options{\n\t\tAddr:     \"localhost:6379\",\n\t\tPassword: \"\", // no password\n\t\tDB:       0,  // use default DB\n\t\tProtocol: 2,\n\t})\n\n\tctx := context.Background()\n\n\terr := rdb.Set(ctx, \"foo\", \"bar\", 0).Err()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tval, err := rdb.Get(ctx, \"foo\").Result()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Println(\"foo\", val) // >>> foo bar\n\n\thashFields := []string{\n\t\t\"model\", \"Deimos\",\n\t\t\"brand\", \"Ergonom\",\n\t\t\"type\", \"Enduro bikes\",\n\t\t\"price\", \"4972\",\n\t}\n\n\tres1, err := rdb.HSet(ctx, \"bike:1\", hashFields).Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res1) // >>> 4\n\n\tres2, err := rdb.HGet(ctx, \"bike:1\", \"model\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res2) // >>> Deimos\n\n\tres3, err := rdb.HGet(ctx, \"bike:1\", \"price\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res3) // >>> 4972\n\n\tres4, err := rdb.HGetAll(ctx, \"bike:1\").Result()\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(res4)\n\t// >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]\n\n\ttype BikeInfo struct {\n\t\tModel string `redis:\"model\"`\n\t\tBrand string `redis:\"brand\"`\n\t\tType  string `redis:\"type\"`\n\t\tPrice int    `redis:\"price\"`\n\t}\n\n\tvar res4a BikeInfo\n\terr = rdb.HGetAll(ctx, \"bike:1\").Scan(&res4a)\n\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Printf(\"Model: %v, Brand: %v, Type: %v, Price: $%v\\n\",\n\t\tres4a.Model, res4a.Brand, res4a.Type, res4a.Price)\n\t// >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972\n\n\trdb.Close()\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex34",
      "language": "java",
      "code": "import io.lettuce.core.*;\nimport io.lettuce.core.api.StatefulRedisConnection;\nimport io.lettuce.core.api.sync.RedisCommands;\n\n\npublic class ConnectBasicTest {\n\n    public void connectBasic() {\n        RedisURI uri = RedisURI.Builder\n                .redis(\"localhost\", 6379)\n                .build();\n\n        RedisClient client = RedisClient.create(uri);\n        StatefulRedisConnection<String, String> connection = client.connect();\n        RedisCommands<String, String> commands = connection.sync();\n\n        commands.set(\"foo\", \"bar\");\n        String result = commands.get(\"foo\");\n        System.out.println(result); // >>> bar\n\n        connection.close();\n        client.shutdown();\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex35",
      "language": "java",
      "code": "import redis.clients.jedis.RedisClient;\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class LandingExample {\n\n    public void run() {\n        RedisClient jedis = new RedisClient(\"redis://localhost:6379\");\n\n        String res1 = jedis.set(\"bike:1\", \"Deimos\");\n        System.out.println(res1); // >>> OK\n\n        String res2 = jedis.get(\"bike:1\");\n        System.out.println(res2); // >>> Deimos\n\n        Map<String, String> hash = new HashMap<>();\n        hash.put(\"name\", \"John\");\n        hash.put(\"surname\", \"Smith\");\n        hash.put(\"company\", \"Redis\");\n        hash.put(\"age\", \"29\");\n\n        Long res3 = jedis.hset(\"user-session:123\", hash);\n        System.out.println(res3); // >>> 4\n\n        Map<String, String> res4 = jedis.hgetAll(\"user-session:123\");\n        System.out.println(res4);\n        // >>> {name=John, surname=Smith, company=Redis, age=29}\n\n        jedis.close();\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex36",
      "language": "javascript",
      "code": "import { createClient } from 'redis';\n\nconst client = createClient();\n\nclient.on('error', err => console.log('Redis Client Error', err));\n\nawait client.connect();\n\nawait client.set('key', 'value');\nconst value = await client.get('key');\nconsole.log(value); // >>> value\n\nawait client.hSet('user-session:123', {\n    name: 'John',\n    surname: 'Smith',\n    company: 'Redis',\n    age: 29\n})\n\nlet userSession = await client.hGetAll('user-session:123');\nconsole.log(JSON.stringify(userSession, null, 2));\n/* >>>\n{\n  \"surname\": \"Smith\",\n  \"name\": \"John\",\n  \"company\": \"Redis\",\n  \"age\": \"29\"\n}\n */\n\nawait client.quit();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex37",
      "language": "javascript",
      "code": "import { Redis } from 'ioredis';\n\nconst redis = new Redis();\n\nawait redis.set('key', 'value');\nconst value = await redis.get('key');\nconsole.log(value); // >>> value\n\nawait redis.hset('user-session:123', {\n    name: 'John',\n    surname: 'Smith',\n    company: 'Redis',\n    age: 29\n});\n\nconst userSession = await redis.hgetall('user-session:123');\nconsole.log(JSON.stringify(userSession, null, 2));\n/* >>>\n{\n  \"surname\": \"Smith\",\n  \"name\": \"John\",\n  \"company\": \"Redis\",\n  \"age\": \"29\"\n}\n */\n\nredis.disconnect();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex38",
      "language": "javascript",
      "code": "import { createClient } from 'redis';\n\nconst client = createClient();\n\nclient.on('error', err => console.log('Redis Client Error', err));\n\nawait client.connect();\n\nawait client.set('key', 'value');\nconst value = await client.get('key');\nconsole.log(value); // >>> value\n\nawait client.hSet('user-session:123', {\n    name: 'John',\n    surname: 'Smith',\n    company: 'Redis',\n    age: 29\n})\n\nlet userSession = await client.hGetAll('user-session:123');\nconsole.log(JSON.stringify(userSession, null, 2));\n/* >>>\n{\n  \"surname\": \"Smith\",\n  \"name\": \"John\",\n  \"company\": \"Redis\",\n  \"age\": \"29\"\n}\n */\n\nawait client.quit();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex39",
      "language": "javascript",
      "code": "import { Redis } from 'ioredis';\n\nconst redis = new Redis();\n\nawait redis.set('key', 'value');\nconst value = await redis.get('key');\nconsole.log(value); // >>> value\n\nawait redis.hset('user-session:123', {\n    name: 'John',\n    surname: 'Smith',\n    company: 'Redis',\n    age: 29\n});\n\nconst userSession = await redis.hgetall('user-session:123');\nconsole.log(JSON.stringify(userSession, null, 2));\n/* >>>\n{\n  \"surname\": \"Smith\",\n  \"name\": \"John\",\n  \"company\": \"Redis\",\n  \"age\": \"29\"\n}\n */\n\nredis.disconnect();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex40",
      "language": "php",
      "code": "<?php\n\nrequire 'vendor/autoload.php';\n\nuse Predis\\Client as PredisClient;\n\n$r = new PredisClient([\n                'scheme'   => 'tcp',\n                'host'     => '127.0.0.1',\n                'port'     => 6379,\n                'password' => '',\n                'database' => 0,\n            ]);\n\necho $r->set('foo', 'bar'), PHP_EOL;\n// >>> OK\n\necho $r->get('foo'), PHP_EOL;\n// >>> bar\n\n$r->hset('user-session:123', 'name', 'John');\n$r->hset('user-session:123', 'surname', 'Smith');\n$r->hset('user-session:123', 'company', 'Redis');\n$r->hset('user-session:123', 'age', 29);\n\necho var_export($r->hgetall('user-session:123')), PHP_EOL;\n/* >>>\narray (\n  'name' => 'John',\n  'surname' => 'Smith',\n  'company' => 'Redis',\n  'age' => '29',\n)\n*/",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex41",
      "language": "python",
      "code": "r.hset('user-session:123', mapping={\n    'name': 'John',\n    \"surname\": 'Smith',\n    \"company\": 'Redis',\n    \"age\": 29\n})\n# True\n\nr.hgetall('user-session:123')\n# {'surname': 'Smith', 'name': 'John', 'company': 'Redis', 'age': '29'}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex42",
      "language": "ruby",
      "code": "r.hset 'user-session:123', 'name', 'John'\nr.hset 'user-session:123', 'surname', 'Smith'\nr.hset 'user-session:123', 'company', 'Redis'\nr.hset 'user-session:123', 'age', 29\n\nhash_value = r.hgetall('user-session:123')\nputs hash_value\n# >>> {\"name\"=>\"John\", \"surname\"=>\"Smith\", \"company\"=>\"Redis\", \"age\"=>\"29\"}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex43",
      "language": "rust",
      "code": "mod tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_multiplexed_async_connection().await {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        if let Ok(res) = r.set(\"foo\", \"bar\").await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n\n        match r.get(\"foo\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };\n\n        let hash_fields = [\n            (\"model\", \"Deimos\"),\n            (\"brand\", \"Ergonom\"),\n            (\"type\", \"Enduro bikes\"),\n            (\"price\", \"4972\"),\n        ];\n\n        if let Ok(res) = r.hset_multiple(\"bike:1\", &hash_fields).await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting bike:1\");\n        }\n\n        match r.hget(\"bike:1\", \"model\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> Deimos\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 model: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"bike:1\", \"price\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 price: {e}\");\n                return;\n            }\n        }\n\n        match r.hgetall(\"bike:1\").await {\n            Ok(res) => {\n                let res: Vec<(String, String)> = res;\n\n                for (key, value) in res {\n                    println!(\"{key}: {value}\");\n                }\n                // >>> model: Deimos\n                // >>> brand: Ergonom\n                // >>> type: Enduro bikes\n                // >>> price: 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1: {e}\");\n                return;\n            }\n        }\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex44",
      "language": "rust",
      "code": "mod landing_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_connection() {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        if let Ok(res) = r.set(\"foo\", \"bar\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n        \n        match r.get(\"foo\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\");   // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };\n    \n        let hash_fields = [\n            (\"model\", \"Deimos\"),\n            (\"brand\", \"Ergonom\"),\n            (\"type\", \"Enduro bikes\"),\n            (\"price\", \"4972\"),\n        ];\n    \n        if let Ok(res) = r.hset_multiple(\"bike:1\", &hash_fields) {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting bike:1\");\n        }\n    \n        match r.hget(\"bike:1\", \"model\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> Deimos\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 model: {e}\");\n                return;\n            }   \n        }\n    \n        match r.hget(\"bike:1\", \"price\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 price: {e}\");\n                return;\n            }   \n        }\n    \n        match r.hgetall(\"bike:1\") {\n            Ok(res) => {\n                let res: Vec<(String, String)> = res;\n                \n                for (key, value) in res {\n                    println!(\"{key}: {value}\");\n                }\n                // >>> model: Deimos\n                // >>> brand: Ergonom\n                // >>> type: Enduro bikes\n                // >>> price: 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1: {e}\");\n                return;\n            }   \n        }\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex45",
      "language": "c",
      "code": "#include <stdio.h>\n#include <stdlib.h>\n\n#include <hiredis/hiredis.h>\n\nint main() {\n    // The `redisContext` type represents the connection\n    // to the Redis server. Here, we connect to the\n    // default host and port.\n    redisContext *c = redisConnect(\"127.0.0.1\", 6379);\n\n    // Check if the context is null or if a specific\n    // error occurred.\n    if (c == NULL || c->err) {\n        if (c != NULL) {\n            printf(\"Error: %s\\n\", c->errstr);\n            // handle error\n        } else {\n            printf(\"Can't allocate redis context\\n\");\n        }\n\n        exit(1);\n    }\n\n    // Set a string key.\n    redisReply *reply = redisCommand(c, \"SET foo bar\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: OK\n    freeReplyObject(reply);\n\n    // Get the key we have just stored.\n    reply = redisCommand(c, \"GET foo\");\n    printf(\"Reply: %s\\n\", reply->str); // >>> Reply: bar\n    freeReplyObject(reply);\n\n    // Close the connection.\n    redisFree(c);\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex46",
      "language": "csharp",
      "code": "using NRedisStack;\nusing NRedisStack.RedisStackCommands;\nusing StackExchange.Redis;\n\npublic class AsyncLandingExample\n{\n    public async Task Run()\n    {\n        var muxer = await ConnectionMultiplexer.ConnectAsync(\"localhost:6379\");\n        var db = muxer.GetDatabase();\n\n        await db.StringSetAsync(\"foo\", \"bar\");\n        string? fooResult = await db.StringGetAsync(\"foo\");\n        Console.WriteLine(fooResult); // >>> bar\n\n        var hash = new HashEntry[] { \n            new HashEntry(\"name\", \"John\"), \n            new HashEntry(\"surname\", \"Smith\"),\n            new HashEntry(\"company\", \"Redis\"),\n            new HashEntry(\"age\", \"29\"),\n            };\n        await db.HashSetAsync(\"user-session:123\", hash);\n\n        var hashFields = await db.HashGetAllAsync(\"user-session:123\");\n        Console.WriteLine(String.Join(\"; \", hashFields));\n        // >>> name: John; surname: Smith; company: Redis; age: 29\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex47",
      "language": "csharp",
      "code": "using NRedisStack;\nusing NRedisStack.RedisStackCommands;\nusing StackExchange.Redis;\n\npublic class SyncLandingExample\n{\n    public void Run()\n    {\n        var muxer = ConnectionMultiplexer.Connect(\"localhost:6379\");\n        var db = muxer.GetDatabase();\n\n        db.StringSet(\"foo\", \"bar\");\n        Console.WriteLine(db.StringGet(\"foo\")); // >>> bar\n\n        var hash = new HashEntry[] {\n            new HashEntry(\"name\", \"John\"),\n            new HashEntry(\"surname\", \"Smith\"),\n            new HashEntry(\"company\", \"Redis\"),\n            new HashEntry(\"age\", \"29\"),\n        };\n        db.HashSet(\"user-session:123\", hash);\n\n        var hashFields = db.HashGetAll(\"user-session:123\");\n        Console.WriteLine(String.Join(\"; \", hashFields));\n        // >>> name: John; surname: Smith; company: Redis; age: 29\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex48",
      "language": "go",
      "code": "rdb.Close()",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex49",
      "language": "java",
      "code": "connection.close();\n        client.shutdown();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex50",
      "language": "java",
      "code": "jedis.close();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex51",
      "language": "javascript",
      "code": "await client.quit();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex52",
      "language": "javascript",
      "code": "redis.disconnect();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex53",
      "language": "javascript",
      "code": "await client.quit();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex54",
      "language": "javascript",
      "code": "redis.disconnect();",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex55",
      "language": "php",
      "code": "<?php\n\nrequire 'vendor/autoload.php';\n\nuse Predis\\Client as PredisClient;\n\n$r = new PredisClient([\n                'scheme'   => 'tcp',\n                'host'     => '127.0.0.1',\n                'port'     => 6379,\n                'password' => '',\n                'database' => 0,\n            ]);\n\necho $r->set('foo', 'bar'), PHP_EOL;\n// >>> OK\n\necho $r->get('foo'), PHP_EOL;\n// >>> bar\n\n$r->hset('user-session:123', 'name', 'John');\n$r->hset('user-session:123', 'surname', 'Smith');\n$r->hset('user-session:123', 'company', 'Redis');\n$r->hset('user-session:123', 'age', 29);\n\necho var_export($r->hgetall('user-session:123')), PHP_EOL;\n/* >>>\narray (\n  'name' => 'John',\n  'surname' => 'Smith',\n  'company' => 'Redis',\n  'age' => '29',\n)\n*/",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex56",
      "language": "python",
      "code": "r.close()",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex57",
      "language": "ruby",
      "code": "r.close()",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex58",
      "language": "rust",
      "code": "mod tests {\n    use redis::AsyncCommands;\n\n    async fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_multiplexed_async_connection().await {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        if let Ok(res) = r.set(\"foo\", \"bar\").await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n\n        match r.get(\"foo\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };\n\n        let hash_fields = [\n            (\"model\", \"Deimos\"),\n            (\"brand\", \"Ergonom\"),\n            (\"type\", \"Enduro bikes\"),\n            (\"price\", \"4972\"),\n        ];\n\n        if let Ok(res) = r.hset_multiple(\"bike:1\", &hash_fields).await {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting bike:1\");\n        }\n\n        match r.hget(\"bike:1\", \"model\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> Deimos\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 model: {e}\");\n                return;\n            }\n        }\n\n        match r.hget(\"bike:1\", \"price\").await {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 price: {e}\");\n                return;\n            }\n        }\n\n        match r.hgetall(\"bike:1\").await {\n            Ok(res) => {\n                let res: Vec<(String, String)> = res;\n\n                for (key, value) in res {\n                    println!(\"{key}: {value}\");\n                }\n                // >>> model: Deimos\n                // >>> brand: Ergonom\n                // >>> type: Enduro bikes\n                // >>> price: 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1: {e}\");\n                return;\n            }\n        }\n    }\n}",
      "section_id": "connect-and-test"
    },
    {
      "id": "connect-and-test-ex59",
      "language": "rust",
      "code": "mod landing_tests {\n    use redis::Commands;\n\n    fn run() {\n        let mut r = match redis::Client::open(\"redis://127.0.0.1\") {\n            Ok(client) => {\n                match client.get_connection() {\n                    Ok(conn) => conn,\n                    Err(e) => {\n                        println!(\"Failed to connect to Redis: {e}\");\n                        return;\n                    }\n                }\n            },\n            Err(e) => {\n                println!(\"Failed to create Redis client: {e}\");\n                return;\n            }\n        };\n\n        if let Ok(res) = r.set(\"foo\", \"bar\") {\n            let res: String = res;\n            println!(\"{res}\");    // >>> OK\n        } else {\n            println!(\"Error setting foo\");\n        }\n        \n        match r.get(\"foo\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\");   // >>> bar\n            },\n            Err(e) => {\n                println!(\"Error getting foo: {e}\");\n                return;\n            }\n        };\n    \n        let hash_fields = [\n            (\"model\", \"Deimos\"),\n            (\"brand\", \"Ergonom\"),\n            (\"type\", \"Enduro bikes\"),\n            (\"price\", \"4972\"),\n        ];\n    \n        if let Ok(res) = r.hset_multiple(\"bike:1\", &hash_fields) {\n            let res: String = res;\n            println!(\"{res}\"); // >>> OK\n        } else {\n            println!(\"Error setting bike:1\");\n        }\n    \n        match r.hget(\"bike:1\", \"model\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> Deimos\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 model: {e}\");\n                return;\n            }   \n        }\n    \n        match r.hget(\"bike:1\", \"price\") {\n            Ok(res) => {\n                let res: String = res;\n                println!(\"{res}\"); // >>> 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1 price: {e}\");\n                return;\n            }   \n        }\n    \n        match r.hgetall(\"bike:1\") {\n            Ok(res) => {\n                let res: Vec<(String, String)> = res;\n                \n                for (key, value) in res {\n                    println!(\"{key}: {value}\");\n                }\n                // >>> model: Deimos\n                // >>> brand: Ergonom\n                // >>> type: Enduro bikes\n                // >>> price: 4972\n            },\n            Err(e) => {\n                println!(\"Error getting bike:1: {e}\");\n                return;\n            }   \n        }\n    }\n}",
      "section_id": "connect-and-test"
    }
  ]
}
