Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Expire does not work

I use ServiceStack.Redis (build from the latest sources: https://github.com/ServiceStack/ServiceStack.Redis/tree/master/src).

I do something like this:

CacheRecord foundKey = cacheRecords.GetById(p_sParentKey);
...
CacheRecord cacheRecord = cacheRecords.Store(foundKey);
...
redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);

i tried to debug using Console.WriteLine(cacheRecords.GetTimeToLive(p_sParentKey)); which returns -00:00:01. it doesnt matter which value i assign to validityPeriodInMinutes.

I tried aswell Expire, ExpireAt, ExpireEntryAt, ExpireEntryIn. I also tried something like this:

int epoch = (int)(DateTime.UtcNow.AddSeconds(validityPeriodInMinutes) - new DateTime(1970, 1, 1)).TotalSeconds;
redisClient.ExpireAt(p_sParentKey, epoch);

any idea?

[Serializable]
public class CacheRecord
{
    public CacheRecord()
    {
        this.Children = new List<CacheRecordChild>();
    }

    public string Id { get; set; }
    public List<CacheRecordChild> Children { get; set; }
}
#endregion

#region public class CacheRecordChild
[Serializable]
public class CacheRecordChild
{
    public string Id { get; set; }
    public string Data { get; set; }
}

public void AddKey(string p_sParentKey, string p_sChildKey, string p_sData, int validityPeriodInMinutes)
    {
        using (ServiceStack.Redis.RedisClient redisClient = new ServiceStack.Redis.RedisClient())
        {
            using (var cacheRecords = redisClient.GetTypedClient<CacheRecord>())
            {
                CacheRecord foundKey = cacheRecords.GetById(p_sParentKey);
                if (foundKey == null)
                {
                    foundKey = new CacheRecord();
                    foundKey.Id = p_sParentKey;
                    CacheRecordChild child = new CacheRecordChild();
                    child.Id = p_sChildKey;
                    child.Data = p_sData;
                    foundKey.Children.Add(child);                        
                    CacheRecord cacheRecord = cacheRecords.Store(foundKey);
                    //redisClient.Expire(p_sParentKey, validityPeriodInMinutes);
                    redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);                       
                }
                else
                {
                    CacheRecordChild child = new CacheRecordChild();
                    child.Id = p_sChildKey;
                    child.Data = p_sData;
                    foundKey.Children.Add(child);
                    CacheRecord cacheRecord = cacheRecords.Store(foundKey);
                    // redisClient.Expire(p_sParentKey, validityPeriodInMinutes);
                    // redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);
                    // int epoch = (int)(DateTime.UtcNow.AddSeconds(validityPeriodInMinutes) - new DateTime(1970, 1, 1)).TotalSeconds;
                    redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);
                }
            }
        }
    }
like image 807
NickD Avatar asked Mar 17 '11 17:03

NickD


People also ask

What happens when Redis key expires?

After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology. The timeout will only be cleared by commands that delete or overwrite the contents of the key, including DEL , SET , GETSET and all the *STORE commands.

How can I tell when my Redis expires?

Redis TTL command is used to get the remaining time of key expiry in seconds. Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.

How do I change the expiration date on Redis cache?

To create a Redis with an expiration time, use the SET command and the EX option to set the expiration time. The EX option takes a number in seconds and sets the number of seconds the key is valid until expiration. You can also use PX to specify the expiration time in Milliseconds.

Can we set TTL in Redis?

No, Redis doesn't have a notion of a global/default TTL and yes, you do have to set it for each key independently.


1 Answers

Sorry but I can't really read your code very well in order to know if/what you're doing wrong.

I have a fair few tests for Expire, ExpireAt operations, here are some below which may better demonstrate how to use it:

https://github.com/ServiceStack/ServiceStack.Redis/blob/master/tests/ServiceStack.Redis.Tests/RedisClientTests.cs

[Test]
public void Can_Expire()
{
    Redis.SetEntry("key", "val");
    Redis.Expire("key", 1);
    Assert.That(Redis.ContainsKey("key"), Is.True);
    Thread.Sleep(2000);
    Assert.That(Redis.ContainsKey("key"), Is.False);
}

[Test]
public void Can_ExpireAt()
{
    Redis.SetEntry("key", "val");

    var unixNow = DateTime.Now.ToUnixTime();
    var in1Sec = unixNow + 1;

    Redis.ExpireAt("key", in1Sec);

    Assert.That(Redis.ContainsKey("key"), Is.True);
    Thread.Sleep(2000);
    Assert.That(Redis.ContainsKey("key"), Is.False);
}

If you're still having a problem, can you please post a runnable code snippet or gist so I can better read your code.

EDIT: Answer to code example

When you use a typed client, the key that ultimately gets stored in Redis looks like:

'urn:CacheRecord:' + p_sParentKey

Which is a different key to what you're using in your example:

redisClient.Expire(p_sParentKey, validityPeriodInMinutes * 60);

So there are a couple of ways to get the urn key that's used in Redis. If you have the entity you can use the ToUrn() Extension method, e.g.

var cacheKey = foundKey.ToUrn();

Otherwise if you just have the 'Id', you can create the urn key like:

var cacheKey = IdUtils.CreateUrn<CacheRecord>(p_sParentKey);

From there you can expire your entry:

redisClient.Expire(cacheKey, validityPeriodInMinutes * 60);

Although I understand how this is not intuitive, so I will look to add a RedisTypedClient.ExpiryIn method in a future version which would do this automatically for you. this should then let you do something like:

cacheRecords.ExpireIn(p_sParentKey, TimeSpan.FromMinutes(validityPeriodInMinutes));

EDIT: Added method overloads:

I've added the method above in the latest version of the Redis Client (v2.07) which you can download here: https://github.com/ServiceStack/ServiceStack.Redis/downloads

With tests using your CacheRecord.

BTW: the Redis Client doesn't actually need [Serializer] attribute.

like image 91
mythz Avatar answered Oct 06 '22 08:10

mythz