I have been using StackExchange.redis and StackExchange.Redis.Extensions libraries to drive data in and out of redis.
Initially i used StackExchange.redis,
var propertyList = ConvertToHashEntryList(productObject);
db.HashSet("Product:" + productObject.ProductID, propertyList.ToArray());
where ConvertToHashEntryList will return list of HashEntry ( in which propertyName is Rediskey and PropertyValue is RedisValue).
API :
db.HashSet(RedisKey key,HashEntry[] hashfields,CommandFlags)
This insert in redis appears to have Product:xxxx....xxxx as key and list of HashEntries as value. So this shows, one productObject will hold all its properties as hash entries.One hashSet - single object.
HASH Product:Guid
Key | Value
------------------
Property1 Value1
Property2 Value2
Property3 Value3
Property4 Value4
Property5 Value5
--------------------------------------------------------------------------------------
While in StackExchang.Redis.Extension,
Dictionary<string, Product> objToBeAdded1 = new Dictionary<string, Product>();
for (int i = 0; i < 5; i++)
{
Product storedObject = GetNewProduct();
objToBeAdded1.Add(storedObject.ID.ToString(), storedObject);
}
cacheClient.HashSet<Product>("Products", objToBeAdded1, CommandFlags.None);
API:
db.HashSet<T>(RedisKey key, Dictionary<string,T> object, CommandFlags);
Now the insert in Redis appears to have List of Object as value for the Product key.
where each object is serialized.
HASH Product
Key | Value
-----------
Guid1 {Serialized object1 in json -1}
Guid2 {Serialized object1 in json -2}
Guid3 {Serialized object1 in json -3}
Both APIs, Store objects in a different way. I would like to know which is the best approach to store object in redis?
What I have tried:
StackExchange.redis
var propertyList = ConvertToHashEntryList(productObject);
db.HashSet("Product:" + productObject.ProductID, propertyList.ToArray());
StackExchang.Redis.Extension:
Dictionary<string, Product> objToBeAdded1 = new Dictionary<string, Product>();
for (int i = 0; i < 5; i++)
{
Product storedObject = GetNewProduct();
objToBeAdded1.Add(storedObject.ID.ToString(), storedObject);
}
cacheClient.HashSet<Product>("Products", objToBeAdded1, CommandFlags.None);