caching - 将 MemoryCacheHandle 与 RedisCacheBackplane 一起使用,但不使用 RedisCacheHandle

标签 caching redis distributed-caching cachemanager

文档上说

    /// The cache manager must have at least one cache handle configured with <see cref="CacheHandleConfiguration.IsBackplaneSource"/> set to <c>true</c>.
    /// Usually this is the redis cache handle, if configured. It should be the distributed and bottom most cache handle.

我知道如何使用 RedisCacheHandle 来完成它,因为它在 Cachemanager 的网站上作为示例给出

var cache = CacheFactory.Build<int>("myCache", settings =>
{
    settings
    .WithSystemRuntimeCacheHandle("inProcessCache")
    .And
    .WithRedisConfiguration("redis", config =>
    {
        config.WithAllowAdmin()
            .WithDatabase(0)
            .WithEndpoint("localhost", 6379);
    })
    .WithMaxRetries(1000)
    .WithRetryTimeout(100)
    .WithRedisBackplane("redis")
    .WithRedisCacheHandle("redis", true);
});

问题是我不想使用 Redis 作为缓存资源;我只是想通过 Redis Pub/Sub 机制的力量做一个分布式缓存。根据我通过代码进行的调试,通过使用 Redis 背板功能,我确实能够向 Redis 发送消息和从 Redis 接收消息。那么为什么不使用 RedisCacheHandle 而是使用 SystemRuntimeCacheHandle?

所以,我的期望是使用以下缓存配置成功执行

var cache = CacheFactory.Build<int>("myCache", settings =>
{
    settings
    .WithSystemRuntimeCacheHandle("inProcessCache")
    .And
    .WithRedisConfiguration("redis", config =>
    {
        config.WithAllowAdmin()
            .WithDatabase(0)
            .WithEndpoint("localhost", 6379);
    })
    .WithMaxRetries(1000)
    .WithRetryTimeout(100)
    .WithRedisBackplane("redis")
    .WithSystemRuntimeCacheHandle("inProcessCache", true);
});

但它不起作用。你能告诉我一个解决方案吗?我究竟做错了什么?或者,尽管它在文档中写为

...Usually this is the redis cache handle...

没有RedisCacheHandle,有没有办法使用缓存同步功能?

https://github.com/MichaCo/CacheManager/issues/111

最佳答案

我猜你说“不工作”是指其他缓存没有同步,例如如果我从 cacheA 中删除一个键,它不会从 cacheB 中删除吗? 是的,这是目前的预期行为。

背板旨在与只有一种状态的进程外缓存一起使用。 有 2 个缓存实例都使用系统运行时缓存,你有两个在 proc 缓存中完全断开连接。

通常,如果您有一个 Redis 层,并且您从缓存实例 A 中删除了一个键,该项目将从 Redis 层中删除。该消息被发送到同一缓存的其他实例,并将从除 redis(标记为背板源)之外的任何其他缓存层中删除 key 。 这意味着,我们预计背板源已经同步。

现在如果您有一个进程内缓存作为背板源会怎样。这是行不通的,因为两个实例总是不同步的。

让我们看这个例子:

var cacheConfig = ConfigurationBuilder.BuildConfiguration(settings =>
{
    settings
    .WithSystemRuntimeCacheHandle("inProcessCache")
    .And
    .WithRedisConfiguration("redis", config =>
    {
        config.WithAllowAdmin()
            .WithDatabase(0)
            .WithEndpoint("localhost", 6379);
    })
    .WithMaxRetries(1000)
    .WithRetryTimeout(100)
    .WithRedisBackplane("redis")
    .WithSystemRuntimeCacheHandle("inProcessCache", true);
});

var cacheA = new BaseCacheManager<string>(cacheConfig);
var cacheB = new BaseCacheManager<string>(cacheConfig);

cacheB.Backplane.Removed += (obj, args) =>
{
    Console.WriteLine(args.Key + " removed from B.");
};

cacheA.Add("key", "value");

var result = cacheB.Get("key");
Console.WriteLine("Result should be null:" + result);

cacheB.Add("key", "value");
result = cacheB.Get("key");
Console.WriteLine("Result should not be null:" + result);

// triggers backplane remove event
cacheA.Remove("key");

// lets give redis some time send messages
Thread.Sleep(100);

result = cacheB.Get("key");
Console.WriteLine("Result should be null again but isn't:" + result);

Console.ReadKey();

如果运行此程序,您可以看到背板事件实际触发,但因为唯一的进程内缓存是背板源,所以 key 不会被删除。 这就是为什么最后,您仍然会得到返回给您的 key 。

正如我所说,这是目前预期的行为。

不过,您可以通过监听这些事件来实现自定义逻辑。 (事件在下个版本中会略有变化,目前存在一些错误和不一致)。

此外,不要指望背板会将缓存值传输到其他实例。那永远不会发生。 CacheManager 只发送关键事件,而不发送数据,因为数据通常由进程外缓存处理。 意思是,如果你只有背板的进程内缓存,在 cacheA 中添加一个项目,不会将该项目复制到 cacheB!不过,您可能会收到 cacheB 上的键的 change 事件。

我希望这是有道理的;)

关于caching - 将 MemoryCacheHandle 与 RedisCacheBackplane 一起使用,但不使用 RedisCacheHandle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40741986/

相关文章:

linux - 用于验证多播网络设置的简单 ehcache 程序

java - 自定义模型的缓存/持久化的最佳方法

java - 如何以编程方式清除android M(6.0)中其他应用程序的缓存

caching - 如何确保shdfs集中式缓存正常工作

c# - Redis Pub/Sub ServiceStack,取消线程

laravel - 使用正则表达式忘记缓存 laravel

asp.net-core - IDistributedCache SQL Server 删除过期记录

python - 在 Python 中使用 MySQLdb 的长期陈旧结果

java - 在 Java 应用程序服务器上缓存 CSV 文件

django - 如何从 django-celery 3 任务发送 channel 2.x 组消息?