netCore自定义接口依赖注入DI

netCore自定义接口依赖注入DI定义接口ICacheUtil.csnamespace Snblog.Cache.CacheUtil{public interface ICacheUtil{/// summary/// 设置并返回缓存值(值类型)/// /summary/// typeparam name=T返回类型...

netCore自定义接口依赖注入DI

定义接口

ICacheUtil.cs

namespace Snblog.Cache.CacheUtil
{
    public interface ICacheUtil
    {
        /// <summary>
        /// 设置并返回缓存值(值类型)
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="key">缓存键值</param>
        /// <param name="value">要缓存的值</param>
        /// <returns>result</returns>
        public T CacheNumber<T>(string key, T value);
        /// <summary>
        /// 设置并返回缓存值(字符串)
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="key">缓存键值</param>
        /// <param name="value">要缓存的值</param>
        public T CacheString<T>(string key, T value);
    }
}

继承接口

using Snblog.Cache.Cache;

namespace Snblog.Cache.CacheUtil
{
    public class CacheUtil:ICacheUtil
    {

        //创建内存缓存对象
        private static CacheManager _cache = new CacheManager();
        /// <summary>
        /// 设置并返回缓存值(值类型)
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="key">缓存键值</param>
        /// <param name="value">要缓存的值</param>
        /// <returns>result</returns>
        public T CacheNumber<T>(string key, T value)
        {
            T result = default;

            if (_cache.IsInCache(key)) //如果存在缓存取值
            {
                result = _cache.Get<T>(key);
            }
            else
            {
                if (!value.Equals(0))
                {
                _cache.Set_AbsoluteExpire<T>(key, value, _cache.time);
                }
            }
            return result;
        }

          /// <summary>
        /// 设置并返回缓存值(字符串)
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="key">缓存键值</param>
        /// <param name="value">要缓存的值</param>
        public T CacheString<T>(string key, T value)
        {
            T result = default;
            if (_cache.IsInCache(key)) //如果存在缓存取值
            {
                result = _cache.Get<T>(key);
            }
            else
            {
                if (value != null)
                {
                    _cache.Set_AbsoluteExpire<T>(key, value, _cache.time);
                }
            }
            return result;
        }
    }
}

DI依赖注入配置

ConfigureServices

services.AddScoped<ICacheUtil,CacheUtil>();

使用

private readonly CacheUtil _cacheUtil;
public SnArticleService(ICacheUtil cacheUtil,IRepositoryFactory repositoryFactory, IconcardContext mydbcontext, snblogContext coreDbContext, ILogger<SnArticleService> logger) : base(repositoryFactory, mydbcontext)
        {
            _coreDbContext = coreDbContext;
            _cacheUtil= (CacheUtil)cacheUtil;
            _logger = logger ?? throw new ArgumentNullException(nameof(Logger));
        }
        
  result = _cacheUtil.CacheString("AsyGetTestName" + id, result);

本文标题为:netCore自定义接口依赖注入DI