问题描述:

In redis cache we can store data into key value pair ,

how do I store full object data ?

I want to store customer information inredis cache

I tried google and found this following but I wonder How to use it can any one elaborate more ?

public bool Add<T>(string key, T value, DateTimeOffset expiresAt) where T : class

{

var serializedObject = JsonConvert.SerializeObject(value);

var expiration = expiresAt.Subtract(DateTimeOffset.Now);

return database.StringSet(key, serializedObject, expiration);

}

public T Get<T>(string key) where T : class

{

var serializedObject = database.StringGet(key);

return JsonConvert.DeserializeObject<T>(serializedObject);

}

网友答案:

Serializing an object into a string and storing it as is in Redis is certainly one way of doing that.

Another way would be to model your custom object using Redis data structures. Most often, this is done using the Hash data type but, depending on the object's properties and your read/write requirements, you may want/need to use other types.

There are also helper libraries that can do this for you almost automagically - they usually go by the acronym ORM (Object Redis Mapper) or a variation of it. I'm not too familiar with the .NET scene, but here's a possible direction that you can research: https://github.com/ServiceStack/ServiceStack.OrmLite

相关阅读:
Top