p.durga shankar
p.durga shankar

Reputation: 1217

How to set TTL (expiry) for json record in Redis OM .NET

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 ?

Upvotes: 1

Views: 2349

Answers (2)

Tibi
Tibi

Reputation: 26

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 

Upvotes: 1

p.durga shankar
p.durga shankar

Reputation: 1217

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.

Upvotes: 1

Related Questions