using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SiteCore
{
public class RedisHelper
{
#region
//执行顺序---静态字段---静态构造函数---构造函数
private static ConnectionMultiplexer redis;
static RedisHelper()
{
if (redis == null || redis.IsConnected)
{
redis = ConnectionMultiplexer.Connect("127.0.0.1:6379, password=abc123abc,allowAdmin=true");
}
}
public void Clear()
{
var endpoints = redis.GetEndPoints(true);
foreach (var endpoint in endpoints)
{
var server = redis.GetServer(endpoint);
server.FlushAllDatabases();
}
}
#endregion
public static void SetKeyValue(object key, object v)
{
if (redis.GetDatabase().StringGet(key.ToString()) == RedisValue.Null)
{
redis.GetDatabase().StringSet(key.ToString(), v.ToString(), TimeSpan.FromDays(1));
}
else
{
redis.GetDatabase().StringIncrement(key.ToString(), 1);
}
}
public static bool HasKey(string key)
{
return redis.GetDatabase().KeyExists(key);
}
#region redis 字符串(string)操作
///
/// 设置指定键的值
///
///
///
///
public static bool StringSet(string key, string value)
{
return redis.GetDatabase().StringSet(key, value);
}
///
/// 获取指定键的值
///
///
///
public static object StringGet(string key)
{
RedisValue rv = redis.GetDatabase().StringGet(key);
if (rv == RedisValue.Null) return null;
return rv;
}
///
/// 获取存储在键上的字符串的子字符串
///
///
///
///
///
public static object StringGet(string key, int start, int end)
{
return redis.GetDatabase().StringGetRange(key, start, end);
}
///
/// 设置键的字符串值并返回其旧值
///
///
///
///
public static object StringGetAndSet(string key, string value)
{
return redis.GetDatabase().StringGetSet(key, value);
}
///
/// 返回在键处存储的字符串值中偏移处的位值
///
///
///
///
public static bool StringGetBit(string key, long offset)
{
return redis.GetDatabase().StringGetBit(key, offset);
}
///
/// 获取所有给定键的值
///
///
///
public static List