使用Redis作为缓存和C#客户端 [英] Using Redis as Cache and C# client

查看:486
本文介绍了使用Redis作为缓存和C#客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Redis的新手,并试图找出一种简单的方法将Redis用作我的C#应用​​程序的本地缓存.我已经从 https://github.com/MSOpenTech/redis/releases <下载并运行了Redis服务器/a>

I'm new to Redis and trying to figure out a simple way to use Redis as a local cache for my C# app. I've downloaded and ran the redis-server from https://github.com/MSOpenTech/redis/releases

我可以成功存储键值并按以下方式检索它:

I can successfully store a key value and retrieve it as follows:

        var redisManager = new PooledRedisClientManager("localhost:6379");
        using (var redis = redisManager.GetClient())
        {
            redis.Set("mykey_1", 15, TimeSpan.FromSeconds(3600));
            // get typed value from cache
            int valueFromCache = redis.Get<int>("mykey_1"); // must be = 
        }

我想限制Redis在我的服务器上使用的内存量,并且我还希望Redis在内存填满时自动清除值.我尝试了maxmemory命令,但是在redus-cli程序中找不到maxmemory.

I want to limit the amount of memory Redis uses on my server and I also want redis to automatically purge values when memory fills. I tried the maxmemory command but in the redus-cli program maxmemory is not found.

Redus会自动为我清除旧值吗? (我认为不是),如果没有,是否可以通过下面使用的Set方法使redis的默认行为做到这一点?

Will Redus automatically purge old values for me? (I assume not) and if not, is there a way that I can make the default behavior of redis do that with the Set method I'm using below?

如果我走错了路,请告诉我.

If I'm heading down the wrong path, please let me know.

推荐答案

此处描述了您的问题的答案:

The answer to your question is described here: What does Redis do when it runs out of memory?

基本上,您是从配置文件而不是redis-cli设置maxmemory.您还可以指定maxmemory-policy,这是当Redis耗尽指定内存时将执行的一组过程.根据该配置文件,Redis总共有6条策略在内存用尽时使用:

Basically, you set the maxmemory from the config file, and not from the redis-cli. You can also specify a maxmemory-policy, which is a set of procedures that redis executes when it runs out of the specified memory. According to that config file, there are a total of 6 policies that Redis is using when it runs out of memory:

volatile-lru ->使用LRU算法删除具有过期设置的密钥

volatile-lru -> remove the key with an expire set using an LRU algorithm

allkeys-lru ->根据LRU算法删除任何密钥

allkeys-lru -> remove any key according to the LRU algorithm

易失性随机->删除具有过期集的随机密钥

volatile-random -> remove a random key with an expire set

allkeys-random ->删除随机密钥,任何密钥

allkeys-random -> remove a random key, any key

volatile-ttl ->删除最接近到期​​时间(较小的TTL)的密钥

volatile-ttl -> remove the key with the nearest expire time (minor TTL)

驱逐->根本不会过期,只是在写操作时返回错误

noeviction -> don't expire at all, just return an error on write operations

您可以使用在redis.conf文件的LIMITS部分中找到的 maxmemory-policy 指令(在maxmemory指令上方)来设置这些行为.

You can set those behaviours using the maxmemory-policy directive that you find in the LIMITS section of redis.conf file (above the maxmemory directive).

因此,您可以为Redis中存储的每个密钥设置过期时间(较大的 expire 时间),还设置了 volatile-ttl 策略.这样,当Redis内存不足时,将根据您设置的策略删除TTL最小的密钥(也是最旧的密钥).

So, you can set an expire time to every key that you store in Redis (a large expire time) and also set a volatile-ttl policy. In that way, when Redis runs out of memory, the key with the smallest TTL (which is also the oldest one) is removed, according to the policy that you've set.

这篇关于使用Redis作为缓存和C#客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆