示例#1
0
        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);
            }
        }
示例#2
0
        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);
            }
        }