C# redis簡單的使用

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   os   使用   for   sp   

1、項目一:用於在Redis中添加資料

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ServiceStack.Redis;namespace RedisTest{    class Program    {        static void Main(string[] args)        {            var client = new RedisClient("172.16.1.114", 6379);            //client.Set<string>("name", "老王");            //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 = new List<UserInfo>();            Random r = new Random();            char[] sexs = new []{‘男‘,‘女‘};            for (int i = 1; i < 101; i++)            {                list.Add(new UserInfo() { Id = i, UserName = "張三" + r.Next(1, 100), Gender = sexs[r.Next(0, 2)], Age = r.Next(20, 100) });            }            client.Add<List<UserInfo>>("userInfoList",list);            Console.WriteLine("ok");            Console.ReadKey();        }    }    public class UserInfo    {        public UserInfo()        {            //            //TODO: 在此處添加建構函式邏輯            //        }        public int Id { get; set; }        public string UserName { get; set; }        public char Gender { get; set; }        public int Age { get; set; }        public string ToString()        {            return "Id=" + Id + ",UserName=" + UserName + ",Gender=" + Gender + ",Age=" + Age;        }    }}

2、項目二:用於訪問項目一Redis中的資料

using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using ServiceStack.Redis;using System.Collections.Generic;public partial class _Default : System.Web.UI.Page {    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {             var client = new RedisClient("172.16.1.114", 6379);            List<UserInfo> list = client.Get<List<UserInfo>>("userInfoList");            foreach (var item in list)            {                Response.Write(item.ToString());                Response.Write("<br />");            }            //UserInfo userInfo = new UserInfo() { Id = 111, UserName = "李四", Age = 45, Gender = ‘男‘ };            //var isAdd = client.Add<UserInfo>("userinfo", userInfo);            //Response.Write("是否添加成功:" + 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簡單的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.