2025-04-24 18:31:27 +08:00

208 lines
5.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyCode.Project.Infrastructure.Common;
using MyCode.Project.Infrastructure.Extensions;
namespace MyCode.Project.Infrastructure.Cache
{
public class RedisCache : IMyCodeCacheService
{
private static ConnectionMultiplexer _connectionMultiplexer;
private static IDatabase _database;
/// <summary>
/// Redis缓存前缀
/// </summary>
private static string _prefix = "";
public RedisCache(string address, string prefix)
{
if (_connectionMultiplexer == null)
{
_connectionMultiplexer = ConnectionMultiplexer.Connect(address);
_database = _connectionMultiplexer.GetDatabase(0);
_prefix = prefix;
}
}
#region GetCacheKey(key)
private string GetCacheKey(string key)
{
return _prefix + key;
}
#endregion
#region SetInt()
/// <summary>
/// 设置一个整型缓存
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public void SetIncr(string key, int value)
{
_database.StringSet(GetCacheKey(key), value);
}
#endregion
#region Incr(1)
/// <summary>
/// 自增1返回自增后的值
/// </summary>
public long Incr(string key)
{
var result= _database.StringIncrement(GetCacheKey(key));
_database.KeyExpire(GetCacheKey(key),new TimeSpan(1,0,0,0));
return result;
}
#endregion
#region KeyExpire()
/// <summary>
/// 设置缓存有效时长
/// </summary>
/// <param name="key"></param>
/// <param name="expiry"></param>
public void KeyExpire(string key ,TimeSpan? expiry)
{
if (expiry.HasValue)
{
_database.KeyExpire(GetCacheKey(key), expiry);
}
}
#endregion
#region Decr(1)
/// <summary>
/// 递减1返回递减后的值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public long Decr(string key)
{
return _database.StringDecrement(GetCacheKey(key));
}
#endregion
#region Get(key得到对象)
public object Get(string key)
{
var cacheValue = _database.StringGet(GetCacheKey(key));
if (cacheValue.HasValue)
{
var value = JsonHelper.ToObject(cacheValue);
if (value != null)
return value;
else
return cacheValue;
}
return null;
}
#endregion
#region Get(key的值)
public T Get<T>(string key)
{
var value = Get(key);
if (value == null) { return default(T); }
return (T)value;
}
#endregion
#region Set()
public void Set(string key, object data, TimeSpan? cacheTime)
{
if (cacheTime == null || cacheTime.Value == TimeSpan.Zero) { cacheTime = new TimeSpan(0, 0, 30); }
if (data == null) { return; }
try
{
_database.StringSet(GetCacheKey(key), JsonHelper.ObjectToByte(data), cacheTime);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region Remove(key删除)
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
public void Delete(string key)
{
_database.KeyDelete(GetCacheKey(key), CommandFlags.HighPriority);
}
#endregion
#region Exists(key是否存在)
/// <summary>
/// 判断key是否存在
/// </summary>
public bool Exists(string key)
{
return _database.KeyExists(GetCacheKey(key));
}
#endregion
#region Push()
public void Push(string key, object data, TimeSpan? timeSpan = null)
{
//,TimeSpan? timeSpan = null
_database.ListRightPush(GetCacheKey(key), JsonHelper.ObjectToByte(data));
if(timeSpan!=null)
_database.KeyExpire(GetCacheKey(key), timeSpan);
}
#endregion
#region Push()
public void Push(string key, object data )
{
//,TimeSpan? timeSpan = null
_database.ListRightPush(GetCacheKey(key), JsonHelper.ObjectToByte(data));
}
#endregion
#region Popup()
public object Popup(string key)
{
var cacheValue = _database.ListLeftPop(GetCacheKey(key));
if (cacheValue.HasValue)
{
try
{
var value = JsonHelper.ToObject(cacheValue);
return value;
}
catch (Exception ex)
{
return cacheValue;
}
}
return null;
}
#endregion
}
}