কম্পিউটার টিউটোরিয়াল

সি شار্প সহ এজার রেডিস ক্যাশ দক্ষতা: একটি প্র্যাক্টিক্যাল গাইড

ভূমিকা

এজার রেডিস ক্যাশ ওপেন সোর্স, ইন-মেমরি রেডিস ক্যাশের উপর ভিত্তি করে তৈরি, যা ওয়েব অ্যাপ্লিকেশনগুলিকে ব্যাকএন্ড ডেটা সোর্স থেকে ডেটা ক্যাশে আনতে এবং ক্যাশ থেকে ওয়েব পেজ سرو করতে সক্ষম করে, ফলে অ্যাপ্লিকেশনের পারফরম্যান্স উন্নত হয়। এই ধাপে ধাপে টিউটোরিয়ালে, আমরা শিখব কীভাবে আমাদের ওয়েব অ্যাপ্লিকেশনে এজার রেডিস ক্যাশ ব্যবহার করব।

এজার রেডিস ক্যাশ কী?

আধুনিক অ্যাপ্লিকেশনগুলো অধিকাংশ বড় পরিমাণ ডেটার সাথে কাজ করে। এই পরিস্থিতিতে, যখন আপনি ডেটাবেস থেকে ডেটা রিট্রিভ করেন, এটি সাধারণত টেবিল খুঁজে বের করে এবং ফলাফলগুলো ব্যবহারকারীকে ফেরত পাঠায়। একাধিক রিকোয়েস্টের কারণে পারফরম্যান্স নেমে যায়। তাই, কম পরিবর্তনশীল ডেটা ক্যাশে রাখে আপনি কিছু রিকোয়েস্ট কমাতে পারেন।

রেডিস ক্যাশ একটি ওপেন সোর্স, ইন-মেমরি ডেটাবেস যা কী-ভ্যালু ফরম্যাটে ক্যাশ মেমরিতে ডেটা সংরক্ষণ ও রিট্রিভ করে অ্যাপ্লিকেশনের পারফরম্যান্স উন্নত করতে ব্যবহৃত হয়। এজার রেডিস ক্যাশ একটি ফিচার-রিচ ফাংশনালিটি যা আপনাকে সুরক্ষিত, লো-লেটেন্সি, হাই-পারফরম্যান্স থ্রুপুট প্রদান করে।

চলুন সি شار্প সহ রেডিস ক্যাশ বাস্তবায়ন শুরু করি।

ধাপ ১। এজার পোর্টালে লগ ইন করুন, ডেটাবেস >> রেডিস ক্যাশ-এ যান।

ধাপ ২। একটি নতুন রেডিস ক্যাশ তৈরি করুন।

ধাপ ৩। নতুন তৈরি করা রেডিস ক্যাশের সাথে সংযোগের জন্য এক্সেস কীগুলো পান।

সি شار্প সহ এজার রেডিস ক্যাশ দক্ষতা: একটি প্র্যাক্টিক্যাল গাইড

StackExchange.Redis ইনস্টল করুন

ধাপ ৪। নিম্নলিখিত কমান্ড ব্যবহার করে StackExchange.Redis নুগেট প্যাকেজ ইনস্টল করুন।

Install-Package StackExchange.Redis

আমরা gầnে এজার ডকুমেন্ট ডিবি CRUD অপারেশনসের কোড দেখেছি। যদি আপনি এখনো না পড়েছেন, তবে এজার ডকুমেন্ট ডিবি CRUD অপারেশন পড়ে দেখুন। ডকুমেন্ট ডিবিতে CRUD অপারেশনসের কোড আছে। এখন, আমরা এখানে রেডিস ক্যাশ বাস্তবায়ন করব।

ধাপ ৫। পূর্ববর্তী আর্টিকেলের মতো, আমাদের appsettings.dev.json ফাইলে রেডিস ক্যাশ কানেকশন স্ট্রিং যোগ করতে হবে।

সি شار্প সহ এজার রেডিস ক্যাশ দক্ষতা: একটি প্র্যাক্টিক্যাল গাইড

ধাপ ৬। এখন, Config.cs-এ RedisCache নামে একটি প্রোপার্টি যোগ করুন যা appsettings.dev.json- থেকে রেডিস ক্যাশ কানেকশন স্ট্রিং এর মান পাবে।

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; }
}

ধাপ ৭। Program.cs ফাইলে আসুন এবং রেডিস ক্যাশের জন্য 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;
}

ধাপ ৮। এখন, রেডিস ক্যাশ থেকে ডকুমেন্ট পড়ুন।

// 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());
}

নিচের স্ন্যাপশটটি দেখায় কীভাবে আমরা রেডিস ক্যাশ থেকে একটি ডকুমেন্ট পড়ি।

ধাপ ১০। নিচে 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; }
    }
}

আশা করি এই নিবন্ধটি আপনাকে সাহায্য করবে।

  1. জাভাস্ক্রিপ্ট দিয়ে HTML অডিও প্লেয়ারের কারেন্ট টাইমস্ট্যাম্প দেখানো

  2. লিনাক্স শেল স্ক্রিপ্টিং: ভেরিয়েবল নিয়ে গভীরে দৃষ্টিপাত – পার্ট ৯

  3. C++-এ Largest BST Subtree: বাইনারি ট্রিতে সর্বোচ্চ BST সাবট্রি খুঁজে বের করা

  4. Java MySQL ব্যবহার করে MySQL টেবিল আপডেট করা