Reputation: 297
I want to store object in Redis where key is not integer Id but property of type DateTime. Is it possible? How to tell Redis which of the properties is key? Merely any property named 'Id'?
If I put object to cache like this:
var redisClient = new RedisClient("localhost", 30050);
using (var myRedis = redisClient.As<MyObject>())
{
myRedis.Store(myObject);
}
how can I get it later?
var redisClient = new RedisClient("localhost", 30050);
using (var myRedis = redisClient.As<MyObject>())
{
myRedis.GetById(?????????);
}
Upvotes: 2
Views: 7426
Reputation: 6787
If you setup your MyObject class as:
class MyObject : IHasId<DateTime>
{
public DateTime Id {get;set;}
.......
}
Then it should work fine. Redis doesn't care about what your "Id" is. The ServiceStack Typed redis client cares because it takes your "Id" property and uses that to store your object. If you want to use a datetime as your key, so be it.
You'd get it later by passing a datetime into client.GetById(). That said, seems like a somewhat tricky key to manage unless you define the granularity level of your datetime based key. By that I mean, you need to decide if you'll care only about the date portion, or if you'll care about the date + time down to the minute, or the date + time down to the second, etc.
Also - you should change your redis client code to:
using(var client = new RedisClient("..."))
{
var myRedis = client.As<MyObject>();
......
}
The typed redis client returned by As() doesn't actually dispose of its connection, so your code as written would leave connections open.
Upvotes: 1
Reputation: 7212
Unix timestamps as ints will work nicely in Redis. You might need extra information if you need time zone support.
Upvotes: 1