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
- using (var redisclient = redismanager.getclient ())
- {
- var user = redisclient.gettypedclient<user> ();
- var value = user. GetValue (Txtchangekey.text); //Get the value of the current key first
- var changeduser = New User
- {
- Id = value. Id
- Name = Txtchangename.text,
- Job = New Job {Position = Txtchangeposition.text}
- }; //Set the corresponding new value value, and make the other data consistent with the original
- Redisclient.set (Txtchangekey.text, Changeduser); //Modify Value
- Value = user. GetValue (Txtchangekey.text); //Get up-to-date data based on key
- var htmlstr = string. Empty;
- Htmlstr + = "Modified id=" + value. Id + "Name:" + value. Name + "department:" + value. Job.position;
- Lblpeople.text = Htmlstr;
- Lblshow.text = "Total after filter: 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
[CSharp]View Plaincopy
- var dictionary = New dictionary<string, user> ();
- using (var redisclient = redismanager.getclient ())
- {
- var user = redisclient.gettypedclient<user> ();
- var user1 = New User
- {
- Id = user. Getnextsequence (),//Get a new ID
- Name = "Xiaoming",
- Job = New Job {Position = "Python"}
- };
- var user2 = New User
- {
- Id = user. Getnextsequence (),
- Name = "Little Red",
- Job = New Job {Position = "Python"}
- };
- var userkeylist = user. Getallkeys (). Where (x = X.startswith ("urn")). (y = y). ToList (); //Get key to save value only
- Dictionary. ADD (Userkeylist[1], user1); //A second person
- Dictionary. ADD (userkeylist[2], user2); //A third person
- Redisclient.setall (dictionary); //Modify multiple value at the same time
- var users = user. GetAll ();
- if (users. Count > 0)
- {
- var htmlstr = string. Empty;
- foreach (Var u in users)
- {
- Htmlstr + = "<li>id=" + u.id + "Name:" + u.name + "department:" + u.job.position + "</li>";
- }
- Lblpeople.text = Htmlstr;
- }
- Lblshow.text = "Total after filter:" + 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:
[CSharp]View Plaincopy
- using (var redisclient = redismanager.getclient ())
- {
- Redisclient.renamekey (Txtkey.text, Txtnewkey.text);
- var user = redisclient.gettypedclient<user> ();
- var userkeylist = user. Getallkeys ();
- if (Userkeylist.count > 0)
- {
- Lblpeople.text = string. Empty;
- var htmlstr = string. Empty;
- foreach (Var u in userkeylist)
- {
- Htmlstr + = "<li>key=" + U + "</li>";
- }
- Lblpeople.text = Htmlstr;
- }
- Lblshow.text = "Total after filter:" + 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.
[CSharp]View Plaincopy
- Public void Rename (string oldkeyname, string newkeyname)
- {
- if (oldkeyname = = null)
- throw New ArgumentNullException ("Oldkeyname");
- if (newkeyname = = null)
- throw New ArgumentNullException ("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
C # Redis Combat (vii)