Soru

Zorluk: OrtaImplement Azure Cache for Redis Configuration and Data Patterns

A developer is writing a C# application that connects to an Azure Cache for Redis instance using the StackExchange.Redis SDK. To optimize application performance and socket reuse, the application must share a single, thread-safe connection instance using lazy initialization. You need to arrange the steps required to initialize the cache connection client and execute a write operation. What is the correct sequence of steps to configure, establish, and use the connection?

  1. 1Declare a private static Lazy<ConnectionMultiplexer> variable at the class level.
  2. 2Initialize the Lazy<ConnectionMultiplexer> variable with a delegate that calls ConnectionMultiplexer.Connect using a connection string.
  3. 3Access the Value property of the Lazy<ConnectionMultiplexer> variable to retrieve the active ConnectionMultiplexer instance.
  4. 4Call the GetDatabase method on the ConnectionMultiplexer instance to get a reference to the cache database.
  5. 5Call the StringSetAsync method on the database reference to write the data to the cache.

Cevap

The correct sequence starts with declaring the lazy ConnectionMultiplexer variable, followed by initializing the variable with the connection delegate. Next, access the Value property to establish the connection, call GetDatabase to obtain a reference to the cache database, and finally execute the write command using StringSetAsync.
According to Azure Cache for Redis developer guidelines, applications should share and reuse a single ConnectionMultiplexer instance. Using Lazy<ConnectionMultiplexer> ensures the connection is only established when the application first requires it, and in a thread-safe manner. Once the Value property is accessed and the connection is active, the database reference is fetched via GetDatabase, and commands are sent through the database reference.

Adım Adım Çözüm

1
Declare the static Lazy<ConnectionMultiplexer> variable.
A class-level variable is ready to hold the lazy initializer.
Creating a static variable ensures the ConnectionMultiplexer is shared and reused across the application to prevent socket exhaustion.
2
Initialize the Lazy instance with a connection delegate.
The initialization delegate is registered with connection settings.
This sets up the connection logic without immediately paying the performance cost of establishing the network connection.
3
Access the Value property of the Lazy instance.
The connection delegate is executed, returning the active ConnectionMultiplexer.
Accessing the Value property triggers the connection build thread-safely upon the first request.
4
Retrieve the database reference.
An IDatabase object is returned from the multiplexer.
The application needs a database context to interact with keys and values in the Redis instance.
5
Call StringSetAsync to write data.
The key-value pair is saved in the Azure Cache for Redis.
Commands must be executed against the obtained database reference rather than the multiplexer itself.

Anahtar Kavram

Lazy initialization of StackExchange.Redis ConnectionMultiplexer for efficient socket and connection management
Bu soruyu puanla