skip to Main Content

How can we set expiry for a record in dotnet using Redis.OM dotnet ?
I did go through documentation of Redis.OM dotnet but could not find any method that we can use to set expiry for a record. But for the same package using python ( Redis OM Python ) there is a method to set expiry.

person_to_expire = Person.get(id)
Person.db().expire(person_to_expire.key(), seconds)

Can someone help me to understand how we can do the similar thing using Redis OM dotnet ?

2

Answers


  1. Chosen as BEST ANSWER

    I got the answer from the contributor of the package.

    https://github.com/redis/redis-om-dotnet/issues/107

     private RedisConnectionProvider _provider = new RedisConnectionProvider("your connection string");
    

    once the connection has been established and while inserting your record, you can use this method to set the expiration for the record.

    _provider.Connection.Execute("EXPIRE", "your_key_for_the_record", "60");
    

    Note: Here we need to pass seconds in string format.


  2. You can set TTL (Time to Live) during insert:

    string _redisConnectionString = "redis://localhost:6379";
    RedisConnectionProvider _provider = new RedisConnectionProvider(_redisConnectionString);
    RedisCollection<Model.MyObject> _redisColl = (RedisCollection<Model.MyObject>)_provider.RedisCollection<Model.MyObject>();
    var myObj = new Model.MyObject{id=1,name="My Name"};
    
    _redisColl .InsertAsync(myObj, new TimeSpan(0,10,0)); //for 10 minutes TTL 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search