自己動手搭建 Redis 環境,並建立一個 .NET HelloWorld 程式測試(轉)

來源:互聯網
上載者:User

標籤:des   blog   http   os   io   使用   ar   for   檔案   

關於 Redis ,下面來自百度百科:

redis是一個key-value儲存系統。和Memcached類似,它支援儲存的value類型相對更多,包括string(字串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hashs(雜湊類型)。這些資料類型都支援push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支援各種不同方式的排序。與memcached一樣,為了保證效率,資料都是緩衝在記憶體中。區別的是redis會周期性的把更新的資料寫入磁碟或者把修改操作寫入追加的記錄檔案,並且在此基礎上實現了master-slave(主從)同步。Redis 是一個高效能的key-value資料庫。 redis的出現,很大程度補償了memcached這類key/value儲存的不足,在部 分場合可以對關聯式資料庫起到很好的補充作用。它提供了Python,Ruby,Erlang,PHP用戶端,使用很方便。

上次測試了 Memcached,今天決定試試這個 Redis。

由於 Redis 部署在 Linux 環境下效能優於 Windows,於是就打算在虛擬機器 ubuntu 環境下部署,然後在 Windows 8 下測試。

1. 去官網下載 Redis 解壓和安裝,我下載的 2.8.0-rc2。

$ wget http://redis.googlecode.com/files/redis-2.6.15.tar.gz$ tar xzf redis-2.6.15.tar.gz$ cd redis-2.6.15$ make

2. 編譯後的可執行檔在src目錄中,可以使用下面的命令運行Redis:

$ src/redis-server

 

3. 你可以使用內建的用戶端串連Redis:

$ src/redis-cliredis> set foo barOKredis> get foo"bar"

 

4. 在 Windows 下建立 .NET 程式測試。

如果連不上

成功的情況下

測試代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using ServiceStack.Redis;using System.Threading;namespace RedisTutorial{    class Program    {        static void Main(string[] args)        {            using (var redisClient = RedisManager.GetClient())            {                using (var cars = redisClient.GetTypedClient<Car>())                {                    if (cars.GetAll().Count > 0)                        cars.DeleteAll();                    var dansFord = new Car { Id = cars.GetNextSequence(), Title = "張三的汽車", Make = new Make { Name = "寶馬" }, Model = new Model { Name = "平治" }};                    var beccisFord = new Car{Id = cars.GetNextSequence(),Title = "李四的汽車",Make = new Make { Name = "本田" },Model = new Model { Name = "福特" }};                    var vauxhallAstra = new Car{Id = cars.GetNextSequence(),Title = "王五的汽車",Make = new Make { Name = "比亞迪" },Model = new Model { Name = "通用" }};                    var vauxhallNova = new Car{Id = cars.GetNextSequence(),Title = "趙六的汽車",Make = new Make { Name = "福士" },Model = new Model { Name = "奧迪" }};                    var carsToStore = new List<Car> { dansFord, beccisFord, vauxhallAstra, vauxhallNova };                    cars.StoreAll(carsToStore);                    Console.WriteLine("Redis 有-> " + cars.GetAll().Count + " 輛汽車。");                    cars.ExpireAt(vauxhallAstra.Id, DateTime.Now.AddSeconds(5));                    Thread.Sleep(6000);                     Console.WriteLine("Redis 有-> " + cars.GetAll().Count + " 輛汽車。");                    var carsFromRedis = cars.GetAll().Where(car => car.Make.Name == "比亞迪");                    foreach (var car in carsFromRedis)                    {                        Console.WriteLine("Redis 有 ->" + car.Title);                    }                }            }            Console.ReadLine();        }    }    public class Car    {        public long Id { get; set; }        public string Title { get; set; }        public string Description { get; set; }        public Make Make { get; set; }        public Model Model { get; set; }    }    public class Make    {        public int Id { get; set; }        public string Name { get; set; }    }    public class Model    {        public int Id { get; set; }        public Make Make { get; set; }        public string Name { get; set; }    }}

 

設定檔:

<?xml version="1.0"?><configuration>  <configSections>    <section name="RedisConfig" type="RedisTutorial.RedisConfigInfo, RedisTutorial"/>  </configSections>  <RedisConfig WriteServerList="192.168.77.27:6379"                ReadServerList="192.168.77.27:6379"                MaxWritePoolSize="60"                MaxReadPoolSize="60"               AutoStart="true"                LocalCacheTime="180"                RecordeLog="false">       </RedisConfig><startup>  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>


測試代碼下載:點我下載

參考:ServiceStack.Redis 使用教程

其它資源:

redis在.net架構中的應用(1)--利用servicestack串連redis

謝謝瀏覽!

自己動手搭建 Redis 環境,並建立一個 .NET HelloWorld 程式測試(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.