ASP.NET CORE 弟六篇 AOP自定义筛选,Redis入门 11.1

原文作者:老张的哲学

零、今天完成的大红色部分

image

一、给缓存增加验证筛选特性

1、自定义缓存特性
在解决方案中添加新项目Blog.Core.Common,然后在该Common类库中添加 特性文件夹 和 特性实体类,以后特性就在这里

//CachingAttribute

 

using System;

namespace Blog.Core.Common.Attribue
{
    /// <summary>
    /// 这个Attribute就是使用时候的验证,把它添加到要缓存数据的方法中,
    /// 即可完成缓存的操作。注意是对Method验证有效
    /// </summary>
    public class CachingAttribue : Attribute
    {
        /// <summary>
        /// 缓存绝对过期时间
        /// </summary>
        public int AbsoluteExpiration { get; set; } = 30;
    }
}

2、在AOP拦截器中进行过滤

添加Common程序集引用,然后修改缓存AOP类方法 BlogCacheAOP=》Intercept,简单对方法的方法进行判断

//qCachingAttribute 代码

 

//Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义
        public void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;
            //对当前方法的特性验证
            var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute;
            //只有那些指定的才可以被缓存,需要验证
            if (qCachingAttribute != null)
            {
                //获取自定义缓存键
                var cacheKey = CustomCacheKey(invocation);
                //根据key获取相应的缓存值
                var cacheValue = _cache.Get(cacheKey);
                if (cacheValue != null)
                {
                    //将当前获取到的缓存值,赋值给当前执行方法
                    invocation.ReturnValue = cacheValue;
                    return;
                }
                //去执行当前的方法
                invocation.Proceed();
                //存入缓存
                if (!string.IsNullOrWhiteSpace(cacheKey))
                {
                    _cache.Set(cacheKey, invocation.ReturnValue);
                }
            }
            else
            {
                invocation.Proceed();//直接执行被拦截方法
            }
        }

3、在service层中增加缓存特性
在指定的Service层中的某些类的某些方法上增加特性(一定是方法,不懂的可以看定义特性的时候AttributeTargets.Method)

 

using Blog.Core.Common.Attribue;
using Blog.Core.IRepository;
using Blog.Core.IServices;
using Blog.Core.Model;
using Blog.Core.Services.Base;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Blog.Core.Services
{
    public class ArticleServices : BaseServices<Article>, IArticleServices
    {
        readonly IArticleRepository dal;

        public ArticleServices(IArticleRepository dal)
        {
            this.dal = dal;
            base.baseDal = dal;
        }

        /// <summary>
        /// 获取博客列表
        /// </summary>
        /// <returns></returns>
        [CachingAttribue(AbsoluteExpiration = 10)] // 添加特性
        public async Task<List<Article>> getBlogs()
        {
            var blogList = await dal.Query(a => a.bID > 0,a => a.bID);
            return blogList;
        }
    }
}

4、特定缓存效果展示

运行项目,打断点,就可以看到,普通的Query或者CURD等都不继续缓存了,只有咱们特定的 getBlogs()方法,带有缓存特性的才可以

image

**

当然,这里还有一个小问题,就是所有的方法还是走的切面,只是增加了过滤验证,大家也可以直接把那些需要的注入,不需要的干脆不注入Autofac容器,我之所以需要都经过的目的,就是想把它和日志结合,用来记录Service层的每一个请求,包括CURD的调用情况。

二、什么是Redis,为什么使用它

我个人有一个理解,关于Session或Cache等,在普通单服务器的项目中,很简单,有自己的生命周期等,想获取Session就获取,想拿啥就拿傻,但是在大型的分布式集群中,有可能这一秒的点击的页面和下一秒的都不在一个服务器上,对不对!想想如果普通的办法,怎么保证session的一致性,怎么获取相同的缓存数据,怎么有效的进行消息队列传递?

这个时候就用到了Redis,这些内容,网上已经到处都是,但是还是做下记录吧

Redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。它内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区。在此基础上,Redis支持各种不同方式的排序。为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

也就是说,缓存服务器如果意外重启了,数据还都在,嗯!这就是它的强大之处,不仅在内存高吞吐,还能持久化。

Redis支持主从同步。数据可以从主服务器向任意数量的从服务器上同步,从服务器可以是关联其他从服务器的主服务器。这使得Redis可执行单层树复制。存盘可以有意无意的对数据进行写操作。由于完全实现了发布/订阅机制,使得从数据库在任何地方同步树时,可订阅一个频道并接收主服务器完整的消息发布记录。同步对读取操作的可扩展性和数据冗余很有帮助。

Redis也是可以做为消息队列的,与之相同功能比较优秀的就是Kafka

Redis还是有自身的缺点:

Redis只能存储key/value类型,虽然value的类型可以有多种,但是对于关联性的记录查询,没有Sqlserver、Oracle、Mysql等关系数据库方便。
Redis内存数据写入硬盘有一定的时间间隔,在这个间隔内数据可能会丢失,虽然后续会介绍各种模式来保证数据丢失的可能性,但是依然会有可能,所以对数据有严格要求的不建议使用Redis做为数据库。

关于Redis的使用,看到网上一个流程图:

1、保存数据不经常变化

image

2、如果数据经常变化,就需要取操作Redis和持久化数据层的动作了,保证所有的都是最新的,实时更新Redis 的key到数据库,data到Redis中,但是要注意高并发

三、Redis的安装和调试使用

**1.下载最新版redis,选择.msi安装版本,或者.zip免安装 **(我这里是.msi安装)

下载地址:https://github.com/MicrosoftArchive/redis/releases

image

2.双击执行.msi文件,一路next,中间有一个需要注册服务,因为如果不注册的话,把启动的Dos窗口关闭的话,Redis就中断连接了。

image

3.如果你是免安装的,需要执行以下语句

启动命令:redis-server.exe redis.windows.conf

注册服务命令:redis-server.exe --service-install redis.windows.conf

去服务列表查询服务,可以看到redis服务默认没有开启,开启redis服务(可以设置为开机自动启动)

image

还有要看Redis服务是否开启

image

更新:这里有个小插曲,如果你第一次使用,可以修改下 Redis 的默认端口 6079 ,之前有报导说可能存在被攻击的可能性,不过个人开发,我感觉无可厚非。知道有这个事儿即可。

四、创建appsettings.json数据获取类

如果你对.net 获取app.config或者web.config得心应手的话,在.net core中就稍显吃力,因为不支持直接对Configuration的操作,

1、appsettings.json文件配置参数
前几篇文章中有一个网友说了这样的方法,在Starup.cs中的ConfigureServices方法中,添加

Blog.Core.Repository.BaseDBConfig.ConnectionString = Configuration.GetSection("AppSettings:SqlServerConnection").Value;

当然这是可行的,只不过,如果配置的数据很多,比如这样的,那就不好写了。

 

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  //用户配置信息
  "AppSettings": {
    //Redis缓存
    "RedisCaching": {
      "Enabled": true,
      "ConnectionString": "127.0.0.1:6379"
    },
    //数据库配置
    "SqlServer": {
      "SqlServerConnection": "Server=.;Database=WMBlogDB;User ID=sa;Password=123;",
      "ProviderName": "System.Data.SqlClient"
    },
    "Date": "2018-08-28",
    "Author": "Blog.Core"
  }
}

当然,我受到他的启发,简单做了下处理,大家看看是否可行

1、创建 appsettings 帮助类
在Blog.Core.Common类库中,新建Helper文件夹,新建Appsettings.cs操作类,然后引用 Microsoft.Extensions.Configuration.Json 的Nuget包

 

/// <summary>
    /// appsettings.json操作类
    /// </summary>
    public class Appsettings
    {
        static IConfiguration Configuration { get; set; }
        static Appsettings()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载
            Configuration = new ConfigurationBuilder()
            .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
            .Build();
        }
        /// <summary>
        /// 封装要操作的字符
        /// </summary>
        /// <param name="sections"></param>
        /// <returns></returns>
        public static string app(params string[] sections)
        {
            try
            {
                var val = string.Empty;
                for (int i = 0; i < sections.Length; i++)
                {
                    val += sections[i] + ":";
                }

                return Configuration[val.TrimEnd(':')];
            }
            catch (Exception)
            {
                return "";
            }

        }
    }

2、按照规则获取指定参数
如何使用呢,直接引用类库,传递想要的参数就行(这里对参数是有顺序要求的,这个顺序就是json文件中的层级)

 

/// <summary>
   /// 获取博客列表
   /// </summary>
   /// <returns></returns>
   [HttpGet]
   [Route("GetBlogs")]
   public async Task<List<BlogArticle>> GetBlogs()
   {
       var connect=Appsettings.app(new string[] { "AppSettings", "RedisCaching" , "ConnectionString" });//按照层级的顺序,依次写出来

       return await blogArticleServices.getBlogs();
   }

3、将Appsettings.json添加到bin生成文件中

如果直接运行,会报错,提示没有权限,
操作:右键appsettings.json =》 属性 =》 Advanced =》 复制到输出文件夹 =》 永远复制 =》应用,保存

image

4、运行项目,查看效果

image

五、基于Controller的Redis缓存

1、自定义序列化帮助类
在Blog.Core.Common的Helper文件夹中,添加SerializeHelper.cs 对象序列化操作,以后再扩展

 

using Newtonsoft.Json;
using System.Text;

namespace Blog.Core.Common.Helper
{
    public class SerializeHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static byte[] Serialize(object item)
        {
            var jsonString = JsonConvert.SerializeObject(item);
            return Encoding.UTF8.GetBytes(jsonString);
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TEntity Deserialize<TEntity>(byte[] value)
        {
            if(value == null)
            {
                return default(TEntity);
            }
            var jsonString = Encoding.UTF8.GetString(value);
            return JsonConvert.DeserializeObject<TEntity>(jsonString);
        }
    }
}

image.png

2、定义Redis接口和实现类

在Blog.Core.Common类库中,新建Redis文件夹,新建IRedisCacheManager接口和RedisCacheManager类,并引用Nuget包StackExchange.Redis

 

namespace Blog.Core.Common
{
    /// <summary>
    /// Redis缓存接口
    /// </summary>
    public interface IRedisCacheManager
    {

        //获取 Reids 缓存值
        string GetValue(string key);

        //获取值,并序列化
        TEntity Get<TEntity>(string key);

        //保存
        void Set(string key, object value, TimeSpan cacheTime);

        //判断是否存在
        bool Get(string key);

        //移除某一个缓存值
        void Remove(string key);

        //全部清除
        void Clear();
    }
}

因为在开发的过程中,通过ConnectionMultiplexer频繁的连接关闭服务,是很占内存资源的,所以我们使用单例模式来实现:

这里要引用 Redis 依赖,现在的在线项目已经把这个类迁移到了Common 层,大家知道怎么用就行。

image

添加nuget包后,然后引用
using StackExchange.Redis;

 

using Blog.Core.Common.Helper;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace Blog.Core.Common.Redis
{
    class RedisCacheManager : IRedisCacheManager
    {
        private readonly string redisConnectionString;
        public volatile ConnectionMultiplexer redisConnection;
        private readonly object redisConnectionLock = new object();

        public RedisCacheManager()
        {
            // 获取连接字符串
            string redisConfiguration = Appsettings.app(new string[] { "AppSettings", "RedisCaching", "ConnectionString" });
            if (string.IsNullOrWhiteSpace(redisConfiguration))
            {
                throw new ArgumentException("redis config is empty",nameof(redisConfiguration));
            }
            this.redisConnectionString = redisConfiguration;
            this.redisConnection = GetRedisConnection();
        }

        /// <summary>
        /// 核心代码,获取连接实例
        /// 通过双if 夹lock的方式,实现单例模式
        /// </summary>
        /// <returns></returns>
        private ConnectionMultiplexer GetRedisConnection()
        {
            // 如果已经连接实例,直接返回
            if(this.redisConnection != null && this.redisConnection.IsConnected)
            {
                return this.redisConnection;
            }
            // 加锁,防止异步编程中,出现单例无效的问题
            lock(redisConnectionLock)
            {
                if(this.redisConnection != null)
                {
                    // 释放redis连接
                    this.redisConnection.Dispose();
                }
                try
                {
                    this.redisConnection = ConnectionMultiplexer.Connect(redisConnectionString);
                }
                catch (Exception)
                {
                    throw new Exception("Redis服务未启用,请开启该服务");
                }
            }
            return this.redisConnection;
        }

        public void Clear()
        {
            foreach(var endPoint in this.GetRedisConnection().GetEndPoints())
            {
                var server = this.GetRedisConnection().GetServer(endPoint);
                foreach(var key in server.Keys())
                {
                    redisConnection.GetDatabase().KeyDelete(key);
                }
            }
        }

        public TEntity Get<TEntity>(string key)
        {
            var value = redisConnection.GetDatabase().StringGet(key);
            if (value.HasValue)
            {
                // 需要用的反序列化,将Redis存储的Byte[],进行反序列化
                return SerializeHelper.Deserialize<TEntity>(value);
            }
            else
            {
                return default(TEntity);
            }
        }

        public bool Get(string key)
        {
            return redisConnection.GetDatabase().KeyExists(key);
        }

        public string GetValue(string key)
        {
            return redisConnection.GetDatabase().StringGet(key);
        }

        public void Remove(string key)
        {
            redisConnection.GetDatabase().KeyDelete(key);
        }

        public void Set(string key, object value, TimeSpan cacheTime)
        {
            if(value != null)
            {
                // 序列化,将object值生成RedisValue
                redisConnection.GetDatabase().StringSet(key, SerializeHelper.Serialize(value), cacheTime);
            }
        }

        public bool SetValue(string key, byte[] value)
        {
            return redisConnection.GetDatabase().StringSet(key, value, TimeSpan.FromSeconds(120));
        }
    }
}

代码还是很简单的,网上都有很多资源,就是普通的CURD

image

3、将Redis服务注入到容器中,并在Controller中调用**

将redis接口和类 在ConfigureServices中 进行注入,

services.AddScoped<IRedisCacheManager, RedisCacheManager>();//这里说下,如果是自己的项目,个人更建议使用单例模式 services.AddSingleton

关于为啥我使用了 Scoped 的,可能是想多了,想到了分布式里边了,这里有个博问:Redis多实例创建连接开销的一些疑问?大家自己看看就好,用单例就可以。
注意是构造函数注入,然后在controller中添加代码测试

 

        /// <summary>
        /// 获取博客列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetBlogs")]
        public async Task<List<BlogArticle>> GetBlogs()
        {
            var connect=Appsettings.app(new string[] { "AppSettings", "RedisCaching" , "ConnectionString" });//按照层级的顺序,依次写出来

            List<BlogArticle> blogArticleList = new List<BlogArticle>();

            if (redisCacheManager.Get<object>("Redis.Blog") != null)
            {
                blogArticleList = redisCacheManager.Get<List<BlogArticle>>("Redis.Blog");
            }
            else
            {
                blogArticleList = await blogArticleServices.Query(d => d.bID > 5);
                redisCacheManager.Set("Redis.Blog", blogArticleList, TimeSpan.FromHours(2));//缓存2小时
            }

            return blogArticleList;
        }

4、运行,执行Redis缓存,看到结果

image

六、基于AOP的Redis缓存

1、核心:Redis缓存切面拦截器

在上篇文章中,我们已经定义过了一个拦截器,只不过是基于内存Memory缓存的,并不适应于Redis,上边咱们也说到了Redis必须要存入指定的值,比如字符串,而不能将异步对象 Task<T> 保存到硬盘上,所以我们就修改下拦截器方法,一个专门应用于 Redis 的切面拦截器:

 

using Blog.Core.Common.Attribue;
using Blog.Core.Common.Redis;
using Castle.DynamicProxy;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.Core.API.AOP
{
    /// <summary>
    /// 通过注入的方式,把Redis缓存操作接口通过构造函数注入
    /// </summary>
    public class BlogCacheRedisAOP : BaseCacheAOP
    {

        private readonly IRedisCacheManager _cache;

        public BlogCacheRedisAOP(IRedisCacheManager cache)
        {
            _cache = cache;
        }

        /// <summary>
        /// Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义
        /// </summary>
        /// <param name="invocation"></param>
        public override void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;
            // 对当前方法的特性验证
            var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute;
            if (qCachingAttribute != null)
            {
                // 获取自定义缓存键,这个和Memory内存缓存是一样的,不细说
                var cacheKey = CustomCacheKey(invocation);
                // 核心1:注意这里和之前不同,是获取的string值,之前是object
                var cacheValue = _cache.GetValue(cacheKey);
                if (cacheValue != null)
                {
                    // 将当前获取到的缓存值,赋值给当前执行方法
                    var type = invocation.Method.ReturnType;
                    var resultTypes = type.GenericTypeArguments;
                    if (type.FullName == "System.Void") return;
                    object response;
                    if (type != null && typeof(Task).IsAssignableFrom(type))
                    {
                        // 核心2:返回异步对象Task<T>
                        if (resultTypes.Count() > 0)
                        {
                            var resultType = resultTypes.FirstOrDefault();
                            // 核心3,直接序列化成 dynamic 类型,之前我一直纠结特定的实体
                            dynamic temp = JsonConvert.DeserializeObject(cacheValue, resultType);
                            response = Task.FromResult(temp);
                        }
                        else
                        {
                            // Task 无返回方法 指定时间内不允许重新运行
                            response = Task.Yield();
                        }
                    }
                    else
                    {
                        // 核心4,要进行 ChangeType
                        response = System.Convert.ChangeType(_cache.Get<object>(cacheKey), type);
                    }
                    invocation.ReturnValue = response;
                    return;
                }
                // 去执行当前的方法
                invocation.Proceed();
                // 存入缓存
                if (!string.IsNullOrWhiteSpace(cacheKey))
                {
                    object response;
                    // Type type = invocation.ReturnValue?.GetType();
                    var type = invocation.Method.ReturnType;
                    if (type != null && typeof(Task).IsAssignableFrom(type))
                    {
                        var resultProperty = type.GetProperty("Result");
                        response = resultProperty.GetValue(invocation.ReturnValue);
                    }
                    else
                    {
                        response = invocation.ReturnValue;
                    }
                    if (response == null) response = string.Empty;
                    // 核心5:将获取到指定的response 和特性的缓存时间,进行set操作
                    _cache.Set(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration));
                }
            }
            else
            {
                // 直接执行被拦截方法
                invocation.Proceed();
            }
        }
    }
}

BaseCacheAOP.cs:

 

using Castle.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Blog.Core.API.AOP
{
    public abstract class BaseCacheAOP : IInterceptor
    {
        public abstract void Intercept(IInvocation invocation);

        /// <summary>
        /// 定义缓存键
        /// </summary>
        /// <param name="invication"></param>
        /// <returns></returns>
        protected string CustomCacheKey(IInvocation invication)
        {
            var typeName = invication.TargetType.Name;
            var methodName = invication.Method.Name;

            // 获取参数列表,我最多需要三个即可
            var methodArguments = invication.Arguments.Select(GetArgumentValue).Take(3).ToList();
            string key = $"{typeName}:{methodName}";
            foreach (var param in methodArguments)
            {
                key += $"{param}";
            }
            return key.TrimEnd(':');
        }

        /// <summary>
        /// object 转 string
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        protected string GetArgumentValue(object arg)
        {
            if (arg is int || arg is long || arg is string)
                return arg.ToString();
            if (arg is DateTime)
                return ((DateTime)arg).ToString("yyyyMMddHHmmss");
            return "";
        }
    }
}

上边红色标注的,是和之前不一样的,整体结构还是差不多的,相信都能看的懂的,最后我们就可以很任性的在Autofac容器中,进行任意缓存切换了,是不是很棒!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值