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
                using(varRedisclient =redismanager.getclient ()) {                    varuser = Redisclient.gettypedclient<user>(); varValue = user. GetValue (Txtchangekey.text);//first get the value of the current key                    varChangeduser =NewUser {Id=value. Id, Name=Txtchangename.text, Job=NewJob {Position =Txtchangeposition.text}};//set the corresponding new value value, and make the other data consistent with the originalRedisclient.set (Txtchangekey.text, Changeduser);//Modify ValueValue = user. GetValue (Txtchangekey.text);//get the latest data based on key                    varHtmlstr =string.                    Empty; Htmlstr+="Modified id="+ value. Id +"&nbsp;&nbsp; Name:"+ value. Name +"&nbsp;&nbsp; Department:"+value.                    Job.position; Lblpeople.text=Htmlstr; Lblshow.text="total After screening: 1 people! "; }
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
            varDictionary =Newdictionary<string, user>(); using(varRedisclient =redismanager.getclient ()) {                varuser = Redisclient.gettypedclient<user>(); varUser1 =NewUser {Id= user. Getnextsequence (),//get the new IDName ="Xiao Ming", Job=NewJob {Position ="Python" }                }; varUser2 =NewUser {Id=user. Getnextsequence (), Name="Little Red", Job=NewJob {Position ="Python" }                }; varuserkeylist = user. Getallkeys (). Where (x = X.startswith ("urn")). (y = y). ToList ();//get only key that holds valueDictionary. ADD (userkeylist[1], user1);//a second personDictionary. ADD (userkeylist[2], user2);//a third personRedisclient.setall (dictionary);//Modify multiple value at the same time                               varUsers =user.                GetAll (); if(Users. Count >0)                {                    varHtmlstr =string.                    Empty; foreach(varUinchusers) {Htmlstr+="<li>id="+ U.id +"&nbsp;&nbsp; Name:"+ U.name +"&nbsp;&nbsp; Department:"+ U.job.position +"</li>"; } Lblpeople.text=Htmlstr; } Lblshow.text="total after filtering:"+ Users. Count.tostring () +"people! "; }
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:
using(varRedisclient =redismanager.getclient ())                                       {Redisclient.renamekey (Txtkey.text, Txtnewkey.text); varuser = Redisclient.gettypedclient<user>(); varUserkeylist =user.                    Getallkeys (); if(Userkeylist.count >0) {Lblpeople.text=string.                        Empty; varHtmlstr =string.                        Empty; foreach(varUinchuserkeylist) {Htmlstr+="<li>key="+ U +"</li>"; } Lblpeople.text=Htmlstr; } Lblshow.text="total after filtering:"+ userKeyList.Count.ToString () +"keys! "; }
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.
 Public voidRename (stringOldkeyname,stringnewkeyname) {            if(Oldkeyname = =NULL)                Throw NewArgumentNullException ("Oldkeyname"); if(Newkeyname = =NULL)                Throw NewArgumentNullException ("Newkeyname");        Sendexpectsuccess (Commands.rename, Oldkeyname.toutf8bytes (), newkeyname.toutf8bytes ()); }
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

http://blog.csdn.net/qiujialongjjj/article/details/17298767

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.