Use Redis with the Command Line
In this example, the Redis Command Line Interface (Redis-cli) will be used to show off some of the possible actions that can be performed with Redis.
Prerequisites
The first step is to install the Redis-cli from the official website. Then load up a command line tool and run the following command,
If you have password authentication enabled:
1 |
redis-cli -c -u redis://icredis:<password>@<cluster node IP>:6379 --no-auth-warning |
If you have disabled password authentication:
1 |
redis-cli -c -u redis://icredis:@<cluster node IP>:6379 --no-auth-warning |
Where <cluster node IP> is an IP of any of the master nodes in the cluster.
Where <password> is the password for the user you are connecting as. By default instaclustr Redis instances will be created with the user icredis.
Examples
Setting and Getting keys.
1 2 3 4 |
> set foo bar OK > get foo "bar" |
Incrementing a key.
1 2 3 4 5 6 7 8 |
> set counter 10 OK > incr counter (integer) 11 > incr counter (integer) 12 >incrby counter 10 (integer) 22 |
Setting and Getting multiple keys.
1 2 3 4 5 |
> mset foo bar insta clustr OK > mget foo insta 1) “bar” 2) “clustr” |
Deleting a key (returns 1 (true) if deleted).
1 2 3 4 5 6 |
> set foo bar OK > del foo (integer) 1 > get foo (nil) |
Checking if a key exists (returns 1 if true, 0 if false).
1 2 3 4 5 6 7 8 |
> set foo bar OK > exists foo (integer) 1 > del foo (integer) 1 > exists foo (integer) 0 |
Expiring a key (using 5 seconds) and checking expiration (ttl) returning seconds left before expiration.
1 2 3 4 5 6 7 8 9 10 |
> set foo bar OK > expire foo 5 (integer) 1 > get foo (immediately) "Bar" > ttl foo (immediately) (integer) 4 > get foo (after some time) (nil) |
Troubleshooting
Sometimes after authenticating through the Redis-cli, it is possible that running commands still require authentication. This is due to the Redis-cli switching to a different node to run commands, and therefore requires authentication through that node as well. To fix this issue, simply provide the authentication again.
1 |
> AUTH <cluster password> |
Further Reading
The examples above only give basic usage cases for the redis-cli. For other languages or more advanced usages (such as using lists, hashes, sets etc) please see the official redis documentation.