@@ -23,7 +23,7 @@ public class CacheSubscriber : ICacheSubscriber
2323 /// <param name="cacheManager">Cache manager instance</param>
2424 /// <param name="exchange">RabbitMQ exchange name</param>
2525 /// <param name="queue">RabbitMQ queue name</param>
26- public CacheSubscriber ( IConnection connection , IEasyCacheManager cacheManager , string exchange , string queue )
26+ public CacheSubscriber ( IConnection connection , string exchange , string queue , IEasyCacheManager cacheManager )
2727 {
2828 _connection = connection ?? throw new ArgumentNullException ( nameof ( connection ) ) ;
2929 _cacheManager = cacheManager ?? throw new ArgumentNullException ( nameof ( cacheManager ) ) ;
@@ -36,41 +36,45 @@ public CacheSubscriber(IConnection connection, IEasyCacheManager cacheManager, s
3636 /// </summary>
3737 public async Task SubscribeAsync ( CancellationToken cancellationToken )
3838 {
39- using var channel = await _connection . CreateChannelAsync ( cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
40-
41- await channel . ExchangeDeclareAsync ( exchange : _exchange , type : ExchangeType . Fanout , durable : true , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
42- _ = await channel . QueueDeclareAsync ( queue : _queue , durable : true , exclusive : false , autoDelete : false , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
43- await channel . QueueBindAsync ( queue : _queue , exchange : _exchange , routingKey : string . Empty , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
39+ try
40+ {
41+ using var channel = await _connection . CreateChannelAsync ( cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
4442
45- var consumer = new AsyncEventingBasicConsumer ( channel ) ;
43+ await channel . ExchangeDeclareAsync ( exchange : _exchange , type : ExchangeType . Fanout , durable : true , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
44+ _ = await channel . QueueDeclareAsync ( queue : _queue , durable : true , exclusive : false , autoDelete : false , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
45+ await channel . QueueBindAsync ( queue : _queue , exchange : _exchange , routingKey : string . Empty , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
4646
47- consumer . ReceivedAsync += async ( _ , ea ) =>
48- {
49- var message = Encoding . UTF8 . GetString ( ea . Body . ToArray ( ) ) ;
47+ var consumer = new AsyncEventingBasicConsumer ( channel ) ;
5048
51- if ( message == "*" )
52- {
53- await _cacheManager . ClearAllCacheAsync ( ) . ConfigureAwait ( false ) ;
54- }
55- else
56- {
57- await _cacheManager . ClearCacheAsync ( message ) . ConfigureAwait ( false ) ;
58- }
49+ consumer . ReceivedAsync += async ( _ , ea ) => await ProcessAsync ( ea , channel , cancellationToken ) . ConfigureAwait ( false ) ;
5950
60- await channel . BasicAckAsync ( ea . DeliveryTag , false , cancellationToken ) . ConfigureAwait ( false ) ;
61- } ;
51+ // Start consuming messages
52+ _ = await channel . BasicConsumeAsync ( queue : _queue , autoAck : false , consumer : consumer , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
53+ }
54+ catch ( TaskCanceledException )
55+ {
56+ }
57+ catch ( Exception e )
58+ {
59+ Console . WriteLine ( e ) ;
6260
63- // Start consuming messages
64- _ = await channel . BasicConsumeAsync ( queue : _queue , autoAck : false , consumer : consumer , cancellationToken : cancellationToken ) . ConfigureAwait ( false ) ;
61+ await Task . Delay ( 100 , cancellationToken ) . ConfigureAwait ( false ) ;
62+ }
6563 }
6664
6765 /// <summary>
6866 /// Stops the Kafka subscription process.
6967 /// </summary>
7068 public async Task StopAsync ( )
7169 {
70+ if ( _disposed )
71+ {
72+ return ;
73+ }
74+
7275 await _connection . DisposeAsync ( ) . ConfigureAwait ( false ) ;
7376 await _connection . CloseAsync ( ) . ConfigureAwait ( false ) ;
77+ _disposed = true ;
7478 }
7579
7680 /// <summary>
@@ -88,4 +92,32 @@ public async ValueTask DisposeAsync()
8892
8993 _disposed = true ;
9094 }
95+
96+ private async Task ProcessAsync ( BasicDeliverEventArgs ea , IChannel channel , CancellationToken cancellationToken )
97+ {
98+ try
99+ {
100+ var key = Encoding . UTF8 . GetString ( ea . Body . ToArray ( ) ) ;
101+
102+ if ( key . Equals ( StaticData . ClearAllKey , StringComparison . Ordinal ) )
103+ {
104+ await _cacheManager . ClearAllCacheAsync ( ) . ConfigureAwait ( false ) ;
105+ }
106+ else
107+ {
108+ await _cacheManager . ClearCacheAsync ( key ) . ConfigureAwait ( false ) ;
109+ }
110+
111+ await channel . BasicAckAsync ( ea . DeliveryTag , false , cancellationToken ) . ConfigureAwait ( false ) ;
112+ }
113+ catch ( TaskCanceledException )
114+ {
115+ }
116+ catch ( Exception e )
117+ {
118+ Console . WriteLine ( e ) ;
119+
120+ await Task . Delay ( 100 , cancellationToken ) . ConfigureAwait ( false ) ;
121+ }
122+ }
91123}
0 commit comments