微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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;
  }
 
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

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

相关推荐