251 lines
7.0 KiB
C#
251 lines
7.0 KiB
C#
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Nirvana.Common
|
|
{
|
|
public static class RedisHelpers
|
|
{
|
|
private static object cacheLocker = new object();//缓存锁对象
|
|
private static ICache cache = null;//缓存接口
|
|
static RedisHelpers()
|
|
{
|
|
Load();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载缓存
|
|
/// </summary>
|
|
/// <exception cref=""></exception>
|
|
private static void Load()
|
|
{
|
|
try
|
|
{
|
|
cache = new Cache();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// Log.Error(ex.Message);
|
|
}
|
|
}
|
|
|
|
public static ICache GetCache()
|
|
{
|
|
return cache;
|
|
}
|
|
|
|
/// 缓存过期时间
|
|
/// </summary>
|
|
public static int TimeOut
|
|
{
|
|
get
|
|
{
|
|
return cache.TimeOut;
|
|
}
|
|
set
|
|
{
|
|
lock (cacheLocker)
|
|
{
|
|
cache.TimeOut = value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获得指定键的缓存值
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <returns>缓存值</returns>
|
|
public static object Get(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
return null;
|
|
return cache.Get(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获得指定键的缓存值
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <returns>缓存值</returns>
|
|
public static T Get<T>(string key)
|
|
{
|
|
return cache.Get<T>(key);
|
|
}
|
|
|
|
public static T stringGet<T>(string key)
|
|
{
|
|
return cache.stringGet<T>(key);
|
|
}
|
|
|
|
public static string stringGet(string key)
|
|
{
|
|
return stringGet<string>(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将指定键的对象添加到缓存中
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <param name="data">缓存值</param>
|
|
public static void Insert(string key, object data)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key) || data == null)
|
|
return;
|
|
//lock (cacheLocker)
|
|
{
|
|
cache.Insert(key, data);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 将指定键的对象添加到缓存中
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <param name="data">缓存值</param>
|
|
public static void Insert<T>(string key, T data)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key) || data == null)
|
|
return;
|
|
//lock (cacheLocker)
|
|
{
|
|
cache.Insert<T>(key, data);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 将指定键的对象添加到缓存中,并指定过期时间
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <param name="data">缓存值</param>
|
|
/// <param name="cacheTime">缓存过期时间(秒)</param>
|
|
public static void Insert(string key, object data, int cacheTime)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(key) && data != null)
|
|
{
|
|
//lock (cacheLocker)
|
|
{
|
|
cache.Insert(key, data, cacheTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将指定键的对象添加到缓存中,并指定过期时间
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <param name="data">缓存值</param>
|
|
/// <param name="cacheTime">缓存过期时间(秒)</param>
|
|
public static void Insert<T>(string key, T data, int cacheTime)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(key) && data != null)
|
|
{
|
|
//lock (cacheLocker)
|
|
{
|
|
cache.Insert<T>(key, data, cacheTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将指定键的对象添加到缓存中,并指定过期时间
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <param name="data">缓存值</param>
|
|
/// <param name="cacheTime">缓存过期时间</param>
|
|
public static void Insert(string key, object data, DateTime cacheTime)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(key) && data != null)
|
|
{
|
|
//lock (cacheLocker)
|
|
{
|
|
cache.Insert(key, data, cacheTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将指定键的对象添加到缓存中,并指定过期时间
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
/// <param name="data">缓存值</param>
|
|
/// <param name="cacheTime">缓存过期时间</param>
|
|
public static void Insert<T>(string key, T data, DateTime cacheTime)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(key) && data != null)
|
|
{
|
|
//lock (cacheLocker)
|
|
{
|
|
cache.Insert<T>(key, data, cacheTime);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从缓存中移除指定键的缓存值
|
|
/// </summary>
|
|
/// <param name="key">缓存键</param>
|
|
public static void Remove(string key)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
return;
|
|
lock (cacheLocker)
|
|
{
|
|
cache.Remove(key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断key是否存在
|
|
/// </summary>
|
|
public static bool Exists(string key)
|
|
{
|
|
return cache.Exists(key);
|
|
}
|
|
|
|
#region Hash操作
|
|
public static bool HashExists(string key, string field)
|
|
{
|
|
return cache.HashExists(key, field);
|
|
}
|
|
|
|
public static bool HashDelete(string key, string field)
|
|
{
|
|
return cache.HashDelete(key, field);
|
|
}
|
|
|
|
public static bool HashSet(string key, string field, string value)
|
|
{
|
|
return cache.HashSet(key, field, value);
|
|
}
|
|
|
|
public static void HashSet(string key, IEnumerable<HashEntry> hashFields)
|
|
{
|
|
cache.HashSet(key, hashFields);
|
|
}
|
|
|
|
public static object HashGet(string key, string field)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
return null;
|
|
return cache.HashGet(key, field);
|
|
}
|
|
|
|
public static List<T> HashValues<T>(string key)
|
|
{
|
|
List<T> result = new List<T>();
|
|
RedisValue[] arr = cache.HashValues(key);
|
|
foreach (var item in arr)
|
|
{
|
|
if (!item.IsNullOrEmpty)
|
|
{
|
|
result.Add(JsonConvert.DeserializeObject<T>(item));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
#endregion
|
|
}
|
|
}
|