পরিচয়
Azure রেডিস ক্যাশে ওপেন সোর্স, ইন-মেমরি রেডিস ক্যাশের উপর ভিত্তি করে তৈরি করা হয়েছে যা ওয়েব অ্যাপগুলিকে ব্যাকএন্ড ডেটা সোর্স থেকে ক্যাশে এবং সার্ভার ওয়েব পৃষ্ঠাগুলিতে অ্যাপের কার্যকারিতা উন্নত করতে ক্যাশে থেকে ডেটা আনার অনুমতি দেয়। এই ধাপে ধাপে টিউটোরিয়ালে, আমরা শিখব কিভাবে আমাদের ওয়েব অ্যাপে Azure Redis ক্যাশে ব্যবহার করতে হয়।
Azure Redis ক্যাশে কি?
আধুনিক অ্যাপ্লিকেশনগুলি বেশিরভাগ ডেটার সাথে কাজ করে। এই পরিস্থিতিতে, যখন আপনি একটি ডাটাবেস থেকে ডেটা পুনরুদ্ধার করেন, এটি সাধারণত টেবিলটি খুঁজে পায় এবং ফলাফলগুলি পায় যা এটি ব্যবহারকারীকে ফেরত পাঠায়। পারফরম্যান্স, এই ধরনের ক্ষেত্রে, একাধিক অনুরোধের কারণে কমে যায়। তাই, কিছু সংখ্যক অনুরোধ কমাতে, আপনি ক্যাশে ডেটা ব্যবহার করতে পারেন যা ঘন ঘন পরিবর্তন হয় না।
রেডিস ক্যাশে একটি ওপেন সোর্স, ইন-মেমরি ডাটাবেস যা একটি কী-মান বিন্যাস ব্যবহার করে ক্যাশে মেমরিতে ডেটা পুনরুদ্ধার এবং সংরক্ষণ করে একটি অ্যাপ্লিকেশনের কার্যকারিতা উন্নত করতে ব্যবহৃত হয়৷ Azure Redis ক্যাশে একটি বৈশিষ্ট্য-সমৃদ্ধ কার্যকারিতা যা আপনাকে সুরক্ষিত, কম লেটেন্সি, উচ্চ-পারফরম্যান্স থ্রুপুটে অ্যাক্সেস দেয়৷
C# দিয়ে Redis ক্যাশে বাস্তবায়ন শুরু করা যাক।
ধাপ 1. Azure পোর্টে লগ ইন করুন, ডাটাবেসে যান>> রেডিস ক্যাশে।
ধাপ 2। একটি সংবাদ রেডিস ক্যাশে তৈরি করুন৷
৷
ধাপ 3। নতুন তৈরি রেডিস ক্যাশের সাথে সংযোগ করতে অ্যাক্সেস কীগুলি পান৷

StackExchange.Redis ইনস্টল করুন
ধাপ 4। নিম্নলিখিত কমান্ড ব্যবহার করে StackExchange.Redis NuGet প্যাকেজ ইনস্টল করুন।
ইনস্টল-প্যাকেজ StackExchange.Redis
আসুন রেডিস ক্যাশে ডেটা সংরক্ষণ করতে কোডিং শুরু করি এবং রেডিস ক্যাশে থেকে ডেটা পুনরুদ্ধার করি। আমরা সম্প্রতি Azure ডকুমেন্ট DB CRUD অপারেশনের কোড দেখেছি। আপনি যদি এটি এখনও না পড়ে থাকেন তবে Azure Document DB CRUD Operation-এ ক্লিক করুন এবং পড়ুন। ডকুমেন্ট ডিবিতে আমাদের CRUD অপারেশনের কোড আছে। এখন, আমরা এখানে রেডিস ক্যাশে প্রয়োগ করব।
ধাপ 5। আগের নিবন্ধের মতো, আমাদের appsettings.dev.json ফাইলে Redis ক্যাশে সংযোগ স্ট্রিং যোগ করতে হবে।

ধাপ 6. এখন, Config.cs-এ RedisCache আরও একটি প্রপার্টি যোগ করুন যা appsettings.dev.json থেকে Redis ক্যাশে সংযোগ স্ট্রিংয়ের মান পাবে।
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. আসুন program.cs ফাইলে আসি এবং Redis ক্যাশের জন্য ConnectionMultiplexer যোগ করি।
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;
}
}
এখন, ডকুমেন্ট ডিবি-তে একটি ডকুমেন্ট তৈরি করার সময় আমরা কী-এর ভিত্তিতে একটি ডকুমেন্ট রেডিস ক্যাশে সংরক্ষণ করব এবং এই ডকুমেন্টটি পড়ার সময়, ডকুমেন্টটি রেডিস ক্যাশে উপস্থিত আছে কি না তা পরীক্ষা করার জন্য আমরা কী ব্যবহার করব। আমরা ডকুমেন্ট ডিবি থেকে ডকুমেন্টটি পড়া এড়িয়ে যাব। এটি করার মাধ্যমে, আমরা অ্যাপ্লিকেশনটির কর্মক্ষমতা বাড়াতে পারি।
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। পরিবর্তে, আসুন আমরা রেডিস ক্যাশে থেকে নথিটি পড়ি।
// 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 ক্যাশে থেকে একটি ডকুমেন্ট পড়ি।
ধাপ 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;
}
}
}
আমি আশা করি এই নিবন্ধটি আপনাকে সাহায্য করবে৷