Skip to content

Instantly share code, notes, and snippets.

@dbones
Last active January 10, 2022 09:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbones/5f359564b81d3158930b95fb346d3f1e to your computer and use it in GitHub Desktop.
Save dbones/5f359564b81d3158930b95fb346d3f1e to your computer and use it in GitHub Desktop.
possible wapper for redis, encapsulates Keys and Serialization
namespace Core.Redis
{
using System;
using Infrastructure;
using StackExchange.Redis;
public class Cache
{
private readonly IDatabase _database;
private readonly JsonSerializer _serializer;
private readonly string _prefix;
public Cache(IDatabase database, JsonSerializer serializer, RedisConfig config)
{
_database = database;
_serializer = serializer;
_prefix = config.Prefix ?? "_";
}
public void Delete<T>(string id) where T : class
{
_database.KeyDelete(GetId<T>(id));
}
public void Set<T>(string id, T instance) where T : class
{
Set(id, instance, new TimeSpan(2, 0, 0, 0));
}
public void Set<T>(string id, T instance, TimeSpan timeSpan) where T : class
{
var fullId = GetId<T>(id);
var serialized = _serializer.Serialize(instance);
_database.StringSet(fullId, serialized, timeSpan);
}
public T Get<T>(string id) where T : class
{
var fullId = GetId<T>(id);
var entry = _database.StringGet(fullId);
return entry == RedisValue.Null || entry.IsNull
? null
: _serializer.Deserialize<T>(entry);
}
private string GetId<T>(string id) where T : class
{
var key = string.Join(":", _prefix, typeof(T).Name, id);
return key;
}
}
}
namespace Core.Redis
{
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry;
using StackExchange.Redis;
public static class SetupRedis
{
public static IHostBuilder ConfigureRedis(this IHostBuilder hostBuilder)
{
hostBuilder.ConfigureServices(serviceCollection =>
{
serviceCollection.AddSingleton<IConnectionMultiplexer>(provider =>
{
var conf = provider.GetService<IConfiguration>();
var redisConfig = conf.GetSection("Redis").Get<RedisConfig>();
return ConnectionMultiplexer.Connect(redisConfig.ConnectionString);
});
serviceCollection.AddScoped<IDatabase>(provider =>
{
var redis = provider.GetService<IConnectionMultiplexer>();
return redis.GetDatabase();
});
serviceCollection.AddSingleton<Cache>();
serviceCollection.AddTransient<ITracerProviderBuilder, RedisTelemetry>();
serviceCollection.AddConfiguration<RedisConfig>("Redis");
});
return hostBuilder;
}
}
}
@dbones
Copy link
Author

dbones commented Jan 9, 2022

you can turn all of the above into async calls.

@dbones
Copy link
Author

dbones commented Jan 9, 2022

Remove line 30 (this is custom code to allow Modular setup of Telemetry)

//this line
serviceCollection.AddTransient<ITracerProviderBuilder, RedisTelemetry>();

@dbones
Copy link
Author

dbones commented Jan 9, 2022

serviceCollection.AddConfiguration("Redis");

this is a custom extension method, which sets up a convention that the key "Redis" in the config file will be deserialised into the RedisConfig type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment