5

I use ServiceStack and would like to store objects as hashes in Redis and get an access to their parts (fields) by ids without serializing whole object, so I have a questions:

  1. Is there a way to use other property then "Id", to mark id field? I am using naming conventions, where id field is named like "class
    name+Id". So in User class there will be UserId id field, in
    Itemclass ItemId and so on.

  2. Is the a way to update properties that were changed in the object without serializing whole object into hash and without low-level manipulations with hash command of Redis? For example, by using some sort of lambdas.

 

User user=client.GetById(userId);
user.Name="New name";
client.Update<User>(user,u=>u.Name);
tom redfern
  • 30,562
  • 14
  • 91
  • 126
Sergey Zhukov
  • 1,342
  • 1
  • 13
  • 24

1 Answers1

5

Yes you can override the default using ModelConfig with:

ModelConfig<User>.Id(x => x.UserId);
ModelConfig<Item>.Id(x => x.ItemId);

This needs to be configured once on startup before you use the RedisClient.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thank you, Demis. Haven't seen this function before. A little correct of using: ModelConfig.Id(x=>x.UserId); And what about partial saving of object? I tried to write it by myself, but was stuck in expression trees. The issue is that if I use expression trees I have to use .Compile() method which is horrible slow down execution of saving. Also I don't want to use magic strings of property names for saving properties to hash... – Sergey Zhukov Sep 09 '12 at 09:27
  • If this answered your original question, mark it as answered. If you have another question you need to ask a new question. Make it obvious what the you're asking, e.g provide a code-sample if it helps describe what you're looking for. – mythz Sep 09 '12 at 10:00
  • Part N1 is answered. Part N2 is not answered. Sample of the code is provided in the part N2. What I want to do: get the User object from the redis hash, change "Name" property and save only the "Name" property into the hash without serializing and saving whole User object. I can do this by manually serializing properties into strings and using low-level hash functions, but just looking more common and obvious way like the code I provided. – Sergey Zhukov Sep 09 '12 at 20:34
  • The typed APIs in RedisClient blobs your objects as JSON. The Hash API's store like a string dictionary. You can partially update the hash contents with each property separately, you can't do this with the Typed APIs which blobs the object. Note: we have no plans to add partial update APIs on typed clients. – mythz Sep 09 '12 at 20:41
  • You can add custom extension methods against the `IRedisClient*` interfaces if you want to extend the clients to add this functionality. – mythz Sep 09 '12 at 20:48
  • I need partial updates too, I'm going to have a crack at implementing this. – DanB Feb 08 '13 at 17:17