最近做一个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 ;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
dy("nrwz");