Scenery
I created a function using AOP to do information cache, when the method is called I store in Redis using cache.StringSet
, but when I need to capture back the value of Redis I need to return the same type as I received, I know if I'm doing the right thing by serializing and storing the string, but when I get back from Redis cache.StringGet
I need it to be the same type when the object was inserted.
Remembering that objects can vary, they can be of N different types.
Code
public sealed override void OnInvoke(MethodInterceptionArgs args)
{
var cache = RedisConnectorHelper.Connection.GetDatabase();
var result = cache.StringGet(args.Method.Name);
if (result.HasValue)
{
args.ReturnValue = result;
return;
}
base.OnInvoke(args);
cache.StringSet(args.Method.Name, Serialize(args.ReturnValue));
}
private string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}