using System;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using System.Collections;
namespace CallCenter.Utility
{
public class RedisHelper
{
#region
//执行顺序---静态字段---静态构造函数---构造函数
private static ConnectionMultiplexer redis;
private static IDatabase database;
static RedisHelper()
{
if (redis == null || !redis.IsConnected)
{
var config = new ConfigurationOptions
{
AbortOnConnectFail = false,
AllowAdmin = true,
ConnectTimeout = 15000,
SyncTimeout = 5000,
ResponseTimeout = 15000,
Password = "",//Redis数据库密码
EndPoints = { Configs.GetValue("Redis_Server") + ":" + Configs.GetValue("Redis_Port") }// connectionString 为IP:Port 如”192.168.2.110:6379”
};
redis = ConnectionMultiplexer.Connect(config);
//redis = ConnectionMultiplexer.Connect(Configs.GetValue("Redis_Server")+":"+Configs.GetValue("Redis_Port"));
//redis = ConnectionMultiplexer.Connect("192.168.4.18, abortConnect=false");
database = redis.GetDatabase();
}
}
#endregion
#region redis 字符串(string)操作
///
/// 设置指定键的值
///
///
///
///
public static bool StringSet(string key, string value)
{
return database.StringSet(key, value);
}
///
/// 获取指定键的值
///
///
///
public static object StringGet(string key)
{
if (string.IsNullOrEmpty(key))
{
return null;
}
else
{
var rt = database.StringGet(key);
if (rt.IsNull)
{
return null ;
}
else
{
return rt;
}
}
}
///
/// 获取存储在键上的字符串的子字符串
///
///
///
///
///
public static object StringGet(string key, int start, int end)
{
var rt = database.StringGetRange(key, start, end);
if (rt.IsNull)
{
return null;
}
else
{
return rt;
}
}
///
/// 设置键的字符串值并返回其旧值
///
///
///
///
public static object StringGetAndSet(string key, string value)
{
var rt = database.StringGetSet(key, value);
if (rt.IsNull)
{
return null;
}
else
{
return rt;
}
}
///
/// 返回在键处存储的字符串值中偏移处的位值
///
///
///
///
public static bool StringGetBit(string key, long offset)
{
return database.StringGetBit(key, offset);
}
///
/// 获取所有给定键的值
///
///
///
public static List