初入Redis---->簡單的部署

來源:互聯網
上載者:User

標籤:redis

公司項目後台有用到redis,在我看來一直覺得很高大上,我這種菜鳥高攀不起哈!懷著試試看的想法,自己也嘗試了下。。

  1. 下載了redis,解壓後看到如下檔案

    650) this.width=650;" src="http://s2.51cto.com/wyfs02/M01/89/B4/wKiom1gaChbxwYDoAAA2XtaLacE621.png-wh_500x0-wm_3-wmp_4-s_2749902050.png" title="redis.png" alt="wKiom1gaChbxwYDoAAA2XtaLacE621.png-wh_50" />

      沒猜錯的話redis-server.exe 指伺服器端,而redis-cli.exe指的是用戶端,redis.conf應該是設定檔吧,我想應該沒猜錯吧!

     開啟設定檔,一臉懵逼了,對於英語渣渣的我簡直就是懵逼的n次方,沒事,有度娘在手,應該能懂一點吧。

2. 首先開啟vs,建立一個控制台。

3. 先引用對應的dll,如所示 650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/89/B2/wKioL1gaDD7glbItAADRcruWOuo549.png-wh_500x0-wm_3-wmp_4-s_2657763539.png" title="libDll.png" alt="wKioL1gaDD7glbItAADRcruWOuo549.png-wh_50" />

4.先建立一個redis對象,由下面代碼我們可知

using System;namespace ServiceStack.Redis{    // 摘要:     //     Provide the default factory implementation for creating a RedisClient that    //     can be mocked and used by different ‘Redis Client Managers‘    public class RedisClientFactory : IRedisClientFactory    {        //由這裡可知,我們可以通過調用該執行個體得到一個RedisClientFactory 對象        public static RedisClientFactory Instance;        public RedisClientFactory();                //得到RedisClientFactory 對象後我們就可以調用該方法去建立RedisClient 執行個體了        public RedisClient CreateRedisClient(string host, int port);    }}

5.然後我們就可以開始愉快的擼代碼了

        static string ip = "127.0.0.1";        static int port = 6379;        static void Main(string[] args)        {            //開始調用            //1 確定redis伺服器的ip(127.0.0.1)+port (預設6379)            //2 執行個體化redis的用戶端執行個體            using (var client = RedisClientFactory.Instance.CreateRedisClient(ip,port))            {                //redis 的key對應的值可以是string,List<>,Set等等                //利用Set儲存資料:特點:如果key不存在,則建立,否則跟新其資料                client.Set<string>("name1", "SmallHan");                Console.WriteLine(client.Get<string>("name1"));                //List                Console.WriteLine();                Console.WriteLine("使用List");                var list = new List<string>(){                    "小瀚","小露","小明"                };                foreach (var it in list)                {                    client.AddItemToList("list", it);                }                var resultList = client.GetAllItemsFromList("list");                resultList.ForEach(c => Console.WriteLine(c));                //Set                Console.WriteLine();                Console.WriteLine("使用Set");                client.AddItemToSet("1", "2");                client.AddItemToSet("1", "2");                client.AddItemToSet("1", "3");                client.GetAllItemsFromSet("1").ToList().ForEach(c => Console.WriteLine(c));            }            Console.ReadKey();       }

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M02/89/B4/wKiom1gaDnyyJNopAAGLYo-kxAo713.png-wh_500x0-wm_3-wmp_4-s_81078150.png" title="result.png" alt="wKiom1gaDnyyJNopAAGLYo-kxAo713.png-wh_50" />

6.最簡單的redis讀寫好了,不過在啟動並執行時候記得將服務開啟,不過當伺服器重啟後,我們儲存的資料將會丟失,因為它是儲存在伺服器記憶體中的,我們可以通過設定檔進行對應的配置,將記憶體中的資料持久化到伺服器硬碟中。

本文出自 “11581236” 部落格,謝絕轉載!

初入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.