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;
///
/// Redis缓存前缀
///
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,返回自增后的值)
///
/// 自增1,返回自增后的值
///
public long Incr(string key,long value = 1)
{
return _database.StringIncrement(GetCacheKey(key),value);
}
#endregion
#region GetIncr(得到Incr值)
///
/// 得到Incr值
///
///
///
public object GetIncr(string key)
{
var cacheValue = _database.StringGet(GetCacheKey(key));
return cacheValue;
}
#endregion
#region GetNewId(得到新的id)
///
/// 得到新的id
///
///
///
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(string key)
{
var value = Get(key);
if (value == null) { return default(T); }
return (T)value;
}
#endregion
#region SetIncr(初始化原子性的功能)
///
/// 初始化原子性的功能
///
///
///
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删除)
///
/// 删除
///
///
public void Delete(string key)
{
_database.KeyDelete(GetCacheKey(key), CommandFlags.HighPriority);
}
#endregion
#region Exists(判断key是否存在)
///
/// 判断key是否存在
///
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(返回列表长度)
///
/// 返回列表长度
///
///
///
public long ListLen(string key)
{
key = GetCacheKey(key);
return _database.ListLength(key);
}
#endregion
}
}