Using languages and drivers (Simple)

All the major programming languages have support for Redis through their client libraries. Even if your language of preference does not have a client library to connect and use Redis, it is fairly easy to write a basic client of your own, as the Redis protocol is simple to implement. So, in this recipe, we will cover some clients worth mentioning for commonly used programming languages.

Tip

It is important to update the client libraries when we update the Redis server to get support for new commands and features.

Let us see how to use client libraries to connect to Redis. As part of this recipe, we will see how we can use redis-rb to connect to Redis using Ruby.

How to do it...

  1. You need to download the redis-rb client library from the official GitHub page (https://github.com/redis/redis-rb).
  2. Create a new Ruby script that will connect to the Redis server. The following code should make a connection to the server, assuming the server is running on the local machine at port 6379:
    require "redis"
    redis = Redis.new(:host => "127.0.0.1", :port => 6379)
    
  3. After connecting, we can execute the following commands:
    redis.set("redis", "rocks")
    redis.get('redis')
    
  4. Close the connection once done.
    redis.quit
    

There's more…

You can find multiple libraries that provide almost similar functionalities; the complete list of libraries available for Redis can be found on the official Redis website (http://redis.io/clients). Here we were focusing on stable, active, recommended, and feature-complete libraries only. An official supported library is available only for C; all the libraries for other languages were written and are maintained by the community or external developers.

Client Libraries

The following table has the recommended libraries for popular programming languages (which are stable enough to be used in production):

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.21.47