弃用REDIS

parent 6e161b9d
...@@ -135,10 +135,12 @@ public void ConfigureServices(IServiceCollection services) ...@@ -135,10 +135,12 @@ public void ConfigureServices(IServiceCollection services)
#endregion #endregion
#region redis #region redis
var csredis = new CSRedis.CSRedisClient(connection.Value.RedisConnectionString); //var csredis = new CSRedis.CSRedisClient(connection.Value.RedisConnectionString);
RedisHelper.Initialization(csredis); //RedisHelper.Initialization(csredis);
#endregion #endregion
services.AddMemoryCache();
#region hangfire #region hangfire
services.AddHangfire(config => services.AddHangfire(config =>
{ {
......
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
...@@ -11,10 +12,14 @@ namespace Performance.Services ...@@ -11,10 +12,14 @@ namespace Performance.Services
public class AllotLogHub : Hub public class AllotLogHub : Hub
{ {
private readonly ILogger<AllotLogHub> logger; private readonly ILogger<AllotLogHub> logger;
private readonly IMemoryCache cache;
public AllotLogHub(ILogger<AllotLogHub> logger) public AllotLogHub(
ILogger<AllotLogHub> logger,
IMemoryCache cache)
{ {
this.logger = logger; this.logger = logger;
this.cache = cache;
} }
public override Task OnConnectedAsync() public override Task OnConnectedAsync()
...@@ -26,14 +31,14 @@ public override Task OnDisconnectedAsync(Exception exception) ...@@ -26,14 +31,14 @@ public override Task OnDisconnectedAsync(Exception exception)
{ {
var connectionId = Context.ConnectionId; var connectionId = Context.ConnectionId;
logger.LogDebug($"日志推送 断开连接{connectionId}"); logger.LogDebug($"日志推送 断开连接{connectionId}");
string key = $"AllotLogGroup_{connectionId}";
//1 查询用户分组信息 //1 查询用户分组信息
var groupName = ""; var groupName = "";
//2 删除数据库中用户分组数据 //2 删除数据库中用户分组数据
if (RedisHelper.HExists("AllotLogGroup", connectionId)) if (cache.TryGetValue(key, out groupName))
{ {
groupName = RedisHelper.HGet("AllotLogGroup", connectionId); cache.Remove(key);
RedisHelper.HDel("AllotLogGroup", connectionId);
} }
logger.LogDebug($"日志推送 断开连接{connectionId}-{groupName}"); logger.LogDebug($"日志推送 断开连接{connectionId}-{groupName}");
...@@ -45,15 +50,16 @@ public override Task OnDisconnectedAsync(Exception exception) ...@@ -45,15 +50,16 @@ public override Task OnDisconnectedAsync(Exception exception)
public async Task AddGroup(string token, string groupName) public async Task AddGroup(string token, string groupName)
{ {
var connectionId = Context.ConnectionId; var connectionId = Context.ConnectionId;
if (!RedisHelper.HExists("AllotLogGroup", connectionId)) string key = $"AllotLogGroup_{connectionId}";
if (cache.TryGetValue(key, out groupName))
{ {
//1 向数据库中插入用户及分组信息 cache.Remove(key);
RedisHelper.HSet("AllotLogGroup", connectionId, groupName);
logger.LogDebug($"日志推送 添加用户组{connectionId}-{groupName}");
//2 将用户插入分组
await Groups.AddToGroupAsync(connectionId, groupName);
} }
cache.Set(key, groupName, new TimeSpan(1, 0, 0));
logger.LogDebug($"日志推送 添加用户组{connectionId}-{groupName}");
//2 将用户插入分组
await Groups.AddToGroupAsync(connectionId, groupName);
} }
public async Task SendMessage(string groupName, string message) public async Task SendMessage(string groupName, string message)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment