Skip to content

পোস্ট

ASP.Net Core-এ Request Caching বাস্তবায়ন

৮ জুলাই, ২০১৯ • 4 মিনিট পড়া

ASP.Net Core-এ Request Caching বাস্তবায়ন

একটি অ্যাপ্লিকেশনের ডেভেলপমেন্টের কোনো এক পর্যায়ে, সাধারণত বেশ তাড়াতাড়িই, আপনি বুঝতে পারেন যে অ্যাপ্লিকেশনটি ধীর। কিছু গবেষণার পর, অপরাধী হল, অপ্রয়োজনীয়ভাবে একই ডেটা পুনরুদ্ধার করা, এবং একটি আলো জ্বলে ওঠে, এবং আপনি ভাবেন: “আমার কিছু ক্যাশিং দরকার।”

ক্যাশিং একটি ডেটাবেস বা তৃতীয় পক্ষের API-এর অপ্রয়োজনীয় কল দূর করার জন্য একটি অমূল্য প্যাটার্ন। Microsoft সময়-ভিত্তিক ক্যাশিংয়ের জন্য IMemoryCache প্রদান করে, তবে কখনও কখনও সময়-ভিত্তিক ক্যাশিং আপনার যা প্রয়োজন তা নয়। এই নিবন্ধে, আমরা Request Scoped ক্যাশিং এবং এটি কীভাবে আমাদের উপকার করতে পারে তা দেখব।

Request ক্যাশিং কী? Request ক্যাশিং হল একটি ওয়েব রিকোয়েস্টের জীবনকালের জন্য ডেটা ক্যাশ করার একটি প্রক্রিয়া। ডট-নেটে, আমাদের HttpContext.Items কালেকশনের সাথে কিছু ক্ষমতায় এই সক্ষমতা ছিল, তবে, HttpContext তার ইনজেক্টেবিলিটির জন্য পরিচিত নয়।

Request Scoped ক্যাশিংয়ের কয়েকটি সুবিধা রয়েছে: প্রথমত, এটি পুরানো ডেটার উদ্বেগ দূর করে। বেশিরভাগ পরিস্থিতিতে, একটি রিকোয়েস্ট এক সেকেন্ডেরও কম সময়ে সম্পাদিত হয় এবং যা সাধারণত ডেটা পুরানো হওয়ার জন্য যথেষ্ট দীর্ঘ নয়। এবং দ্বিতীয়ত, মেয়াদ উত্তীর্ণ হওয়া একটি উদ্বেগ নয় কারণ রিকোয়েস্ট শেষ হলে ডেটা মারা যায়।

বক্সের বাইরে, Asp.Net Core-এ ইনজেক্টেবল ক্যাশিং নেই। আগে উল্লেখ করা হয়েছে, HttpContext.Items একটি বিকল্প, কিন্তু এটি একটি মার্জিত সমাধান নয়।

ভাগ্যক্রমে আমাদের জন্য, ASP.Net Core আমাদের বিল্ট-ইন dependency injection (DI) ফ্রেমওয়ার্ক ব্যবহার করে একটি ইনজেক্টেবল Request Caching বাস্তবায়ন তৈরি করার সরঞ্জাম দেয়।

বিল্ট-ইন DI ফ্রেমওয়ার্কে নির্ভরতার জন্য তিনটি জীবনকাল রয়েছে: Singleton, Scoped, এবং TransientSingleton অ্যাপ্লিকেশনের জীবনের জন্য, Scoped রিকোয়েস্টের জীবনের জন্য এবং Transient প্রতিটি রিকোয়েস্টের সাথে একটি নতুন ইনস্ট্যান্স।

আমি জিনিসগুলি সামঞ্জস্যপূর্ণ রাখতে IMemoryCache ইন্টারফেসের মডেল অনুসরণ করে একটি ইন্টারফেস তৈরি করেছি।

ইন্টারফেস

public interface IRequestCache
{
    /// <summary>
    /// Add the value into request cache. If the key already exists, the value is overwritten.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <typeparam name="TValue"></typeparam>
    void Add<TValue>(string key, TValue value);

    /// <summary>
    /// Remove the key from the request cache
    /// </summary>
    /// <param name="key"></param>
    void Remove(string key);

    /// <summary>
    /// Retrieve the value by key, if the key is not in the cache then the add func is called
    /// adding the value to cache and returning the added value.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="add"></param>
    /// <typeparam name="TValue"></typeparam>
    /// <returns></returns>
    TValue RetrieveOrAdd<TValue>(string key, Func<TValue> add);

    /// <summary>
    /// Retrieves the value by key. When the key does not exist the default value for the type is returned.
    /// </summary>
    /// <param name="key"></param>
    /// <typeparam name="TValue"></typeparam>
    /// <returns></returns>
    TValue Retrieve<TValue>(string key);
}

বাস্তবায়ন

public class RequestCache : IRequestCache
{
    IDictionary<string, object> _cache = new Dictionary<string, object>();

    /// <summary>
    /// Add the value into request cache. If the key already exists, the value is overwritten.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <typeparam name="TValue"></typeparam>
    public void Add<TValue>(string key, TValue value)
    {
        _cache[key] = value;
    }

    /// <summary>
    /// Remove the key from the request cache
    /// </summary>
    /// <param name="key"></param>
    public void Remove(string key)
    {
        if (_cache.ContainsKey(key))
        {
            _cache.Remove(key);
        }
    }

    /// <summary>
    /// Retrieve the value by key, if the key is not in the cache then the add func is called
    /// adding the value to cache and returning the added value.
    /// </summary>
    /// <param name="key"></param>
    /// <param name="add"></param>
    /// <typeparam name="TValue"></typeparam>
    /// <returns></returns>
    public TValue RetrieveOrAdd<TValue>(string key, Func<TValue> add)
    {
        if (_cache.ContainsKey(key))
        {
            return (TValue)_cache[key];
        }

        var value = add();

        _cache[key] = value;

        return value;
    }

    /// <summary>
    /// Retrieves the value by key. When the key does not exist the default value for the type is returned.
    /// </summary>
    /// <param name="key"></param>
    /// <typeparam name="TValue"></typeparam>
    /// <returns></returns>
    public TValue Retrieve<TValue>(string key)
    {
        if (_cache.ContainsKey(key))
        {
            return (TValue)_cache[key];
        }

        return default(TValue);
    }
}

ASP.Net Core-এর DI ফ্রেমওয়ার্ক ব্যবহার করে আমরা এটিকে Scoped হিসেবে সংযুক্ত করব।

services.AddScoped<IRequestCache, RequestCache>();

ব্যবহার

public class UserService
{
    private readonly IRequestCache _cache;
    private readonly IUserRepository _userRepository;

    public UserService(IRequestCache cache, IUserRepository userRepository)
    {
        _cache = cache;
        _userRepository = userRepository;
    }

    public User RetrieveUserById(int userId)
    {
        var buildCacheKey = UserService.BuildCacheKey(userId);

        return _cache.RetrieveOrAdd(BuildCacheKey, () => { return _userRepository.RetrieveUserBy(userId); });
    }

    public void Delete(int userId)
    {
        var buildCacheKey = UserService.BuildCacheKey(userId);

        _userRepository.Delete(userId);
        _cache.Remove(BuildCacheKey(userId));
    }

    private static string BuildCacheKey(int userId)
    {
        return $"user_{userId}";
    }
}

এটাই! Request Caching এখন আপনার প্রয়োজনীয় যেকোনো জায়গায় ইনজেক্টেবল।

Git Repository পরিদর্শন করুন এবং নির্দ্বিধায় কোডটি পরীক্ষা করে দেখুন।

লেখক: চাক কনওয়ে সফটওয়্যার ইঞ্জিনিয়ারিং এবং জেনারেটিভ এআই-তে বিশেষজ্ঞ। তার সাথে সোশ্যাল মিডিয়ায় যোগাযোগ করুন: X (@chuckconway) অথবা তাকে YouTube-এ দেখুন।

↑ উপরে ফিরে যান

আপনি এগুলোও পছন্দ করতে পারেন