NetCore MemoryCache use

Reference library

1.Install-Package Microsoft.Extensions.Caching.Memory

MemoryCacheOptions cache configuration

1.ExpirationScanFrequency Gets or sets the minimum time between successive scans spaced overdue items

2.SizeLimit size of the cache is not, this value is set cached copies

Gets or sets the number of compressed 3.CompactionPercentage cache maximum size is exceeded, the compression priority lower priority buffer, representative of 0.2 to 20%

  services.AddMemoryCache (Options => {
                 // buffer up to 100 parts by
                 @ ## Note netcore the cache unit is not, cache entries and cache relative relationship 
                options.SizeLimit = 2 ;
                 // buffer is full when compressed 20 % of the low-priority data 
                options.CompactionPercentage = 0.2 ;
                 // two seconds to find the time overdue items 
                options.ExpirationScanFrequency = TimeSpan.FromSeconds ( 2 ); 
            });

 

 

 

MemoryCacheEntryOptions single cache entry configuration

1.AbsoluteExpiration absolute expiration time

2. AbsoluteExpirationRelativeToNow relative to the absolute expiration time now

3.SlidingExpiration sliding expiration time, the cache is accessed again within range of a time period, the expiration time will be reset

4.Priority priority

5.Size cached copies

 public  BOOL the Add ( String Key, Object value, int ExpirtionTime = 20 is ) 
        { 
            IF (! String .IsNullOrEmpty (Key)) 
            { 
                MemoryCacheEntryOptions cacheEntityOps = new new MemoryCacheEntryOptions () 
                { 
                    // sliding expiration time of 20 seconds is not clear access 
                    slidingExpiration = the TimeSpan. FromSeconds (ExpirtionTime),
                     // set the number of copies 
                    Size = . 1 ,
                     // priority
                    = The Priority CacheItemPriority.Low, 
                }; 
                // expired back off 
                cacheEntityOps.RegisterPostEvictionCallback ((keyInfo, valueInfo, reason, State) => 
                { 
                    Console.WriteLine ($ " callback function keys [Output: {keyInfo}, value: {valueInfo} , cleared reasons: {reason}] " ); 
                }); 
                _cache.Set (Key, value, cacheEntityOps); 
            } 
            return  to true ; 
        }

The complete code

1. Interface

  public interface ICacheService
    {
        /// <summary>
        ///  新增
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="ExpirtionTime"></param>
        /// <returns></returns>
        bool Add(string key, object value, int ExpirtionTime = 20);


        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        string GetValue(string key);
        /// <summary>
        /// 验证缓存项是否存在
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <returns></returns>
        bool Exists(string key);

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool Remove(string key);
    }

2. To achieve ICacheService

/// <summary>
    /// 缓存接口实现
    /// </summary>
    public class MemoryCacheService : ICacheService
    {
        protected IMemoryCache _cache;

        public MemoryCacheService(IMemoryCache cache)
        {
            _cache = cache;
        }

        public bool Add(string key, object value, int ExpirtionTime = 20)
        {
            if (!string.IsNullOrEmpty(key))
            {
                CacheEntityOps MemoryCacheEntryOptions= new new MemoryCacheEntryOptions () 
                { 
                    // sliding expiration time of 20 seconds is not clear access 
                    slidingExpiration = TimeSpan.FromSeconds (ExpirtionTime),
                     // set the number of copies 
                    Size = . 1 ,
                     // priority of 
                    the Priority = CacheItemPriority.Low, 
                }; 
                // expired Press off 
                cacheEntityOps.RegisterPostEvictionCallback ((keyInfo, valueInfo, reason, State) => 
                { 
                    Console.WriteLine ($ " callback function keys [output: {keyInfo}, value: {valueInfo}, cleared reasons: {reason}] " ) ; 
                }); 
                _cache.Set (Key, value, cacheEntityOps); 
            } 
            return  to true ; 
        } 

        public  BOOL the Remove ( String Key) 
        { 
            IF ( String .IsNullOrEmpty (Key)) 
            { 
                return  to false ; 
            } 
            IF (the Exists (Key)) 
            { 
                _cache.Remove (Key); 
                return  to true ; 
            } 
            return  to false ; 
        }

        public string GetValue(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return null;
            }
            if (Exists(key))
            {
                return _cache.Get(key).ToString();
            }
            return null;
        }

        public bool Exists(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }

            object cache;
            return _cache.TryGetValue(key, out cache);
        }

    }

Great God posted 1: https://www.cnblogs.com/mylinx/p/10443494.html

Great God stickers 2: https://www.cnblogs.com/wyy1234/p/10519681.html#_label1_0

Guess you like

Origin www.cnblogs.com/acmeblogs/p/12035655.html