You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
5.8 KiB

using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using GDZZ.Core;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GDZZ.Application.Help
{
/// <summary>
/// 系统缓存服务
/// </summary>
public class CacheService : ICacheService, IDynamicApiController, ITransient
{
private static ICache _redisCache = App.RootServices.GetService(typeof(RedisCache)) as ICache;
/// <summary>
///
/// </summary>
public CacheService() { }
#region 聊天服务
/// <summary>
/// 获取聊天列表
/// </summary>
/// <returns></returns>
[NonAction]
public async Task<List<LiveHistoryLists>> GetLiveHistoryService(long UserID)
{
string cacheKey = SystemConst.LIVE_HISTORYLIST + $"{UserID}";
return await _redisCache.GetAsync<List<LiveHistoryLists>>(cacheKey);
}
/// <summary>
/// 设置聊天列表
/// </summary>
/// <returns></returns>
[NonAction]
public async Task SetLiveHistoryService(long UserID, List<LiveHistoryLists> liveMessageLists)
{
string cacheKey = SystemConst.LIVE_HISTORYLIST + $"{UserID}";
await _redisCache.SetAsync(cacheKey, liveMessageLists, TimeSpan.FromHours(1));
}
/// <summary>
/// 删除聊天列表
/// </summary>
/// <returns></returns>
[NonAction]
public async Task DelLiveHistoryService(long UserID)
{
string cacheKey = SystemConst.LIVE_HISTORYLIST + $"{UserID}";
await _redisCache.DelAsync(cacheKey);
}
#endregion
#region 聊天详情
/// <summary>
/// 获取聊天列表
/// </summary>
/// <returns></returns>
[NonAction]
public async Task<List<LiveMessageList>> GetMessage(long UserID,long ChatUserID)
{
string cacheKey = SystemConst.LIVE_MESSAGE + $"{UserID}_{ChatUserID}";
return await _redisCache.GetAsync<List<LiveMessageList>>(cacheKey);
}
/// <summary>
/// 获取聊天列表
/// </summary>
/// <returns></returns>
[NonAction]
public async Task SetMessage(List<LiveMessageList> liveMessages,long UserID,long ChatUserID)
{
string cacheKey = SystemConst.LIVE_MESSAGE + $"{UserID}_{ChatUserID}";
await _redisCache.SetAsync(cacheKey, liveMessages, TimeSpan.FromHours(1));
}
/// <summary>
/// 设置未读数
/// </summary>
/// <param name="UserID">用户未读</param>
/// <param name="ChatID">聊天室ID</param>
/// <returns></returns>
[NonAction]
public async Task SetUnreadAsync(long UserID, long ChatID)
{
string cacheKey = SystemConst.LIVE_UNREAD + $"{UserID}_{ChatID}";
var res = _redisCache.Get<int>(cacheKey);
await _redisCache.SetAsync(cacheKey, 1+res);
}
/// <summary>
/// 获取未读
/// </summary>
/// <param name="UserID"></param>
/// <param name="ChatID"></param>
/// <returns></returns>
public async Task<int> GetUnreadAsync(long UserID, long ChatID)
{
string cacheKey = SystemConst.LIVE_UNREAD + $"{UserID}_{ChatID}";
return await _redisCache.GetAsync<int>(cacheKey);
}
/// <summary>
/// 获取聊天列表
/// </summary>
/// <returns></returns>
[NonAction]
public async Task DelMessage(long UserID, long ChatUserID)
{
string cacheKey = SystemConst.LIVE_MESSAGE + $"{UserID}_{ChatUserID}";
await _redisCache.DelAsync(cacheKey);
}
#endregion
#region 用户服务
public async Task SetUserInfoAsync(AuthUserOut authUserOut, long UserID)
{
string cacheKey = SystemConst.MINI_USERINFO + $"{UserID}";
await _redisCache.SetAsync(cacheKey, authUserOut, TimeSpan.FromDays(1));
}
public async Task<AuthUserOut> GetUserInfoAsync(long UserID)
{
string cacheKey = SystemConst.MINI_USERINFO + $"{UserID}";
return await _redisCache.GetAsync<AuthUserOut>(cacheKey);
}
public async Task SetVerifyCode(string Phone, string Verify)
{
string cacheKey = SystemConst.MINI_USERPHONECODE + $"{Phone}";
await _redisCache.SetAsync(cacheKey, Verify, TimeSpan.FromMinutes(1));
}
public async Task<string> GetVerifyCode(string Phone)
{
string cacheKey = SystemConst.MINI_USERPHONECODE + $"{Phone}";
return await _redisCache.GetAsync<string>(cacheKey);
}
public async Task<string> GetAccessTokenAsync()
{
string cacheKey = SystemConst.MINI_ACCESSTOKEN;
return await _redisCache.GetAsync<string>(cacheKey);
}
public async Task SetAccessTokenAsync(string AccessToken)
{
string cacheKey = SystemConst.MINI_ACCESSTOKEN;
await _redisCache.SetAsync(cacheKey, AccessToken, TimeSpan.FromHours(2));
}
#endregion
#region 二维码
public async Task<string> GetQRCodeAsync(long UserID)
{
string cacheKey = SystemConst.MINI_QRCODE + $"{UserID}";
return await _redisCache.GetAsync<string>(cacheKey);
}
public async Task SetQRCodeAsync(long UserID,string Url)
{
string cacheKey = SystemConst.MINI_QRCODE+$"{UserID}";
await _redisCache.SetAsync(cacheKey, Url);
}
#endregion
}
}