码迷,mamicode.com
首页 > Web开发 > 详细

Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

时间:2018-05-17 18:49:47      阅读:1037      评论:0      收藏:0      [点我收藏+]

标签:service   redis   from   测试的   使用   bool   生产环境   host   pac   

本文目录
1. 摘要
2. Redis配置
3. RedisHelper
4.使用实例 5. 总结

1.  摘要

  由于內存存取速度远高于磁盘读取的特性,为了程序效率提高性能,通常会把常用的不常变动的数据存储在系统内存中,提升数据读取的速度,在C#下常见的内存操作有微软自带的内存处理、分布式缓存Memcached以及Redis,这里我们介绍Redis分布式缓存,另外两种缓存处理参考《Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类》介绍了基于Microsoft.Extensions.Caching.Memory封装CacheHelper缓存帮助类;《Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。》Memcached帮助类。

2.  Redis配置

  在appsettings.json添加相关配置,配置读写redis服务器,多服务器用逗号隔开。

//redis分布式缓存
  "RedisConfig": {
    //是否打开缓存1是0否
    "IsOpenCache": "0",
    "ReadWriteHosts": "192.168.1.2:6379,192.168.1.3:6379,password=123456",
    "ReadOnlyHosts": "192.168.1.2:6379,password=123456"
  }

技术分享图片

3.  RedisHelper

  添加Nuget包StackExchange.Redis和Newtonsoft.Json;设置Key/Value,删除redis缓存。本帮助类我仅本地两台电脑测试过,生产环境暂未使用,现仅供参考。

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;

using StackExchange.Redis;
using Newtonsoft.Json;

namespace NC.Common
{
    public class RedisHelper
    {
        //单例模式
        public static RedisCommon Default{get { return new RedisCommon(); } }
        public static RedisCommon One { get { return new RedisCommon(1, "127.0.0.1:6379"); } }
        public static RedisCommon Two { get { return new RedisCommon(2, "127.0.0.1:6379"); } }
    }
    /// <summary>
    /// Redis操作类
    /// 老版用的是ServiceStack.Redis。
    /// Net Core使用StackExchange.Redis的nuget包
    /// </summary>
    public class RedisCommon
    {
        public static ILogger Log = UtilLogger<RedisCommon>.Log;//日志记录
        //redis数据库连接字符串
        private string _conn = UtilConf.Configuration["RedisConfig:ReadWriteHosts"] ?? "127.0.0.1:6379";
        private int _db = 0;
        //静态变量 保证各模块使用的是不同实例的相同链接
        private static ConnectionMultiplexer connection;
        public RedisCommon() { }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="connectStr"></param>
        public RedisCommon(int db, string connectStr)
        {
            _conn = connectStr;
            _db = db;
        }
        /// <summary>
        /// 缓存数据库,数据库连接
        /// </summary>
        public ConnectionMultiplexer CacheConnection
        {
            get
            {
                try
                {
                    if (connection == null || !connection.IsConnected)
                    {
                        connection = new Lazy<ConnectionMultiplexer>(() => ConnectionMultiplexer.Connect(_conn)).Value;
                    }
                }
                catch (Exception ex)
                {
                    Log.LogError("RedisHelper->CacheConnection 出错\r\n" + ex.ToString());
                    return null;
                }
                return connection;
            }
        }
        /// <summary>
        /// 缓存数据库
        /// </summary>
        public IDatabase CacheRedis => CacheConnection.GetDatabase(_db);

        #region --KEY/VALUE存取--
        /// <summary>
        /// 单条存值
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">The value.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public bool StringSet(string key, string value)
        {
            return CacheRedis.StringSet(key, value);
        }
        /// <summary>
        /// 保存单个key value
        /// </summary>
        /// <param name="key">Redis Key</param>
        /// <param name="value">保存的值</param>
        /// <param name="expiry">过期时间</param>
        /// <returns></returns>
        public bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?))
        {
            return CacheRedis.StringSet(key, value, expiry);
        }
        /// <summary>
        /// 保存多个key value
        /// </summary>
        /// <param name="arr">key</param>
        /// <returns></returns>
        public bool StringSet(KeyValuePair<RedisKey, RedisValue>[] arr)
        {
            return CacheRedis.StringSet(arr);
        }
        /// <summary>
        /// 批量存值
        /// </summary>
        /// <param name="keysStr">key</param>
        /// <param name="valuesStr">The value.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public bool StringSetMany(string[] keysStr, string[] valuesStr)
        {
            var count = keysStr.Length;
            var keyValuePair = new KeyValuePair<RedisKey, RedisValue>[count];
            for (int i = 0; i < count; i++)
            {
                keyValuePair[i] = new KeyValuePair<RedisKey, RedisValue>(keysStr[i], valuesStr[i]);
            }

            return CacheRedis.StringSet(keyValuePair);
        }

        /// <summary>
        /// 保存一个对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
        {
            string json = JsonConvert.SerializeObject(obj);
            return CacheRedis.StringSet(key, json, expiry);
        }
        /// <summary>
        /// 追加值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void StringAppend(string key, string value)
        {
            ////追加值,返回追加后长度
            long appendlong = CacheRedis.StringAppend(key, value);
        }

        /// <summary>
        /// 获取单个key的值
        /// </summary>
        /// <param name="key">Redis Key</param>
        /// <returns></returns>
        public RedisValue GetStringKey(string key)
        {
            return CacheRedis.StringGet(key);
        }
        /// <summary>
        /// 根据Key获取值
        /// </summary>
        /// <param name="key">键值</param>
        /// <returns>System.String.</returns>
        public string StringGet(string key)
        {
            try
            {
                return CacheRedis.StringGet(key);
            }
            catch (Exception ex)
            {
                Log.LogError("RedisHelper->StringGet 出错\r\n" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获取多个Key
        /// </summary>
        /// <param name="listKey">Redis Key集合</param>
        /// <returns></returns>
        public RedisValue[] GetStringKey(List<RedisKey> listKey)
        {
            return CacheRedis.StringGet(listKey.ToArray());
        }
        /// <summary>
        /// 批量获取值
        /// </summary>
        public string[] StringGetMany(string[] keyStrs)
        {
            var count = keyStrs.Length;
            var keys = new RedisKey[count];
            var addrs = new string[count];

            for (var i = 0; i < count; i++)
            {
                keys[i] = keyStrs[i];
            }
            try
            {

                var values = CacheRedis.StringGet(keys);
                for (var i = 0; i < values.Length; i++)
                {
                    addrs[i] = values[i];
                }
                return addrs;
            }
            catch (Exception ex)
            {
                Log.LogError("RedisHelper->StringGetMany 出错\r\n" + ex.ToString());
                return null;
            }
        }
        /// <summary>
        /// 获取一个key的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetStringKey<T>(string key)
        {
            return JsonConvert.DeserializeObject<T>(CacheRedis.StringGet(key));
        }

        #endregion

        #region --删除设置过期--
        /// <summary>
        /// 删除单个key
        /// </summary>
        /// <param name="key">redis key</param>
        /// <returns>是否删除成功</returns>
        public bool KeyDelete(string key)
        {
            return CacheRedis.KeyDelete(key);
        }
        /// <summary>
        /// 删除多个key
        /// </summary>
        /// <param name="keys">rediskey</param>
        /// <returns>成功删除的个数</returns>
        public long KeyDelete(RedisKey[] keys)
        {
            return CacheRedis.KeyDelete(keys);
        }
        /// <summary>
        /// 判断key是否存储
        /// </summary>
        /// <param name="key">redis key</param>
        /// <returns></returns>
        public bool KeyExists(string key)
        {
            return CacheRedis.KeyExists(key);
        }
        /// <summary>
        /// 重新命名key
        /// </summary>
        /// <param name="key">就的redis key</param>
        /// <param name="newKey">新的redis key</param>
        /// <returns></returns>
        public bool KeyRename(string key, string newKey)
        {
            return CacheRedis.KeyRename(key, newKey);
        }
        /// <summary>
        /// 删除hasekey
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public bool HaseDelete(RedisKey key, RedisValue hashField)
        {
            return CacheRedis.HashDelete(key, hashField);
        }
        /// <summary>
        /// 移除hash中的某值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="dataKey"></param>
        /// <returns></returns>
        public bool HashRemove(string key, string dataKey)
        {
            return CacheRedis.HashDelete(key, dataKey);
        }
        /// <summary>
        /// 设置缓存过期
        /// </summary>
        /// <param name="key"></param>
        /// <param name="datetime"></param>
        public void SetExpire(string key, DateTime datetime)
        {
            CacheRedis.KeyExpire(key, datetime);
        }
        #endregion

    }
}

 

4.  使用实例

  在HomeCtroller控制器中添加测试redis方法如下:写;读;删除。

    public IActionResult Index()
        {
            #region --测试redis--
            RedisHelper.Default.StringSet("redis", "redis" + DateTime.Now, TimeSpan.FromSeconds(1000000));
            ViewData["redis"] = RedisHelper.Default.StringGet("redis");
            //RedisHelper.Default.KeyDelete("redis");
            #endregion
            return View();
        }     

5.  总结

  Redis读写速度快性能高,但部署到生产环境中要注意端口、密码设置,最好是部署在内网环境中,另由于之前都是使用ServiceStack.Redis,由于ServiceStack.Redis v4版本后是收费版的,好像还有每小时6000次访问请求的问题需注意,具体这俩请自行搜索对比。Redis 有五种常用数据类型,字符串类型(string),散列类型(hash),列表类型(list),集合类型(set),有序集合类型(zset)参考:https://www.cnblogs.com/mingtianct/p/6291593.html;这里我暂只处理了string类型,上面我测试的方法有限,其他类型各位可自行补充,网上有很多案例。

 

 

Asp.Net Core 2.0 项目实战(6)Redis配置、封装帮助类RedisHelper及使用实例

标签:service   redis   from   测试的   使用   bool   生产环境   host   pac   

原文地址:https://www.cnblogs.com/oorz/p/9052498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!