Memory Cache in dotnet core

前端 未结 3 1343
心在旅途
心在旅途 2021-02-13 14:05

I am trying to write a class to handle Memory cache in a .net core class library. If I use not the core then I could write

using System.Runtime.Caching;
using S         


        
相关标签:
3条回答
  • 2021-02-13 14:52

    If you using Asp.net core you no need to custom SingleTon for cache, because Asp.net core is supported DI for your Cache class.

    To using IMemoryCache to set data to the memory of the server you can do as below:

    public void Add<T>(T o, string key)
    {
        if (IsEnableCache)
        {
            T cacheEntry;
    
            // Look for cache key.
            if (!_cache.TryGetValue(key, out cacheEntry))
            {
                // Key not in cache, so get data.
                cacheEntry = o;
    
                // Set cache options.
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                    // Keep in cache for this time, reset time if accessed.
                    .SetSlidingExpiration(TimeSpan.FromSeconds(7200));
    
                // Save data in cache.
                _cache.Set(key, cacheEntry, cacheEntryOptions);
            }
        }
    }
    

    For more detail, you can read the article implement in-memory in asp.net core

    0 讨论(0)
  • 2021-02-13 14:58

    The constructor is:

    using Microsoft.Extensions.Caching.Memory;
    

    . . .

    MemoryCache myCache = new MemoryCache(new MemoryCacheOptions());
    
    0 讨论(0)
  • 2021-02-13 15:05

    My answer is focused on the "Within .Net core I could not find System.Runtime.Cache", as I run into this same issue. For using IMemoryCache with the specific OP's scenario, the accepted answer is great.


    There are two completely different caching implementations/solutions:

    1 - System.Runtime.Caching/MemoryCache
    2 - Microsoft.Extensions.Caching.Memory/IMemoryCache


    System.Runtime.Caching/MemoryCache:
    This is pretty much the same as the old day's ASP.Net MVC's HttpRuntime.Cache. You can use it on ASP.Net CORE without any dependency injection. This is how to use it:

    // First install 'System.Runtime.Caching' (NuGet package)
    
    // Add a using
    using System.Runtime.Caching;
    
    // To get a value
    var myString = MemoryCache.Default["itemCacheKey"];
    
    // To store a value
    MemoryCache.Default["itemCacheKey"] = myString;
    

    Microsoft.Extensions.Caching.Memory
    This one is tightly coupled with Dependency Injection. This is one way to implement it:

    // In asp.net core's Startup add this:
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMemoryCache();
    }
    

    Using it on a controller:

    // Add a using
    using Microsoft.Extensions.Caching.Memory;
    
    // In your controller's constructor, you add the dependency on the 'IMemoryCache'
    public class HomeController : Controller
    {
        private IMemoryCache _cache;
        public HomeController(IMemoryCache memoryCache)
        {
            _cache = memoryCache;
        }
    
        public void Test()
        {
            // To get a value
            string myString = null;
            if (_cache.TryGetValue("itemCacheKey", out myString))
            { /*  key/value found  -  myString has the key cache's value*/  }
    
    
            // To store a value
            _cache.Set("itemCacheKey", myString);
        }
    }
    

    As pointed by @WillC, this answer is actually a digest of Cache in-memory in ASP.NET Core documentation. You can find extended information there.

    0 讨论(0)
提交回复
热议问题