1. Project one: Used to add data to Redis
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingServicestack.redis;namespaceredistest{classProgram {Static voidMain (string[] args) { varClient =NewRedisclient ("172.16.1.114",6379); //client. Set<string> ("name", "Lao Wang"); //String value = client. Get<string> ("name"); //Console.WriteLine (value); //string redis = client. Get<string> ("Redis"); //Console.WriteLine (Redis); //UserInfo UserInfo = client. Get<userinfo> ("UserInfo"); //string info = userinfo. ToString (); //Console.WriteLine (info);List<UserInfo> list =NewList<userinfo>(); Random R=NewRandom (); Char[] Sexs =New[]{'male','female'}; for(inti =1; I <101; i++) {list. ADD (NewUserInfo () {Id = i, UserName ="Zhang San"+ R.next (1, -), Gender = Sexs[r.next (0,2)], age = R.next ( -, -) }); } client. ADD<List<UserInfo>> ("userinfolist", list); Console.WriteLine ("OK"); Console.readkey (); } } Public classUserInfo { PublicUserInfo () {// //TODO: Add constructor logic here// } Public intId {Get;Set; } Public stringUserName {Get;Set; } Public CharGender {Get;Set; } Public intAge {Get;Set; } Public stringToString () {return "id="+ Id +", Username="+ UserName +", gender="+ Gender +", age="+Age ; } }}
2. Item Two: Used to access project one Redis data
usingSystem;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingServicestack.redis;usingSystem.Collections.Generic; Public Partial class_default:system.web.ui.page {protected voidPage_Load (Objectsender, EventArgs e) { if(!IsPostBack) { varClient =NewRedisclient ("172.16.1.114",6379); List<UserInfo> list = client. Get<list<userinfo>> ("userinfolist"); foreach(varIteminchlist) {Response.Write (item. ToString ()); Response.Write ("<br/>"); } //UserInfo UserInfo = new UserInfo () {Id = 111, UserName = "John Doe", age = $, Gender = ' man '}; //var isadd = client. Add<userinfo> ("UserInfo", UserInfo); //Response.Write ("Whether to add success:" + Isadd); //Response.Write ("<br/>"); //UserInfo UserInfo = client. Get<userinfo> ("UserInfo"); //string info = userinfo. ToString (); //Response.Write (info); //Response.Write ("<br/>"); //client. Add<string> ("Redis", "Redis"); //string redis = client. Get<string> ("Redis"); //Response.Write (Redis); } }}
C # Redis Easy to use