.NET Cache方法类

2023年 10月 4日 78.7k 0

创建CacheHelper.cs

     public class CacheHelper
    {
        public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());

        /// 
        /// 缓存绝对过期时间
        /// 
        ///Cache键值
        ///给Cache[key]赋的值
        ///minute分钟后绝对过期
        public static void AddCacheWithAbsolute(string key, object value, int minute)
        {
            if (value == null) return;
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions()
                    .SetAbsoluteExpiration(TimeSpan.FromMinutes(minute)));
        }


        /// 
        /// 缓存相对过期,最后一次访问后minute分钟后过期
        /// 
        ///Cache键值
        ///给Cache[key]赋的值
        ///滑动过期分钟
        public static void AddCacheWithRelative(string key, object value, int minute)
        {
            if (value == null) return;
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions()
                    .SetSlidingExpiration(TimeSpan.FromMinutes(minute)));
        }


        /// 
        ///以key键值,把value赋给Cache[key].如果不主动清空,会一直保存在内存中.
        /// 
        ///Cache键值
        ///给Cache[key]赋的值
        public static void AddCache(string key, object value)
        {
            _memoryCache.Set(key, value);
        }

        /// 
        ///清除Cache[key]的值
        /// 
        ///
        public static void CleanCache(string key)
        {
            _memoryCache.Remove(key);
        }

        /// 
        ///根据key值,返回Cache[key]的值
        /// 
        ///
        public static object GetCache(string key)
        {
            return _memoryCache.Get(key);
        }
        /// 
        /// 根据key查询value,如果不存在,则执行传入的查询方法并存入缓存
        /// 
        /// 
        /// 
        /// 
        /// 
        public static r GetCacheOrQuery(string key, Func myFunc)
        {
            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc();
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }

        public static r GetCacheOrQuery(string key, Func myFunc, p param1)
        {
            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc(param1);
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }

        public static r GetCacheOrQuery(string key, Func myFunc, p1 param1, p2 param2)
        {
            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc(param1,param2);
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }


        public static r GetCacheOrQuery(string key, Func myFunc, p1 param1, p2 param2, p3 param3)
        {
            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc(param1,param2,param3);
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }


        public static r GetCacheOrQuery
            (string key, Func myFunc, p1 param1, p2 param2, p3 param3, p4 param4)
        {
            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc(param1,param2,param3,param4);
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }

        public static r GetCacheOrQuery
            (string key, Func myFunc, p1 param1, p2 param2, p3 param3, p4 param4, p5 param5)
        {

            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc(param1, param2, param3, param4,param5);
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }

        public static r GetCacheOrQuery
            (string key, Func myFunc, p1 param1, p2 param2, p3 param3, p4 param4, p5 param5, p6 param6)
        {

            r result;
            if (GetCache(key) is not r value)
            {
                result = myFunc(param1, param2, param3, param4,param5,param6);
                AddCache(key, result);
            }
            else
            {
                result = value;
            }
            return result;
        }

    }

测试

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Any;

namespace Cache.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public Object Get()
        {
            object queryData = CacheHelper.GetCacheOrQuery("queryValue", value);
            return queryData;
        }

        private static int index = 1;

        private object value()
        {
            Console.WriteLine($"这是第{index++}次查询数据");
            return new
            {
                name = "zhangsan",
                age = 22
            };
        }
    }
}

只有当第一次访问时,才会执行查询方法

image.png

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论