欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现redis读写的方法

程序员文章站 2023-10-22 16:14:27
最近做一个c#项目,需要对radis进行读写。 首先引入system.configuration,如下 实现代码如下: public class manu...

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

首先引入system.configuration,如下

C#实现redis读写的方法

实现代码如下:

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

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