.NET Cache方法类

创建CacheHelper.cs

csharp 复制代码
     public class CacheHelper
    {
        public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());

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


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


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

        /// <summary>
        ///清除Cache[key]的值
        /// </summary>
        ///<param name="key"></param>
        public static void CleanCache(string key)
        {
            _memoryCache.Remove(key);
        }

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

        public static r GetCacheOrQuery<p, r>(string key, Func<p, r> 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<p1, p2, r>(string key, Func<p1, p2, r> 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<p1, p2, p3, r>(string key, Func<p1, p2, p3, r> 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<p1, p2, p3, p4, r>
            (string key, Func<p1, p2, p3, p4, r> 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<p1, p2, p3, p4, p5, r>
            (string key, Func<p1, p2, p3, p4, p5, r> 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<p1, p2, p3, p4, p5, p6, r>
            (string key, Func<p1, p2, p3, p4, p5, p6,r> 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;
        }

    }

测试

csharp 复制代码
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
            };
        }
    }
}

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

相关推荐
码上一元2 小时前
SpringBoot自动装配原理解析
java·spring boot·后端
枫叶_v4 小时前
【SpringBoot】22 Txt、Csv文件的读取和写入
java·spring boot·后端
杜杜的man5 小时前
【go从零单排】Closing Channels通道关闭、Range over Channels
开发语言·后端·golang
java小吕布5 小时前
Java中Properties的使用详解
java·开发语言·后端
2401_857610036 小时前
Spring Boot框架:电商系统的技术优势
java·spring boot·后端
杨哥带你写代码8 小时前
网上商城系统:Spring Boot框架的实现
java·spring boot·后端
camellias_8 小时前
SpringBoot(二十一)SpringBoot自定义CURL请求类
java·spring boot·后端
背水8 小时前
初识Spring
java·后端·spring
晴天飛 雪9 小时前
Spring Boot MySQL 分库分表
spring boot·后端·mysql
weixin_537590459 小时前
《Spring boot从入门到实战》第七章习题答案
数据库·spring boot·后端