【发布时间】:2020-03-06 11:33:07
【问题描述】:
首先,我正在使用这个包装器:https://github.com/garywoodfine/redis-mvc-core/blob/master/RedisConfiguration/RedisVoteService.cs 和 Delete(...) 似乎不起作用。我想我尝试了IDistributedCache,它过去也不会删除该对象,但它至少会将所有属性都归零。
您可以在 cmets 中看到我尝试过 FlushDatabase(),但它似乎也不起作用。我想要 Delete 方法来删除对象。不仅使它们无效(这也不起作用)。
有什么想法吗?老实说,我想获得一个更好的包装器,支持List<T>。
var redis = new RedisAlgorithmService<BotSession>(_connectionFactory);
var test = redis.Get("Test");
if (test == null)
redis.Save("Test", new BotSession(TrendType.Uptrend, bot.Id));
test.NTimes = 123;
redis.Delete("Test");
using StackExchange.Redis;
using System;
namespace Binance.Redis
{
public class RedisAlgorithmService<T> : BaseService<T>, IRedisService<T>
{
internal readonly IRedisConnectionFactory _connectionFactory;
protected readonly IDatabase _database;
public RedisAlgorithmService(IRedisConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
_database = _connectionFactory.Connection().GetDatabase();
}
public void Delete(string key)
{
if (string.IsNullOrWhiteSpace(key) || key.Contains(":"))
throw new ArgumentException("Invalid key!");
key = GenerateKey(key);
_database.KeyDelete(key);
// _database.HashDelete(key, );
// var endpoints = _connectionFactory.Connection().GetEndPoints();
// _connectionFactory.Connection().GetServer(endpoints[0]).FlushDatabase();
}
public T Get(string key)
{
key = GenerateKey(key);
var hash = _database.HashGetAll(key);
return MapFromHash(hash);
}
public void Save(string key, T obj)
{
if (obj != null)
{
var hash = GenerateHash(obj);
key = GenerateKey(key);
if (_database.HashLength(key) == 0)
{
_database.HashSet(key, hash);
}
else
{
var props = Properties;
foreach (var item in props)
{
if (_database.HashExists(key, item.Name))
{
_database.HashIncrement(key, item.Name, Convert.ToInt32(item.GetValue(obj)));
}
}
}
}
}
}
}
【问题讨论】:
标签: asp.net-core redis stackexchange.redis