当前位置: 代码网 > it编程>编程语言>C# > C#实现封装常用Redis工具类的示例代码

C#实现封装常用Redis工具类的示例代码

2024年05月19日 C# 我要评论
1.请先安装csrediscore接口:namespace tools.redis{ public interface iredistool { bool setlongva

1.请先安装csrediscore

接口:

namespace tools.redis
{
    public interface iredistool
    {
        bool setlongvalue(string key, string value);
        bool setvalue(string key, string value, int outsecond);
        bool setvalue(string key, string value);
        bool exists(string key);
        bool updatevalue(string key, string value);
        string? getvalue(string key);
        t? getvalue<t>(string key);
        t? getentity<t>(string key);
        list<t>? getlike<t>(string key);
        void deletekey(string key);
        void deletelike(string key);

    }
}

实现接口方法

using csredis;
using newtonsoft.json;
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace tools.redis
{
    public class redistool : iredistool
    {
        csredisclient csredis;

        public redistool(string redisconfig)
        {
            csredis = new csredisclient(redisconfig);
            redishelper.initialization(csredis);
        }

        /// <summary>
        /// 设置长时间存在的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool setlongvalue(string key, string value)
        {
            try
            {
                csredis.set(key, value);
                return true;
            }
            catch (exception ex)
            {
                nloghelper.error("redisdatahelper-setvalue" + ex.message);
                return false;
            }
        }

        /// <summary>
        /// 设置值,并设置清除时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="outsecond"></param>
        /// <returns></returns>
        public bool setvalue(string key, string value, int outsecond)
        {
            try
            {
                csredis.set(key, value, outsecond);
                return true;
            }
            catch (exception ex)
            {
                nloghelper.error("redisdatahelper-setvalue" + ex.message);
                return false;
            }
        }

        /// <summary>
        /// 设置值,存在则覆盖,并沿用之前的清除时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool setvalue(string key, string value)
        {
            try
            {
                if (csredis.exists(key))
                {
                    long time = csredis.ttl(key);
                    csredis.set(key, value, convert.toint32(time));
                }
                else
                    csredis.set(key, value);

                return true;
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-setvalue[{key}-{value}]" + ex.message);
                return false;
            }
        }

        /// <summary>
        /// 是否存在key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool exists(string key)
        {
            try
            {
                return csredis.exists(key);
            }
            catch (exception ex)
            {
                nloghelper.error("redisdatahelper-keyexists" + ex.message);
                return false;
            }
        }

        /// <summary>
        /// 更新key,把自动注销时间设置为原来的key的时间,不存在返回false
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool updatevalue(string key, string value)
        {
            try
            {
                if (csredis.exists(key))
                {
                    long time = csredis.ttl(key);
                    csredis.set(key, value, convert.toint32(time));
                    return true;
                }
                return false;
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-setvalue[{key}-{value}]" + ex.message);
                return false;
            }
        }


        public string? getvalue(string key)
        {
            try
            {
                return csredis.get(key);
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-getvalue[{key}]" + ex.message);
                return null;
            }
        }

        /// <summary>
        /// 获得json序列化后的
        /// </summary>
        /// <typeparam name="t"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public t? getvalue<t>(string key)
        {
            try
            {
                var data = csredis.get(key);
                return jsonconvert.deserializeobject<t>(data);
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-getvalue[{key}]" + ex.message);
                return default;
            }
        }

        public t? getentity<t>(string key)
        {
            try
            {
                var data = csredis.get(key);
                return jsonconvert.deserializeobject<t>(data);
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-getlist[{key}]" + ex.message);
                return default;
            }
        }

        public list<t>? getlike<t>(string key)
        {
            try
            {
                var datalist = csredis.keys(key + "*");
                list<t> list = new list<t>();
                foreach (string item in datalist)
                {
                    var data = getentity<t>(item);
                    if (data != null)
                    {
                        list.add(data);
                    }
                }
                return list;
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-getlist[{key}]" + ex.message);
                return default;
            }
        }

        public void deletekey(string key)
        {
            try
            {
                csredis.del(key);
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-deletekey[{key}]" + ex.message);
            }
        }

        public void deletelike(string key)
        {
            try
            {
                var datalist = csredis.keys(key + "*");

                foreach (string item in datalist)
                {
                    deletekey(item);
                }
            }
            catch (exception ex)
            {
                nloghelper.error($"redisdatahelper-deletelike[{key}]" + ex.message);
            }
        }


        private bool acquirelock(string lockkey, string lockvalue, int locktimeoutseconds)
        {
            // 尝试获取锁
            bool lockacquired = csredis.setnx(lockkey, lockvalue);

            // 如果成功获取锁,设置锁的超时时间
            if (lockacquired)
            {
                csredis.expire(lockkey, locktimeoutseconds);
            }

            return lockacquired;
        }

        private void releaselock(string lockkey, string lockvalue)
        {
            // 释放锁
            // 使用 lua 脚本确保只有持有锁的客户端才能释放锁
            string luascript = @"
            if redis.call('get', keys[1]) == argv[1] then
                return redis.call('del', keys[1])
            else
                return 0
            end";

            csredis.eval(luascript, lockkey, new[] { lockvalue });
        }
    }
}

以上就是c#实现封装常用redis工具类的示例代码的详细内容,更多关于c#封装常用redis的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com