栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

.net core 操作redis string类型的封装

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.net core 操作redis string类型的封装

安装依赖包

StackExchange.Redis
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core.Abstractions;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;
using Microsoft.Extensions.Options;
using CoreRedis.Config;
using Newtonsoft.Json;

namespace CoreRedis.RedisDataType
{
    public class RedisString
    {
        private static readonly object _lock = new object();

        private ConnectionMultiplexer redisMultiplexer;

        //private static RedisString _redisClient = null;

        private readonly RedisConfig _redisConfig;

        IDatabase db = null;

        //public static RedisString RedisClient
        //{
        //    get
        //    {
        //        if (_redisClient == null)
        //        {
        //            lock(_lock)
        //            {
        //                _redisClient = new RedisString();
        //            }
        //        }
        //        return _redisClient;
        //    }
        //}

        public RedisString(IOptionsMonitor optionsSnapshot)
        {
            _redisConfig = optionsSnapshot.CurrentValue;
            //_redisConfig = optionsSnapshot;
            var RedisConnection = _redisConfig.Value;
            redisMultiplexer = ConnectionMultiplexer.Connect(RedisConnection);
            db = redisMultiplexer.GetDatabase();
        }


        //添加一个key 或者覆盖key的值
        public async Task> SetKey(string key, string value)
        {
            return db.StringSetAsync(key, value, null, false, StackExchange.Redis.When.Always);
        }


        //只有在键不存在的时候才设置
        public async Task> SetKeyNx(string key, string value)
        {
            return db.StringSetAsync(key, value, null, false, When.NotExists);
        }
 
        /// 
        /// 获取一个key的对象
        /// 
        public async Task GetStringKey(string key)
        {
            if (db == null)
            {
                return default;
            }
            var value = await db.StringGetAsync(key);
            if (value.IsNullOrEmpty)
            {
                return default;
            }
            return JsonConvert.DeserializeObject(value);
        }


        //getset一个key,获取旧值并设置新值
        public async Task GetSetKey(string key,string value)
        {
            var oldvalue = await db.StringGetSetAsync(key, value);

            return JsonConvert.DeserializeObject(oldvalue);
        }


        //mset 一次设置多个数据
        public async Task> MSET(KeyValuePair[] values )
        {
            return db.StringSetAsync(values, When.NotExists);
        }

        //mget 一次获取多个数据
        public async Task> MGET(RedisKey[] keys)
        {
            return db.StringGetAsync(keys);
        }


        //获取string的length

        public async Task> KeyLen(string key)
        {
            return db.StringLengthAsync(key);
        }


        //append

        public async Task> Append(string key , string appendvalue)
        {
            return db.StringAppendAsync(key, appendvalue);
        }

        //get range

        public async Task> GetRange(string key , int start, int end)
        {
            return db.StringGetRangeAsync(key, start, end);
        }


        //增加
        public async Task> Incr(string key,int count)
        {
            return db.StringIncrementAsync(key, count);
        }

        //减少
        public async Task> Decr(string key,int count)
        {
            return db.StringDecrementAsync(key, count);
        }


        #region 下面注释的是redis.core的相关方法



        //private IRedisClient _redisClient;


        //public RedisString(IRedisClient redisClient)
        //{
        //    _redisClient = redisClient;
        //}


        set key value

        //public async Task> setKey(string key,string value)
        //{
        //    return _redisClient.Db0.AddAsync(key, value);
        //}

        set key value nx
        //public async Task> setkeyNx(string key,string value)
        //{
        //    return _redisClient.Db0.AddAsync(key, value,StackExchange.Redis.When.NotExists);
        //}

        set key value nx
        //public async Task> setkeyXx(string key, string value)
        //{
        //    return _redisClient.Db0.AddAsync(key, value, StackExchange.Redis.When.Exists);
        //}

        //public async Task> GetKey(string key)
        //{
        //    return _redisClient.Db0.GetAsync(key);
        //}

        //public async Task> MSET()
        //{
        //    return _redisClient.Db0.AddAllAsync(new Tuple[] { new Tuple("key1", "key1"), new Tuple("key2", "key2") });
        //}

        当所有的键都不存在的时候执行
        //public async Task> MSETNX()
        //{
        //    return _redisClient.Db0.AddAllAsync(new Tuple[] { new Tuple("key1", "1"), new Tuple("key2", "2") },StackExchange.Redis.When.NotExists);
        //}

        //public async Task>> MGET()
        //{
        //    return _redisClient.Db0.GetAllAsync(new string[] { "key1", "key2" });
        //}



        //public async Task> GetSet(string key, string value)
        //{
        //    return _redisClient.Db0.()
        //}

        #endregion
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/894769.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号