Files
YunTongJackYunTask/Reportapi/MyCode.Project.Infrastructure/Cache/RedisCache.cs
2025-07-04 09:50:02 +08:00

200 lines
5.3 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;
using MyCode.Project.Infrastructure.Exceptions;
using MyCode.Project.Infrastructure.Constant;
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()
{
if (_connectionMultiplexer == null)
{
throw new BaseException("没有初始化_connectionMultiplexer对象");
}
}
public RedisCache(string address,string prefix)
{
if (_connectionMultiplexer == null)
{
_connectionMultiplexer = ConnectionMultiplexer.Connect(address);
_database = _connectionMultiplexer.GetDatabase(19);
_prefix = prefix;
}
}
#region GetCacheKey(key)
private string GetCacheKey(string key)
{
return _prefix + key;
}
#endregion
#region Incr(1)
/// <summary>
/// 自增1返回自增后的值
/// </summary>
public long Incr(string key,long value = 1)
{
return _database.StringIncrement(GetCacheKey(key),value);
}
#endregion
#region GetIncr(Incr值)
/// <summary>
/// 得到Incr值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public object GetIncr(string key)
{
var cacheValue = _database.StringGet(GetCacheKey(key));
return cacheValue;
}
#endregion
#region GetNewId(id)
/// <summary>
/// 得到新的id
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public long GetNewId()
{
throw new NotImplementedException();
}
#endregion
#region Get(key得到对象)
public object Get(string key)
{
var cacheValue = _database.StringGet(GetCacheKey(key));
if (cacheValue.HasValue)
{
var value = JsonHelper.ToObject(cacheValue);
return value;
}
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 SetIncr()
/// <summary>
/// 初始化原子性的功能
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
public void SetIncr(string key, long data)
{
_database.StringSet(GetCacheKey(key), data, new TimeSpan(365 * 50, 0, 0, 0));
}
#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; }
_database.StringSet(GetCacheKey(key), JsonHelper.ObjectToByte(data), cacheTime);
//_database.StringSet(GetCacheKey(key), JsonHelper.ToJson(data), cacheTime);
}
#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)
{
_database.ListRightPush(GetCacheKey(key), JsonHelper.ObjectToByte(data));
}
#endregion
#region Popup()
public object Popup(string key)
{
var cacheValue = _database.ListLeftPop(GetCacheKey(key));
if (cacheValue.HasValue)
{
var value = JsonHelper.ToObject(cacheValue);
return value;
}
return null;
}
#endregion
#region ListLen()
/// <summary>
/// 返回列表长度
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public long ListLen(string key)
{
key = GetCacheKey(key);
return _database.ListLength(key);
}
#endregion
}
}