Skip to content

Commit 23976fb

Browse files
committed
[refactor] Adding data to PublishAsync method for synchronize other instances
1 parent eb5b964 commit 23976fb

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace RedisPubSub.Common.Models
6+
{
7+
public class MemoryCacheDataDto
8+
{
9+
public string CacheKey { get; set; }
10+
public object Data { get; set; }
11+
}
12+
}

RedisPubSub.Publisher/Services/RedisPublisher.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using RedisPubSub.Common.Constants;
2+
using RedisPubSub.Common.Models;
23
using StackExchange.Redis;
34
using System.Threading.Tasks;
45

@@ -18,7 +19,13 @@ public RedisPublisher(IConnectionMultiplexer redisCachingProvider)
1819
}
1920
public async Task PublishMessage()
2021
{
21-
await _databaseAsync.PublishAsync(RedisChannelConstant.MemoryCache, "keyForDelete");
22+
var updatedData = new MemoryCacheDataDto
23+
{
24+
CacheKey = "user_information",
25+
Data = 20
26+
};
27+
var redisChannelData = System.Text.Json.JsonSerializer.Serialize(updatedData);
28+
await _databaseAsync.PublishAsync(RedisChannelConstant.MemoryCache, redisChannelData);
2229
}
2330
}
2431
}

RedisPubSub.Publisher/Startup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public void ConfigureServices(IServiceCollection services)
3131

3232
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
3333
{
34-
3534
app.UseRouting();
36-
3735
app.UseEndpoints(endpoints =>
3836
{
3937
endpoints.MapDefaultControllerRoute();

RedisPubSub.Subscriber/HostedServices/RedisSubscriberHostedService.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
44
using RedisPubSub.Common.Constants;
5+
using RedisPubSub.Common.Models;
56
using StackExchange.Redis;
67
using System;
78
using System.Threading;
@@ -23,10 +24,12 @@ public RedisSubscriberHostedService(IConnectionMultiplexer connectionMultiplexer
2324

2425
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2526
{
26-
await _subscriber.SubscribeAsync(RedisChannelConstant.MemoryCache, (a, cacheKey) =>
27+
await _subscriber.SubscribeAsync(RedisChannelConstant.MemoryCache, (a, updatedData) =>
2728
{
28-
_memoryCache.Remove(cacheKey);
29-
_logger.LogInformation($"Cache deleted. Key:{cacheKey}");
29+
var data = System.Text.Json.JsonSerializer.Deserialize<MemoryCacheDataDto>(updatedData);
30+
_memoryCache.Remove(data.CacheKey);
31+
_memoryCache.Set(data.CacheKey, data.Data);
32+
_logger.LogInformation($"MemoryCache update. Key:{data.CacheKey}");
3033
});
3134
}
3235
public override async Task StopAsync(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)