परिचय
<पी> एज़्योर रेडिस कैश ओपन सोर्स, इन-मेमोरी रेडिस कैश पर आधारित है जो ऐप के प्रदर्शन को बेहतर बनाने के लिए वेब ऐप्स को बैकएंड डेटा स्रोत से डेटा को कैश में और सर्वर वेब पेजों को कैश में लाने की अनुमति देता है। इस चरण-दर-चरण ट्यूटोरियल में, हम सीखेंगे कि अपने वेब ऐप में Azure Redis Cache का उपयोग कैसे करें। Azure Redis Cache क्या है?
<पी> आधुनिक एप्लिकेशन अधिकतर बड़ी मात्रा में डेटा के साथ काम करते हैं। इस परिदृश्य में, जब आप किसी डेटाबेस से डेटा पुनर्प्राप्त करते हैं, तो यह आम तौर पर तालिका ढूंढता है और परिणाम प्राप्त करता है जो यह उपयोगकर्ता को वापस भेजता है। ऐसे मामले में, कई अनुरोधों के कारण प्रदर्शन कम हो जाता है। इसलिए, अनुरोधों की कुछ संख्या को कम करने के लिए, आप कैश डेटा का उपयोग कर सकते हैं जो बार-बार नहीं बदलता है। <पी> Redis Cache एक खुला स्रोत, इन-मेमोरी डेटाबेस है जिसका उपयोग कुंजी-मूल्य प्रारूप का उपयोग करके कैश मेमोरी में डेटा को पुनर्प्राप्त और संग्रहीत करके किसी एप्लिकेशन के प्रदर्शन को बेहतर बनाने के लिए किया जाता है। Azure Redis Cache एक सुविधा संपन्न कार्यक्षमता है जो आपको सुरक्षित, कम-विलंबता, उच्च-प्रदर्शन थ्रूपुट तक पहुंच प्रदान करती है। <पी> आइए C# के साथ रेडिस कैश कार्यान्वयन शुरू करें। <पी> चरण 1. Azure पोर्ट में लॉग इन करें, डेटाबेस>> रेडिस कैश पर जाएं। <पी> पी> <पी> चरण 2. एक समाचार रेडिस कैश बनाएं। <पी> पी> <पी> चरण 3. नव निर्मित रेडिस कैश से जुड़ने के लिए एक्सेस कुंजियाँ प्राप्त करें। <पी>
StackExchange.Redis इंस्टॉल करें
<पी> चरण 4. निम्न आदेश का उपयोग करके StackExchange.Redis NuGet पैकेज स्थापित करें। <पी> इंस्टॉल-पैकेज StackExchange.Redis <पी> पी> <पी> आइए डेटा को रेडिस कैश में संग्रहीत करने और रेडिस कैश से डेटा पुनर्प्राप्त करने के लिए कोडिंग शुरू करें। हमने हाल ही में Azure दस्तावेज़ DB CRUD संचालन का कोड देखा था। यदि आपने इसे अभी तक नहीं पढ़ा है, तो Azure Document DB CRUD ऑपरेशन पर क्लिक करें और पढ़ें। हमारे पास दस्तावेज़ DB में CRUD संचालन का कोड है। अब, हम यहां रेडिस कैश लागू करेंगे। <पी> चरण 5. पिछले आलेख के समान, हमें Redis Cache कनेक्शन स्ट्रिंग को appsettings.dev.json फ़ाइल में जोड़ने की आवश्यकता है। <पी>
<पी> चरण 6. अब, config.cs में एक और प्रॉपर्टी RedisCache जोड़ें जो कि appsettings.dev.json से Redis Cache कनेक्शन स्ट्रिंग का मान प्राप्त करेगी।
public class Config
{
public DocDbConnectionString docDb { get; set; }
public string RedisCache { get; set; }
}`
public class DocDbConnectionString
{
public string EndPoint { get; set; }
public string AuthKey { get; set; }
public string Database { get; set; }
public string Collection { get; set; }
}
<पी> चरण 7. आइए प्रोग्राम.सीएस फ़ाइल पर आएं और रेडिस कैश के लिए कनेक्शनमल्टीप्लेक्सर जोड़ें।पी>
IDatabase cache = lazyConnection.Value.GetDatabase();
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
string cacheConnection = configs.RedisCache;
return ConnectionMultiplexer.Connect(cacheConnection);
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
<पी> अब, हम दस्तावेज़ DB में दस्तावेज़ बनाते समय कुंजी के आधार पर एक दस्तावेज़ को रेडिस कैश में संग्रहीत करेंगे और इस दस्तावेज़ को पढ़ते समय, हम यह जांचने के लिए कुंजी का उपयोग करेंगे कि दस्तावेज़ रेडिस कैश में मौजूद है या नहीं। हम दस्तावेज़ को दस्तावेज़ DB से आगे पढ़ना छोड़ देंगे। ऐसा करके हम एप्लिकेशन के प्रदर्शन को बढ़ा सकते हैं।
var collection = UriFactory.CreateDocumentCollectionUri(configs.docDb.Database, configs.docDb.Collection);
try
{
// create JObject which contains the employee details
Console.WriteLine("\nCreating document");
JObject emp = new JObject();
emp.Add("id", "V003");
emp.Add("name", "virendra");
emp.Add("address", "Indore");
emp.Add("Country", "India");
// create the document into DocumentDb
var createResponse = await Client.CreateDocumentAsync(collection, emp);
var createdDocument = createResponse.Resource;
Console.WriteLine("Document with id {0} created", createdDocument.Id);
// Set JObject into redis cache with key "redisEmp3"
var entryInRedis = await cache.StringSetAsync("redisEmp3", emp.ToString());
Console.WriteLine("Document with key redisEmp3 stored into redis cache");
}
catch (Exception ex)
{
throw ex;
}
<पी> चरण 8. इसके बजाय, आइए Redis Cache से दस्तावेज़ पढ़ें।
// Read document from Redis Cache.
var empInRedis = await cache.StringGetAsync("redisEmp3");
if (!empInRedis.IsNullOrEmpty)
{
Console.WriteLine("Read Document from RedisCache {0} : ", empInRedis);
}
// If Redis Cache does not have Document, then read the document from Document DB
if (empInRedis.IsNullOrEmpty)
{
var readResponse = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(configs.docDb.Database, configs.docDb.Collection, "V001"));
var readDocument = readResponse.Resource;
Console.WriteLine("Read Document {0}: ", readResponse.Resource.ToString());
}
<पी> नीचे दिया गया स्नैपशॉट दिखाता है कि हम Redis Cache से किसी दस्तावेज़ को कैसे पढ़ते हैं। <पी> पी> <पी> चरण 10. निम्नलिखित Program.cs वर्ग का संपूर्ण कोड है।
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using Microsoft.Azure.Documents.Client;
public class Program
{
private static IConfiguration Configuration { get; set; }
private static Config configs;
private DocumentClient client;
private IDatabase cache = lazyConnection.Value.GetDatabase();
static void Main(string[] args)
{
// Set up Configuration
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: false, reloadOnChange: true);
Configuration = builder.Build();
configs = new Config();
Configuration.Bind(configs);
Program obj = new Program();
obj.CRUDOperation().Wait();
}
// CRUD Operation
private async Task CRUDOperation()
{
var collection = UriFactory.CreateDocumentCollectionUri(configs.docDb.Database, configs.docDb.Collection);
try
{
// create JObject which contains the employee details
Console.WriteLine("\nCreating document");
JObject emp = new JObject();
emp.Add("id", "V003");
emp.Add("name", "virendra");
emp.Add("address", "Indore");
emp.Add("Country", "India");
// create the document
var createResponse = await Client.CreateDocumentAsync(collection, emp);
var createdDocument = createResponse.Resource;
Console.WriteLine("Document with id {0} created", createdDocument.Id);
// Set JObject into redis cache with key "redisEmp3"
var entryInRedis = await cache.StringSetAsync("redisEmp3", emp.ToString());
Console.WriteLine("Document with key redisEmp3 stored into redis cache");
}
catch (Exception ex)
{
throw ex;
}
// read document from redis cache
var empInRedis = await cache.StringGetAsync("redisEmp3");
if (!empInRedis.IsNullOrEmpty)
{
Console.WriteLine("Read Document from RedisCache {0} : ", empInRedis);
}
if (empInRedis.IsNullOrEmpty)
{
// Read document from document Db
var readResponse = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(configs.docDb.Database, configs.docDb.Collection, "V001"));
var readDocument = readResponse.Resource;
Console.WriteLine("Read Document {0}: ", readResponse.Resource.ToString());
}
}
// Get a single instance of Document client and reuse
public DocumentClient Client
{
get
{
if (client == null)
{
Uri endpointUri = new Uri(configs.docDb.EndPoint);
client = new DocumentClient(endpointUri, configs.docDb.AuthKey, null, ConsistencyLevel.Session);
client.OpenAsync();
}
return client;
}
}
// To establish Redis Cache connection
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
string cacheConnection = configs.RedisCache;
return ConnectionMultiplexer.Connect(cacheConnection);
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}
<पी> मुझे उम्मीद है कि यह लेख आपकी मदद करेगा।