C # Redis Easy to use

Source: Internet
Author: User

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

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.