zoukankan      html  css  js  c++  java
  • c# 实现redis读写

    最近做一个C#项目,需要对radis进行读写。

    首先引入System.Configuration,如下

    实现代码如下:

    public class ManualSuggestRedisHelper
        {
            private static IRedisClient GetManualSuggestClient()
            {
                var config = ConfigurationManager.ConnectionStrings["REDIS_MANUAL_VIDEO_LIST"].ConnectionString.Split(':');
                if (config.Length == 3)
                {
                    int dbNum = int.Parse(config[2]);
                    return new RedisClient(config[0], int.Parse(config[1]), db: dbNum);
                }
                else
                {
                    return new RedisClient("192.168.86.15", 6379, db: 8);
                }
            }
    
            public static void AddRangeToList(string key, JSONObject value)
            {
                try
                {
                    using (var redis = GetManualSuggestClient())
                    {
                        redis.SetEntry(key, value.ToString());
                    }
                }
                catch (Exception ex)
                {
                    TxtLogger.DumpException(ex);
                }
            }
    
            public static void AddRangeToSuggestList(string key, List<string> value)
            {
                try
                {
                    using (var redis = GetManualSuggestClient())
                    {
                        redis.AddRangeToList(key, value);
                    }
                }
                catch (Exception ex)
                {
                    TxtLogger.DumpException(ex);
                }
            }
    
            public static void Remove(string key)
            {
                try
                {
                    using (var redis = GetManualSuggestClient())
                    {
                        redis.Remove(key);
                    }
                }
                catch (Exception ex)
                {
                    TxtLogger.AppendStringToTextFile("删除redis key存在异常——" + ex);
                }
            }
    
            public static bool ExistsRedis(string key)
            {
                try
                {
                    using (var redis = GetManualSuggestClient())
                    {
                        List<string> isExists = redis.GetAllItemsFromList(key);
                        if (isExists != null && isExists.Count() > 0)
                        {
                            return true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    TxtLogger.DumpException(ex);
                }
                return false;
            }
    
        }
    
  • 相关阅读:
    IO 模型
    进程、线程、锁
    用多线程,实现并发,TCP
    同步锁(互斥锁),GIL锁(解释器层面的锁),死锁与递归锁
    Java项目中的常用的异常2
    JAVA项目中的常用的异常处理情况1
    添加学生信息web界面连接数据库
    jdbc下载路径
    添加学生信息 web界面 并且连接数据库
    正则表达式
  • 原文地址:https://www.cnblogs.com/java-chanjuan/p/6721208.html
Copyright © 2011-2022 走看看