0
0
Azurecloud~5 mins

Redis connection and basic commands in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Redis is a fast database that stores data in memory. It helps apps remember information quickly. You connect to Redis and use simple commands to save and get data.
When you want to store user session data for a web app to keep users logged in.
When you need to count how many times something happens, like page views.
When you want to cache data to make your app faster by avoiding slow database calls.
When you want to share data between different parts of your app quickly.
When you need a simple message queue to pass messages between services.
Commands
Connect to the Azure Redis Cache instance securely using TLS with the hostname, port, and password.
Terminal
redis-cli -h redis-example.redis.cache.windows.net -p 6380 -a MyStrongPassword --tls
Expected OutputExpected
redis-example.redis.cache.windows.net:6380>
-h - Specifies the Redis server hostname.
-p - Specifies the port number to connect to.
--tls - Enables secure TLS connection.
Store the text "Hello, Redis!" under the key named 'greeting'.
Terminal
SET greeting "Hello, Redis!"
Expected OutputExpected
OK
Retrieve the value stored under the key 'greeting'.
Terminal
GET greeting
Expected OutputExpected
Hello, Redis!
Increase the number stored under 'page_views' by 1. If it doesn't exist, Redis starts it at 0 then adds 1.
Terminal
INCR page_views
Expected OutputExpected
1
Set the key 'greeting' to expire and be deleted after 60 seconds.
Terminal
EXPIRE greeting 60
Expected OutputExpected
1
Key Concept

If you remember nothing else, remember: Redis stores data as keys and values that you can set, get, and update instantly.

Common Mistakes
Trying to connect without the --tls flag to Azure Redis Cache.
Azure Redis requires secure TLS connections on port 6380, so the connection will fail without it.
Always include --tls when connecting to Azure Redis on port 6380.
Using GET on a key that does not exist.
Redis returns (nil) which might be confusing if not handled properly in your app.
Check if the returned value is nil before using it.
Using INCR on a key that holds a non-integer value.
Redis will return an error because it cannot increment non-numeric values.
Ensure the key is either not set or holds an integer before using INCR.
Summary
Use redis-cli with hostname, port, password, and --tls to connect securely to Azure Redis Cache.
Use SET to save data and GET to retrieve it by key.
Use INCR to count things and EXPIRE to set automatic deletion times.