心影(LM)

导航

c# core api 配置redis

1、在appsetting中配置连接字符串

"ConnectionStrings": {
 "RedisConnectionString":"127.0.0.1:6379,password=xxxxx,defaultDatabase=2"
}

 

2、新建Redis帮助类RedisClient.cs;注意:这个时候回报错   直接右键查找最新get包   引用安装就可以 了

 

using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LoveInnPush.API
{
    public class RedisClient
    {
        private static readonly object Locker = new object();

        private ConnectionMultiplexer redisMultiplexer;

        IDatabase db = null;

        private static RedisClient _redisClient = null;
        public static RedisClient redisClient
        {
            get
            {
                if (_redisClient == null)
                {
                    lock (Locker)
                    {
                        if (_redisClient == null)
                        {
                            _redisClient = new RedisClient();

                        }
                    }
                }
                return _redisClient;
            }
        }

        public void InitConnect(IConfiguration Configuration)
        {
            try
            {
                var RedisConnection = Configuration.GetConnectionString("RedisConnectionString");
                redisMultiplexer = ConnectionMultiplexer.Connect(RedisConnection);
                db = redisMultiplexer.GetDatabase();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                redisMultiplexer = null;
                db = null;
            }
        }

        public RedisClient()
        {

        }

        /// <summary>
        /// 保存单个key value
        /// </summary>
        /// <param name="value">保存的值</param>
        /// <param name="expiry">过期时间</param>
        public bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
        {
            return db.StringSet(key, value, expiry);
        }

        /// <summary>
        /// 保存一个对象
        /// </summary>
        /// <param name="obj"></param>
        public bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
        {
            if (db == null)
            {
                return false;
            }
            string json = JsonConvert.SerializeObject(obj);
            return db.StringSet(key, json, expiry);
        }

        /// <summary>
        /// 获取单个key的值
        /// </summary>
        public RedisValue GetStringKey(string key)
        {
            return db.StringGet(key);
        }

        /// <summary>
        /// 获取一个key的对象
        /// </summary>
        public T GetStringKey<T>(string key)
        {
            if (db == null)
            {
                return default;
            }
            var value = db.StringGet(key);
            if (value.IsNullOrEmpty)
            {
                return default;
            }
            return JsonConvert.DeserializeObject<T>(value);
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="key">key值</param>
        public bool Remove(string key)
        {
            return db.KeyDelete(key);
        }

        /// <summary>
        /// 清空
        /// </summary>
        //public void Clear()
        //{
        //    foreach (var endPoint in db.GetEndPoints())
        //    {
        //        var server = db.GetServer(endPoint);
        //        foreach (var key in server.Keys())
        //        {
        //            db.KeyDelete(key);
        //        }
        //    }
        //}

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="key">key值</param>
        public bool Exist(string key)
        {
            return db.KeyExists(key);
        }
    }
}

  

3、在startup.cs文件的ConfigureServices中注入Redis      

RedisClient.redisClient.InitConnect(Configuration);

  

4、调用测试

RedisClient.redisClient.SetStringKey("key4", "value4", TimeSpan.FromHours(8));
RedisClient.redisClient.Remove("key1");

  勿看他人拿高薪,且看闲时谁再拼

posted on 2020-08-18 10:12  心影(LM)  阅读(319)  评论(0编辑  收藏  举报