C# Redis缓存-从缓存读取返回null
我正在跟随codeproject的一篇文章来实现Redis,它满足了我缓存的目的。 我可以将数据保存在Redis缓存中,如果我通过终端(Redis cli)查询缓存,我可以看到与数据一起存储的列表。 用于将数据设置为Redis缓存的代码C# Redis缓存-从缓存读取返回null,c#,asp.net,.net,redis,webforms,C#,Asp.net,.net,Redis,Webforms,我正在跟随codeproject的一篇文章来实现Redis,它满足了我缓存的目的。 我可以将数据保存在Redis缓存中,如果我通过终端(Redis cli)查询缓存,我可以看到与数据一起存储的列表。 用于将数据设置为Redis缓存的代码 private void setDataToRedis() { try { ICacheProvider _cacheProvider = new RedisCacheProvider();
private void setDataToRedis()
{
try
{
ICacheProvider _cacheProvider = new RedisCacheProvider();
List<Person> people = new List<Person>()
{
new Person(1, "Joe", new List<Contact>()
{
new Contact("1", "123456789"),
new Contact("3", "234567890")
})
};
_cacheProvider.Remove("Peoples");
_cacheProvider.Set("Peoples", people);
}
catch (Exception ex)
{
throw ex;
}
}
private void setDataToRedis()
{
尝试
{
ICacheProvider _cacheProvider=新的RedisCacheProvider();
列出人员=新列表()
{
新人(1,“乔”,新名单()
{
新联系人(“1”、“123456789”),
新联系人(“3”、“234567890”)
})
};
_缓存提供程序。删除(“人民”);
_cacheProvider.Set(“人民”,人民);
}
捕获(例外情况除外)
{
掷骰子;
}
}
但当我试图读回代码时,它给出了一个空白对象
从Redis读取数据的代码
private void getDataFromRedis()
{
ICacheProvider _cacheProvider = new RedisCacheProvider();
dynamic contacts = "";
if (_cacheProvider.IsInCache("Peoples"))
{
contacts = _cacheProvider.Get<List<Contact>>("Peoples");
}
}
private void getDataFromRedis()
{
ICacheProvider _cacheProvider=新的RedisCacheProvider();
动态联系人=”;
if(_cacheProvider.IsInCache(“人民”))
{
联系人=_cacheProvider.Get(“人”);
}
}
Redis缓存管理器类
public class RedisCacheProvider : ICacheProvider
{
RedisEndpoint _endPoint;
public RedisCacheProvider()
{
_endPoint = new RedisEndpoint(RedisConfigurationManager.Config.Host, RedisConfigurationManager.Config.Port, RedisConfigurationManager.Config.Password, RedisConfigurationManager.Config.DatabaseID);
}
public void Set<T>(string key, T value)
{
this.Set(key, value, TimeSpan.Zero);
}
public void Set<T>(string key, T value, TimeSpan timeout)
{
using (RedisClient client = new RedisClient(_endPoint))
{
client.As<T>().SetValue(key, value, timeout);
}
}
public T Get<T>(string key)
{
T result = default(T);
using (RedisClient client = new RedisClient(_endPoint))
{
var wrapper = client.As<T>();
result = wrapper.GetValue(key);
}
return result;
}
public bool Remove(string key)
{
bool removed = false;
using (RedisClient client = new RedisClient(_endPoint))
{
removed = client.Remove(key);
}
return removed;
}
public bool IsInCache(string key)
{
bool isInCache = false;
using (RedisClient client = new RedisClient(_endPoint))
{
isInCache = client.ContainsKey(key);
}
return isInCache;
}
}
公共类RedisCacheProvider:ICacheProvider
{
重新定义端点_端点;
公共缓存提供程序()
{
_endPoint=new RedisEndpoint(RedisConfigurationManager.Config.Host、RedisConfigurationManager.Config.Port、RedisConfigurationManager.Config.Password、RedisConfigurationManager.Config.DatabaseID);
}
公共无效集(字符串键,T值)
{
此.Set(键、值、TimeSpan.Zero);
}
公共无效集(字符串键、T值、TimeSpan超时)
{
使用(RedisClient=新RedisClient(_端点))
{
client.As().SetValue(键、值、超时);
}
}
公共T获取(字符串键)
{
T结果=默认值(T);
使用(RedisClient=新RedisClient(_端点))
{
var wrapper=client.As();
结果=wrapper.GetValue(键);
}
返回结果;
}
公共布尔删除(字符串键)
{
bool-removed=false;
使用(RedisClient=新RedisClient(_端点))
{
移除=客户端。移除(密钥);
}
移除返回;
}
公共布尔码IsInCache(字符串键)
{
bool-isInCache=false;
使用(RedisClient=新RedisClient(_端点))
{
isInCache=client.ContainsKey(key);
}
返回isInCache;
}
}
有人能告诉我哪里出了问题吗?请发布存储和检索数据的相关代码,而不是发布图像。@DavidL更新了问题…您存储的是联系人列表,但试图检索联系人列表。那不行。当库像这样处理中介序列化时,需要像对象一样存储和检索对象。我真的忽略了这一点。非常感谢你指出这一点,不客气!请发布存储和检索数据的相关代码,而不是发布图像。@DavidL更新了问题…您存储的是一个联系人列表,但试图检索联系人列表。那不行。当库像这样处理中介序列化时,需要像对象一样存储和检索对象。我真的忽略了这一点。非常感谢你指出这一点,不客气!