C # Redis Combat (vii)

Source: Internet
Author: User

Vii. modify data in the previous C # Redis Combat (vi) Describes how to query data in Redis, this article describes how to modify the relevant data in Redis. We all know that Redis is a key-value storage system, so you should be able to modify the key, or you can modify the value by key. Next will be the detailed C # language to modify the Redis sample. 1. Modify single value by key [CSharp]View Plaincopy
  1. using (var redisclient = redismanager.getclient ())
  2. {
  3. var user = redisclient.gettypedclient<user> ();
  4. var value = user. GetValue (Txtchangekey.text); //Get the value of the current key first
  5. var changeduser = New User
  6. {
  7. Id = value. Id
  8. Name = Txtchangename.text,
  9. Job = New Job {Position = Txtchangeposition.text}
  10. }; //Set the corresponding new value value, and make the other data consistent with the original
  11. Redisclient.set (Txtchangekey.text, Changeduser); //Modify Value
  12. Value = user. GetValue (Txtchangekey.text); //Get up-to-date data based on key
  13. var htmlstr = string.  Empty;
  14. Htmlstr + = "Modified id=" + value. Id + "Name:" + value. Name + "department:" + value.  Job.position;
  15. Lblpeople.text = Htmlstr;
  16. Lblshow.text = "Total after filter: 1 people!"  ";
  17. }
The code executes as follows:
The above code allows Redis to modify the key for the person in the Urn:user:1 department, will be the original. NET is now Python, all of the modified data becomes: 2. Modify multiple values with keys [CSharp]View Plaincopy
  1. var dictionary = New dictionary<string, user> ();
  2. using (var redisclient = redismanager.getclient ())
  3. {
  4. var user = redisclient.gettypedclient<user> ();
  5. var user1 = New User
  6. {
  7. Id = user. Getnextsequence (),//Get a new ID
  8. Name = "Xiaoming",
  9. Job = New Job {Position = "Python"}
  10. };
  11. var user2 = New User
  12. {
  13. Id = user. Getnextsequence (),
  14. Name = "Little Red",
  15. Job = New Job {Position = "Python"}
  16. };
  17. var userkeylist = user. Getallkeys (). Where (x = X.startswith ("urn")). (y = y). ToList (); //Get key to save value only
  18. Dictionary. ADD (Userkeylist[1], user1); //A second person
  19. Dictionary. ADD (userkeylist[2], user2); //A third person
  20. Redisclient.setall (dictionary); //Modify multiple value at the same time
  21. var users = user. GetAll ();
  22. if (users. Count > 0)
  23. {
  24. var htmlstr = string.  Empty;
  25. foreach (Var u in users)
  26. {
  27. Htmlstr + = "<li>id=" + u.id + "Name:" + u.name + "department:" + u.job.position + "</li>";
  28. }
  29. Lblpeople.text = Htmlstr;
  30. }
  31. Lblshow.text = "Total after filter:" + users. Count.tostring () + "People!  ";
  32. }

I need to modify the first person's department based on the same time the second person and the third person's department, name, ID are also modified, executed as follows:
3. Rename KeyThe above example of modifying value only modifies value, but does not change the key value. If you need to rename key, first, query all of the keys in Redis
Modify the key code as follows: [CSharp]View Plaincopy
  1. using (var redisclient = redismanager.getclient ())
  2. {
  3. Redisclient.renamekey (Txtkey.text, Txtnewkey.text);
  4. var user = redisclient.gettypedclient<user> ();
  5. var userkeylist = user. Getallkeys ();
  6. if (Userkeylist.count > 0)
  7. {
  8. Lblpeople.text = string.  Empty;
  9. var htmlstr = string.  Empty;
  10. foreach (Var u in userkeylist)
  11. {
  12. Htmlstr + = "<li>key=" + U + "</li>";
  13. }
  14. Lblpeople.text = Htmlstr;
  15. }
  16. Lblshow.text = "Total after filter:" + userKeyList.Count.ToString () + "keys!  ";
  17. }
This is where you can rename keys, such as:
You can see that urn:user:1 no longer exists, but instead of urn:user:5, but if we run getall<user> again, we find only three data, and in C # Redis Combat (vi) I've mentioned IDs: The concept of user, in fact, in the Servicestack.redis rename does not save the renamed Key in it, which will result in Ids:user only the original three unmodified data, but there are still six data in Seq:user. [CSharp]View Plaincopy
  1. Public void Rename (string oldkeyname, string newkeyname)
  2. {
  3. if (oldkeyname = = null)
  4. throw New ArgumentNullException ("Oldkeyname");
  5. if (newkeyname = = null)
  6. throw New ArgumentNullException ("Newkeyname");
  7. Sendexpectsuccess (Commands.rename, Oldkeyname.toutf8bytes (), newkeyname.toutf8bytes ());
  8. }
But querying a single key can still get complete data: it is understandable that poor urn:user:5 become homeless children, whether getall (), or DeleteAll () have no effect on them. If you want to reprint, please specify the source, this series of blog sample program

C # Redis Combat (vii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.