Basic Redis Commands
Redis provides a rich set of commands for interacting with its key-value data store. These commands allow you to perform various operations such as storing and retrieving data, manipulating data structures, and executing server-side operations. In this section, we will explore some of the basic Redis commands for key-value storage and retrieval.
SET and GET
The SET command is used to set the value of a key in Redis. For example:
1var redis = ConnectionMultiplexer.Connect("localhost:6379");
2var db = redis.GetDatabase();
3db.StringSet("key", "value");The GET command is used to retrieve the value of a key in Redis. For example:
1var value = db.StringGet("key");
2Console.WriteLine(value);DEL
The DEL command is used to delete a key and its associated value from Redis. For example:
1db.KeyDelete("key");EXISTS
The EXISTS command is used to check if a key exists in Redis. It returns 1 if the key exists, and 0 if the key does not exist. For example:
1var exists = db.KeyExists("key");
2Console.WriteLine(exists);EXPIRE
The EXPIRE command is used to set a timeout on a key. After the specified number of seconds, the key will be automatically deleted. For example:
1db.KeyExpire("key", TimeSpan.FromSeconds(60));These are just a few examples of the basic Redis commands available. Redis provides many other commands for working with different data types, manipulating data structures, and executing server-side operations. It is important to understand these basic commands as they form the foundation for interacting with Redis in your .NET applications.
Keep in mind that Redis supports other features as well, such as transactions, pub/sub messaging, and pipelining. These features allow you to perform more advanced operations and build complex applications using Redis.
Take some time to experiment with these basic Redis commands in your .NET application and explore the different possibilities that Redis offers.
xxxxxxxxxxvar redis = ConnectionMultiplexer.Connect("localhost:6379");var db = redis.GetDatabase();db.StringSet("key", "value");var value = db.StringGet("key");Console.WriteLine(value);db.KeyDelete("key");var exists = db.KeyExists("key");Console.WriteLine(exists);db.KeyExpire("key", TimeSpan.FromSeconds(60));


