Last active
January 10, 2022 09:30
-
-
Save dbones/5f359564b81d3158930b95fb346d3f1e to your computer and use it in GitHub Desktop.
possible wapper for redis, encapsulates Keys and Serialization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Remove line 30 (this is custom code to allow Modular setup of Telemetry)
//this line
serviceCollection.AddTransient<ITracerProviderBuilder, RedisTelemetry>();
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
you can turn all of the above into async calls.