【问题标题】:Redis doesn't delete records from the databaseRedis 不会从数据库中删除记录
【发布时间】:2020-03-06 11:33:07
【问题描述】:

首先,我正在使用这个包装器:https://github.com/garywoodfine/redis-mvc-core/blob/master/RedisConfiguration/RedisVoteService.csDelete(...) 似乎不起作用。我想我尝试了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


    【解决方案1】:

    不确定FlushDatabase()FlushAllDatabase() 是否正是您想要的:

    • FLUSHDB – 从连接的当前数据库中删除所有键。
    • FLUSHALL – 从所有数据库中删除所有键。

    还有另一种解决方法,使用扩展方法和Newtonsoft.Json 作为序列化器/反序列化器。我了解IDistributedCache 仅支持字符串/字节数组作为输入,您不能将类对象传递给它,此解决方法将帮助您做到这一点。

    Startup.cs

    services.AddStackExchangeRedisCache(options =>
    {
        options.Configuration = "localhost";
        options.InstanceName = "name";
    });
    

    CacheExtensions.cs

    namespace TestProject.Extensions
    {
        public static class CacheExtensions
        {
            public static async Task<T> SetAsync<T>(this IDistributedCache cache, string key, T item)
            {
                var json = JsonConvert.SerializeObject(item);
    
                await cache.SetStringAsync(key, json);
    
                return await cache.GetAsync<T>(key);
            }
    
            public static async Task<T> SetAsync<T>(this IDistributedCache cache, string key, T item, int expirationInHours)
            {
                var json = JsonConvert.SerializeObject(item);
    
                await cache.SetStringAsync(key, json, new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(expirationInHours)
                });
    
                return await cache.GetAsync<T>(key);
            }
    
            public static async Task<T> GetAsync<T>(this IDistributedCache cache, string key)
            {
                var json = await cache.GetStringAsync(key);
    
                if (json == null)
                    return default;
    
                return JsonConvert.DeserializeObject<T>(json);
            }
        }
    }
    

    DIIDistributedCache:

    private readonly IDistributedCache _cache;
    
    public TestService(IDistributedCache cache)
    {
        _cache = cache;
    }
    

    并像这样使用它:

    List<Test> testList = new List<Test>
    {
        new Test(...),
        new Test(...)
    };
    
    var test = await _cache.GetAsync<List<Test>>("ListKey");
    await _cache.SetAsync("ListKey", testList);
    await _cache.RemoveAsync("ListKey");
    
    var test2 = await _cache.GetAsync<Test>("key");
    await _cache.SetAsync("key", new Test(...));
    await _cache.RemoveAsync("key");
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我不确定这是否对您有帮助。我可以看到您尝试过的这种方法,但似乎不完整。

      也尝试删除哈希。

      DeleteHash(string key, string cacheSubKey)
              {
                  if (string.IsNullOrEmpty(key))
                      throw new ArgumentNullException("Some problem here !");
      
                  _database.HashDelete(key, cacheSubKey);
              }
      

      【讨论】:

      • 是的,它不完整,因为我不知道如何使用它。 @笑脸
      • 我没有cacheSubKey
      猜你喜欢
      • 2013-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 2016-10-16
      相关资源
      最近更新 更多