using StackExchange.Redis; // Create a connection string to the Redis server string connectionString = "localhost:6379"; // Create a Redis client factory var redisConnectionFactory = new Lazy( () => ConnectionMultiplexer.Connect(connectionString) ); // Create a Redis client from the factory var redisClient = redisConnectionFactory.Value.GetDatabase();
// Start a Redis transaction var transaction = redisClient.CreateTransaction(); // Add some commands to the transaction transaction.StringSetAsync("key1", "value1"); transaction.StringSetAsync("key2", "value2"); // Execute the transaction transaction.Execute();
// Create a Redis subscriber var subscriber = redisConnectionFactory.Value.GetSubscriber(); // Subscribe to a Redis channel subscriber.Subscribe("my_channel", (channel, value) => { Console.WriteLine($"Received message: {value}"); }); // Publish a message to the channel redisClient.Publish("my_channel", "Hello, world!");In this example, we first create a Redis subscriber by calling `GetSubscriber()` on the Redis client factory. Then, we subscribe to a Redis channel by calling `Subscribe()` on the subscriber and providing a callback that will be called when a message is received on the channel. Finally, we publish a message to the channel by calling `Publish()` on the Redis client.