public KafkaCache() { Log = LogManager.GetLogger("ASC"); Cts = new ConcurrentDictionary <string, CancellationTokenSource>(); Actions = new ConcurrentDictionary <string, Action <T> >(); Key = Guid.NewGuid(); var settings = ConfigurationManager.GetSetting <KafkaSettings>("kafka"); if (settings != null && !string.IsNullOrEmpty(settings.BootstrapServers)) { ClientConfig = new ClientConfig { BootstrapServers = settings.BootstrapServers }; var config = new ProducerConfig(ClientConfig); Producer = new ProducerBuilder <AscCacheItem, T>(config) .SetErrorHandler((_, e) => Log.Error(e)) .SetKeySerializer(KeySerializer) .SetValueSerializer(ValueSerializer) .Build(); } else { MemoryCacheNotify = new MemoryCacheNotify <T>(); } }
public void Publish(T obj, CacheNotifyAction cacheNotifyAction) { if (ClientConfig == null) { MemoryCacheNotify.Publish(obj, cacheNotifyAction); return; } try { if (Producer == null) { Producer = new ProducerBuilder <AscCacheItem, T>(new ProducerConfig(ClientConfig)) .SetErrorHandler((_, e) => Log.Error(e)) .SetKeySerializer(KeySerializer) .SetValueSerializer(ValueSerializer) .Build(); } var channelName = GetChannelName(cacheNotifyAction); if (Actions.TryGetValue(channelName, out var onchange)) { onchange(obj); } var message = new Message <AscCacheItem, T> { Value = obj, Key = new AscCacheItem { Id = ByteString.CopyFrom(Key.ToByteArray()) } }; Producer.ProduceAsync(channelName, message); } catch (ProduceException <Null, string> e) { Log.Error(e); } catch (Exception e) { Log.Error(e); } }
public KafkaCache(IConfiguration configuration, IOptionsMonitor <ILog> options) { Log = options.CurrentValue; Cts = new ConcurrentDictionary <string, CancellationTokenSource>(); Actions = new ConcurrentDictionary <string, Action <T> >(); Key = Guid.NewGuid(); var settings = configuration.GetSetting <KafkaSettings>("kafka"); if (settings != null && !string.IsNullOrEmpty(settings.BootstrapServers)) { ClientConfig = new ClientConfig { BootstrapServers = settings.BootstrapServers }; } else { MemoryCacheNotify = new MemoryCacheNotify <T>(); } }
public void Publish(T obj, CacheNotifyAction cacheNotifyAction) { if (ClientConfig == null) { MemoryCacheNotify.Publish(obj, cacheNotifyAction); return; } try { var channelName = GetChannelName(cacheNotifyAction); if (Actions.TryGetValue(channelName, out var onchange)) { onchange(obj); } var message = new Message <AscCacheItem, T> { Value = obj, Key = new AscCacheItem { Id = ByteString.CopyFrom(Key.ToByteArray()) } }; Producer.ProduceAsync(channelName, message); } catch (ProduceException <Null, string> e) { Log.Error(e); } catch (Exception e) { Log.Error(e); } }
public void Subscribe(Action <T> onchange, CacheNotifyAction cacheNotifyAction) { if (ClientConfig == null) { MemoryCacheNotify.Subscribe(onchange, cacheNotifyAction); return; } var channelName = GetChannelName(cacheNotifyAction); Cts[channelName] = new CancellationTokenSource(); Actions[channelName] = onchange; void action() { var conf = new ConsumerConfig(ClientConfig) { GroupId = Guid.NewGuid().ToString() }; using var c = new ConsumerBuilder <AscCacheItem, T>(conf) .SetErrorHandler((_, e) => Log.Error(e)) .SetKeyDeserializer(KeyDeserializer) .SetValueDeserializer(ValueDeserializer) .Build(); c.Assign(new TopicPartition(channelName, new Partition())); try { while (true) { try { var cr = c.Consume(Cts[channelName].Token); if (cr != null && cr.Value != null && !(new Guid(cr.Key.Id.ToByteArray())).Equals(Key) && Actions.TryGetValue(channelName, out var act)) { try { act(cr.Value); } catch (Exception e) { Log.Error("Kafka onmessage", e); } } } catch (ConsumeException e) { Log.Error(e); } } } catch (OperationCanceledException) { c.Close(); } } var task = new Task(action, TaskCreationOptions.LongRunning); task.Start(); }