緩衝架構——Redis(一)

來源:互聯網
上載者:User

標籤:.net   架構   緩衝   redis   



一,服務端的安裝


        服務端我用的是redis64-2.6.12.1,版本比較低,因為2.8的都安不上,可能是因為自己電腦是windows的原因吧。


        解壓完成之後,


    進入到這個檔案夾裡面,輸入:redis-server.exe redis.conf,服務開啟成功之後,會這樣子:


          

    


         


二,用戶端程式的安裝


           與memcached一樣,我們也可以通過cmd的方式操作緩衝,在用戶端程式裡面,可以使用dll.

                   在。net項目裡面,我使用的是ServiceStack.Redis.3.9.29.0。



                  

三,簡單Demo樣本

                      

             

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ServiceStack.Common;using ServiceStack.ServiceInterface;using ServiceStack.Redis;using ServiceStack.Text;namespace TestRedis{    class Program    {        static RedisClient Redis = new RedisClient("127.0.0.1", 6379);        static void Main(string[] args)        {            //將字元列表添加到redis            List<string> storeMembers = new List<string>{"one","two","three" };            storeMembers.ForEach(x=>Redis.AddItemToList("addItemToList",x));            //得到指定的key所對應的value集合            var members = Redis.GetAllItemsFromList("addItemToList");            members.ForEach(s=>Console.WriteLine("addItemToList:"+s));            //擷取指定索引位置資料            var item = Redis.GetItemFromList("addItemToList",2);            Console.WriteLine(item);            //移除資料            var list = Redis.Lists["addItemToList"];            list.Clear();//清空            list.Remove("two");//移除指定索引值            //儲存物件(json序列化方法)它比object序列化方法效率高            Redis.Set<UserInfo>("userInfo", new UserInfo() { UserName="lhc",Age=12});            UserInfo userInfo = Redis.Get<UserInfo>("userInfo");            Console.WriteLine("name="+userInfo.UserName+";age="+userInfo.Age);            //儲存實值型別資料            Redis.Set<int>("my_age", 12);            int age = Redis.Get<int>("my_age");            Console.WriteLine("age=" + age);            //object序列化方式儲存            var ser = new ServiceStack.Redis.Support.ObjectSerializer();            bool result=Redis.Set<byte[]>("userInfo2",ser.Serialize(new UserInfo(){UserName="pbc",Age=27}));            UserInfo userInfo2=ser.Deserialize(Redis.Get<byte[]>("userInfo2")) as UserInfo;            Console.WriteLine("name="+userInfo2.UserName+";age="+userInfo2.Age);            //也支援列表            List<UserInfo> userInfoList = new List<UserInfo> {                 new UserInfo{UserName="lhccc",Age=22},                new UserInfo{UserName="bcss",Age=100}            };            Redis.Set<byte[]>("UserInfoList_serialize", ser.Serialize(userInfoList));            List<UserInfo> userList = ser.Deserialize(Redis.Get<byte[]>("UserInfoList_serialize")) as List<UserInfo>;            userList.ForEach(                u => {                    Console.WriteLine("name="+u.UserName+";age="+u.Age);                }                );            Console.ReadKey();        }    }}


           

                個人感覺,redis比memcached支援資料類型多,而且記憶體資料可以持久化,效能也不錯,關於具體的技術選項分析,見下月吧。

   






著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

緩衝架構——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.