Skip to content

पोस्ट

ASP.Net Core में Request Caching को लागू करना

8 जुलाई 2019 • 4 मिनट पढ़ना

ASP.Net Core में Request Caching को लागू करना

किसी एप्लिकेशन के विकास के दौरान, आमतौर पर काफी जल्दी, आप महसूस करते हैं कि एप्लिकेशन धीमी है। कुछ शोध के बाद, अपराधी है, अनावश्यक रूप से एक ही डेटा को पुनः प्राप्त करना, और एक प्रकाश जलता है, और आप सोचते हैं: “मुझे कुछ कैशिंग की आवश्यकता है।”

कैशिंग डेटाबेस या तीसरे पक्ष के API के लिए अनावश्यक कॉल को समाप्त करने के लिए एक अमूल्य पैटर्न है। Microsoft समय-आधारित कैशिंग के लिए IMemoryCache प्रदान करता है, हालांकि कभी-कभी समय-आधारित कैशिंग वह नहीं है जिसकी आपको आवश्यकता है। इस लेख में, हम Request Scoped कैशिंग और यह हमें कैसे लाभ दे सकता है, इस पर नज़र डालते हैं।

Request कैशिंग क्या है? Request कैशिंग एक वेब अनुरोध के जीवन के लिए डेटा को कैश करने की एक तंत्र है। dot-net में, हमारे पास HttpContext.Items संग्रह के साथ कुछ क्षमता में यह क्षमता है, हालांकि, HttpContext अपनी injectability के लिए जाना नहीं जाता है।

Request Scoped कैशिंग के कुछ लाभ हैं: पहला, यह पुराने डेटा की चिंता को समाप्त करता है। अधिकांश परिदृश्यों में, एक अनुरोध एक सेकंड से कम समय में निष्पादित होता है और जो आमतौर पर डेटा के पुराने होने के लिए पर्याप्त लंबा नहीं है। और दूसरा, समाप्ति चिंता का विषय नहीं है क्योंकि डेटा अनुरोध समाप्त होने पर मर जाता है।

बॉक्स से बाहर, Asp.Net Core के पास injectable कैशिंग नहीं है। जैसा कि पहले उल्लेख किया गया है, HttpContext.Items एक विकल्प है, लेकिन यह एक सुरुचिपूर्ण समाधान नहीं है।

सौभाग्य से हमारे लिए, ASP.Net Core हमें built-in dependency injection (DI) फ्रेमवर्क का उपयोग करके एक injectable Request Caching कार्यान्वयन बनाने के लिए उपकरण देता है।

built-in DI फ्रेमवर्क के पास dependencies के लिए तीन lifetimes हैं: 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 अब किसी भी जगह injectable है जहां आपको इसकी आवश्यकता है।

Git Repository पर जाएं और कोड को आजमाने के लिए स्वतंत्र महसूस करें।

लेखक: Chuck Conway एक AI इंजीनियर हैं जिनके पास सॉफ्टवेयर इंजीनियरिंग का लगभग 30 साल का अनुभव है। वह व्यावहारिक AI सिस्टम बनाते हैं—कंटेंट पाइपलाइन, इंफ्रास्ट्रक्चर एजेंट, और ऐसे टूल जो वास्तविक समस्याओं को हल करते हैं—और अपनी सीख को साझा करते हैं। सोशल मीडिया पर उनसे जुड़ें: X (@chuckconway) या YouTube और SubStack पर उनसे मिलें।

↑ शीर्ष पर वापस जाएं

आपको यह भी पसंद आ सकता है