Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save alexandrebl/b9a03ae9c73df9821bec2d22fbdaa3d9 to your computer and use it in GitHub Desktop.

Select an option

Save alexandrebl/b9a03ae9c73df9821bec2d22fbdaa3d9 to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using StackExchange.Redis;
using StackExchange.Redis.Extensions.Core;
using StackExchange.Redis.Extensions.Newtonsoft;
namespace Library {
/// <summary>
/// Gerenciador de cache
/// </summary>
/// <typeparam name="T">tipo</typeparam>
public class CacheManagerUtility<T> : ICacheManagerUtility<T> {
/// <summary>
/// Servidor Redis
/// </summary>
private string _host;
/// <summary>
/// Chanal
/// </summary>
private string _channel;
/// <summary>
/// Método construtor
/// </summary>
/// <param name="configurationUtility">utilitário de configuração</param>
public CacheManagerUtility(IConfigurationUtility configurationUtility) {
//Servidor
_host = configurationUtility.RedisCacheServerIP;
//Canal
_channel = configurationUtility.RedisInputChannel;
}
/// <summary>
/// Publicação
/// </summary>
/// <param name="obj">objeto</param>
void ICacheManagerUtility<T>.Publish(T obj) {
//Inicializa o canal
var redisChannel = new RedisChannel(_channel, RedisChannel.PatternMode.Auto);
//Cria o cliente
using (var stackExchangeRedisCacheClient = new StackExchangeRedisCacheClient(
ConnectionMultiplexer.Connect($"{_host}:6379"),
new NewtonsoftSerializer(new JsonSerializerSettings()))) {
//Publica os dados
stackExchangeRedisCacheClient.Publish(redisChannel, obj);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment