redis學習及實踐

來源:互聯網
上載者:User
一.它是什麼


百度定義:
   “redis是一個key-value儲存系統。和Memcached類似,它支援儲存的value類型相對更多,包括五大資料類型string(字串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(雜湊類型)。這些資料類型都支援push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支援各種不同方式的排序。與memcached一樣,為了保證效率,資料都是緩衝在記憶體中。區別的是redis會周期性的把更新的資料寫入磁碟或者把修改操作寫入追加的記錄檔案,並且在此基礎上實現了master-slave(主從)同步。 Redis 是一個高效能的key-value資料庫。 redis的出現,很大程度補償了memcached這類key/value儲存的不足,在部 分場合可以對關聯式資料庫起到很好的補充作用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等用戶端,使用很方便。 Redis支援主從同步。資料可以從主伺服器向任意數量的從伺服器上同步,從伺服器可以是關聯其他從伺服器的主伺服器。這使得Redis可執行單層樹複製。存檔可以有意無意的對資料進行寫操作。由於完全實現了發布/訂閱機制,使得從資料庫在任何地方同步樹時,可訂閱一個頻道並接收主伺服器完整的訊息發布記錄。同步對讀取操作的可擴充性和資料冗餘很有協助。 redis的官網地址,非常好記,是redis.io。(特意查了一下,網域名稱尾碼io屬於國家網域名稱,是british Indian Ocean territory,即英屬印度洋地區) 目前,Vmware在資助著redis項目的開發和維護.”
二.它與memcached的區別
參見:http://blog.csdn.net/tonysz126/article/details/8280696/

三.它的適用情境
1、會話緩衝(Session Cache)

最常用的一種使用Redis的情景是會話緩衝(session cache)。用Redis緩衝會話比其他儲存(如Memcached)的優勢在於:Redis提供持久化。當維護一個不是嚴格要求一致性的緩衝時,如果使用者的購物車資訊全部丟失,大部分人都會不高興的,現在,他們還會這樣嗎。

幸運的是,隨著 Redis 這些年的改進,很容易找到怎麼恰當的使用Redis來緩衝會話的文檔。甚至廣為人知的商業平台Magento也提供Redis的外掛程式。
2、全頁緩衝(FPC)

除基本的會話token之外,Redis還提供很簡便的FPC平台。回到一致性問題,即使重啟了Redis執行個體,因為有磁碟的持久化,使用者也不會看到頁面載入速度的下降,這是一個極大改進,類似PHP本地FPC。

再次以Magento為例,Magento提供一個外掛程式來使用Redis作為全頁緩衝後端。

此外,對WordPress的使用者來說,Pantheon有一個非常好的外掛程式  wp-redis,這個外掛程式能協助你以最快速度載入你曾瀏覽過的頁面。
3、隊列

Reids在記憶體儲存引擎領域的一大優點是提供 list 和 set 操作,這使得Redis能作為一個很好的訊息佇列平台來使用。Redis作為隊列使用的操作,就類似於本地程式語言(如Python)對 list 的 push/pop 操作。

如果你快速的在Google中搜尋“Redis queues”,你馬上就能找到大量的開源項目,這些項目的目的就是利用Redis建立非常好的後端工具,以滿足各種隊列需求。例如,Celery有一個後台就是使用Redis作為broker,你可以從這裡去查看。
4、熱門排行榜/計數器

Redis在記憶體中對數字進行遞增或遞減的操作實現的非常好。集合(Set)和有序集合(Sorted Set)也使得我們在執行這些操作的時候變的非常簡單,Redis只是正好提供了這兩種資料結構。所以,我們要從排序集合中擷取到排名最靠前的10個使用者–我們稱之為“user_scores”,我們只需要像下面一樣執行即可:

當然,這是假定你是根據你使用者的分數做遞增的排序。如果你想返回使用者及使用者的分數,你需要這樣執行:

ZRANGE user_scores 0 10 WITHSCORES

Agora Games就是一個很好的例子,用Ruby實現的,它的熱門排行榜就是使用Redis來儲存資料的,你可以在這裡看到。
5、發布/訂閱

最後(但肯定不是最不重要的)是Redis的發布/訂閱功能。發布/訂閱的使用情境確實非常多。我已看見人們在社交網路串連中使用,還可作為基於發布/訂閱的指令碼觸發器,甚至用Redis的發布/訂閱功能來建立聊天系統。(不,這是真的,你可以去核實)。

Redis提供的所有特性中,我感覺這個是喜歡的人最少的一個,雖然它為使用者提供如果此多功能。

等等
四.它的優點

效能極高 – Redis能支援超過 100K+ 每秒的讀寫頻率。
豐富的資料類型 – Redis支援二進位案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 資料類型操作。
原子 – Redis的所有操作都是原子性的,同時Redis還支援對幾個操作全並後的原子性執行。
豐富的特性 – Redis還支援 publish/subscribe, 通知, key 到期等等特性。
五.它的缺點

是資料庫容量受到實體記憶體的限制,不能用作海量資料的高效能讀寫,因此Redis適合的情境主要局限在較小資料量的高效能操作和運算上。

六.它的部署     
 1.下載:https://github.com/MSOpenTech/redis/releases      
2.下載後解壓.      
3.配置項說明

<span style="font-family:Microsoft YaHei;">#是否以後台進程運行,預設為no,如果需要以後台進程運行則改為yesdaemonize no  #如果以後台進程啟動並執行話,就需要指定pid,你可以在此自訂redis.pid檔案的位置。pidfile /var/run/redis.pid  #接受串連的連接埠號碼,如果連接埠是0則redis將不會監聽TCP socket串連port 6379 # If you want you can bind a single interface, if the bind option is not# specified all the interfaces will listen for incoming connections.## bind 127.0.0.1 # Specify the path for the unix socket that will be used to listen for# incoming connections. There is no default, so Redis will not listen# on a unix socket when not specified.## unixsocket /tmp/redis.sock# unixsocketperm 755  #連線逾時時間,單位秒。(0 to disable)。timeout 300000000  #記錄層級,預設是verbose(詳細),各種記錄層級:#debug:很詳細的資訊,適合開發與測試#verbose:包含許多不太有用的資訊,但比debug要清爽一些(many rarely useful info, but not a mess like #the debug level)#notice:比較適合生產環境#warning:警告資訊loglevel verbose  #指定log檔案的名字,預設是stdout。stdout會讓redis把日誌輸出到標準輸出。但是如果使用stdout而又以後台進#程的方式運行redis,則日誌會輸出到/dev/nulllogfile stdout  #'syslog-enabled'設定為yes會把日誌輸出到系統日誌,預設是no# syslog-enabled no  #指定syslog的標示符,如果'syslog-enabled'是no,則這個選項無效。# syslog-ident redis  #指定syslog 裝置(facility), 必須是USER或者LOCAL0到LOCAL7.# syslog-facility local0  #設定資料庫數目。預設的資料庫是DB 0。可以通過SELECT <dbid>來選擇一個資料庫,dbid是[0,'databases'-1]的數字databases 16 ################## 快照################################### 硬碟上儲存資料:##   save <seconds> <changes>##   <seconds>和<changes>都滿足時就會觸發資料儲存動作。#   ##   以下面的例子來說明:#   過了900秒並且有1個key發生了改變 就會觸發save動作#   過了300秒並且有10個key發生了改變 就會觸發save動作#   過了60秒並且至少有10000個key發生了改變 也會觸發save動作##   注意:如果你不想讓redis自動儲存資料,那就把下面的配置注釋掉。 save 900 1save 300 10save 60 10000  #儲存資料時是否壓縮資料。預設是yes。rdbcompression yes # 儲存dump資料的檔案名稱dbfilename dump.rdb # 工作目錄.## 資料會被持久化到這個目錄下的‘dbfilename’指定的檔案中。# # # 注意,這裡指定的必須是目錄而不能是檔案。dir ./ ######## REPLICATION(複製,冗餘)################################# # Master-Slave replication. 使用slaveof把一個 Redis 執行個體設定成為另一個Redis server的從庫(熱備). 注意: #配置只對當前slave有效。# 因此可以把某個slave配置成使用不同的時間間隔來儲存資料或者監聽其他連接埠等等。#命令格式:# slaveof <masterip> <masterport>  #如果master有密碼保護,則在slave與master進行資料同步之前需要進行密碼校正,否則master會拒絕slave的請#求。## masterauth <master-password> #當slave丟失與master的串連時,或者slave仍然在於master進行資料同步時(還沒有與master保持一致),#slave可以有兩種方式來響應用戶端請求:## 1) 如果 slave-serve-stale-data 設定成 'yes' (the default) slave會仍然響應用戶端請求,此時可能會有問題。## 2) 如果 slave-serve-stale data設定成  'no'  slave會返回"SYNC with master in progress"這樣的錯誤資訊。 但 INFO 和SLAVEOF命令除外。#slave-serve-stale-data yes ############### 安全 ################################### # 需要用戶端在執行任何命令之前指定 AUTH <PASSWORD>## requirepass foobared # 命令重新命名.### 例如:## rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52## 同樣可以通過把一個命令重新命名為空白串來徹底kill掉這個命令,比如:## rename-command CONFIG "" #################### 限制 #################################### # 設定最大串連數. 預設沒有限制,  '0' 意味著不限制.## maxclients 128  #最大可使用記憶體。如果超過,Redis會試圖刪除EXPIRE集合中的keys,具體做法是:Redis會試圖釋放即將到期的#keys,而保護還有很長生命週期的keys。##如果這樣還不行,Redis就會報錯,但像GET之類的查詢請求還是會得到響應。##警告:如果你想把Redis視為一個真正的DB的話,那不要設定<maxmemory>,只有你只想把Redis作為cache或者#有狀態的server('state' server)時才需要設定。## maxmemory <bytes> #記憶體清理策略:如果達到了maxmemory,你可以採取如下動作:# # volatile-lru -> 使用LRU演算法來刪除到期的set# allkeys-lru -> 刪除任何遵循LRU演算法的key# volatile-random ->隨機地刪除到期set中的key# allkeys->random -> 隨機地刪除一個key# volatile-ttl -> 刪除最近即將到期的key(the nearest expire time (minor TTL))# noeviction -> 根本不到期,寫操作直接報錯# ## 預設策略:## maxmemory-policy volatile-lru # 對於處理redis記憶體來說,LRU和minor TTL演算法不是精確的,而是近似的(估計的)演算法。所以我們會檢查某些樣本#來達到記憶體檢查的目的。預設的樣本數是3,你可以修改它。## maxmemory-samples 3 ################# APPEND ONLY MODE ############################### #預設情況下,Redis會非同步把資料儲存到硬碟。如果你的應用情境允許因為系統崩潰等極端情況而導致最新資料丟失#的話,那這種做法已經很ok了。否則你應該開啟‘append only’模式,開啟這種模式後,Redis會在#appendonly.aof檔案中添加每一個寫操作,這個檔案會在Redis啟動時被讀取來在記憶體中重新構建資料集。##注意:如果你需要,你可以同時開啟‘append only’模式和非同步dumps模式(你需要注釋掉上面的‘save’運算式來禁#止dumps),這種情況下,Redis重建資料集時會優先使用appendonly.aof而忽略dump.rdb#appendonly no #  append only 檔案名稱 (預設: "appendonly.aof")# appendfilename appendonly.aof # 調用fsync()函數通知作業系統立刻向硬碟寫資料## Redis支援3中模式:## no:不fsync, 只是通知OS可以flush資料了,具體是否flush取決於OS.效能更好.# always: 每次寫入append only 記錄檔後都會fsync . 效能差,但很安全.# everysec: 沒間隔1秒進行一次fsync. 折中.## 預設是 "everysec"# appendfsync alwaysappendfsync everysec# appendfsync no # 當AOF fsync策略被設定為always或者everysec並且後台儲存進程(saving process)正在執行大量I/O操作時# Redis可能會在fsync()調用上阻塞過長時間#no-appendfsync-on-rewrite no # append only 檔案的自動重寫# 當AOF 記錄檔即將增長到指定百分比時,Redis可以通過調用BGREWRITEAOF 來自動重寫append only檔案。# # 它是這麼乾的:Redis會記住最近一次重寫後的AOF 檔案size。然後它會把這個size與當前size進行比較,如果當前# size比指定的百分比大,就會觸發重寫。同樣,你需要指定AOF檔案被重寫的最小size,這對避免雖然百分比達到了# 但是實際上檔案size還是很小(這種情況沒有必要重寫)卻導致AOF檔案重寫的情況很有用。### auto-aof-rewrite-percentage 設定為 0 可以關閉AOF重寫功能 auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb ################## SLOW LOG ################################### # Redis slow log用來記錄超過指定執行時間的查詢。# # 你可以指定兩個參數:一個是慢查詢的閥值,單位是毫秒;另外一個是slow log的長度,相當於一個隊列。 # 負數則關閉slow log,0則會導致每個命令都被記錄slowlog-log-slower-than 10000 # 不設定會消耗過多記憶體,所以還是要設定一下。可以使用SLOWLOG RESET命令來回收slow log使用的記憶體slowlog-max-len 1024 ################ 虛擬記憶體 ################################使用redis 就別用虛擬記憶體了,絕對不是一個好主意,加個機器吧,所以這裡不翻譯啦。。 ### WARNING! Virtual Memory is deprecated in Redis 2.4### The use of Virtual Memory is strongly discouraged. # Virtual Memory allows Redis to work with datasets bigger than the actual# amount of RAM needed to hold the whole dataset in memory.# In order to do so very used keys are taken in memory while the other keys# are swapped into a swap file, similarly to what operating systems do# with memory pages.## To enable VM just set 'vm-enabled' to yes, and set the following three# VM parameters accordingly to your needs. vm-enabled no# vm-enabled yes # This is the path of the Redis swap file. As you can guess, swap files# can't be shared by different Redis instances, so make sure to use a swap# file for every redis process you are running. Redis will complain if the# swap file is already in use.## The best kind of storage for the Redis swap file (that's accessed at random) # is a Solid State Disk (SSD).## *** WARNING *** if you are using a shared hosting the default of putting# the swap file under /tmp is not secure. Create a dir with access granted# only to Redis user and configure Redis to create the swap file there.vm-swap-file /tmp/redis.swap # vm-max-memory configures the VM to use at max the specified amount of# RAM. Everything that deos not fit will be swapped on disk *if* possible, that# is, if there is still enough contiguous space in the swap file.## With vm-max-memory 0 the system will swap everything it can. Not a good# default, just specify the max amount of RAM you can in bytes, but it's# better to leave some margin. For instance specify an amount of RAM# that's more or less between 60 and 80% of your free RAM.vm-max-memory 0 # Redis swap files is split into pages. An object can be saved using multiple# contiguous pages, but pages can't be shared between different objects.# So if your page is too big, small objects swapped out on disk will waste# a lot of space. If you page is too small, there is less space in the swap# file (assuming you configured the same number of total swap file pages).## If you use a lot of small objects, use a page size of 64 or 32 bytes.# If you use a lot of big objects, use a bigger page size.# If unsure, use the default :)vm-page-size 32 # Number of total memory pages in the swap file.# Given that the page table (a bitmap of free/used pages) is taken in memory,# every 8 pages on disk will consume 1 byte of RAM.## The total swap size is vm-page-size * vm-pages## With the default of 32-bytes memory pages and 134217728 pages Redis will# use a 4 GB swap file, that will use 16 MB of RAM for the page table.## It's better to use the smallest acceptable value for your application,# but the default is large in order to work in most conditions.vm-pages 134217728 # Max number of VM I/O threads running at the same time.# This threads are used to read/write data from/to swap file, since they# also encode and decode objects from disk to memory or the reverse, a bigger# number of threads can help with big objects even if they can't help with# I/O itself as the physical device may not be able to couple with many# reads/writes operations at the same time.## The special value of 0 turn off threaded I/O and enables the blocking# Virtual Memory implementation.vm-max-threads 4 ################進階配置############################### # Hashes are encoded in a special way (much more memory efficient) when they# have at max a given numer of elements, and the biggest element does not# exceed a given threshold. You can configure this limits with the following# configuration directives.hash-max-zipmap-entries 512hash-max-zipmap-value 64 # Similarly to hashes, small lists are also encoded in a special way in order# to save a lot of space. The special representation is only used when# you are under the following limits:list-max-ziplist-entries 512list-max-ziplist-value 64 # Sets have a special encoding in just one case: when a set is composed# of just strings that happens to be integers in radix 10 in the range# of 64 bit signed integers.# The following configuration setting sets the limit in the size of the# set in order to use this special memory saving encoding.set-max-intset-entries 512 # Similarly to hashes and lists, sorted sets are also specially encoded in# order to save a lot of space. This encoding is only used when the length and# elements of a sorted set are below the following limits:zset-max-ziplist-entries 128zset-max-ziplist-value 64 # Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in# order to help rehashing the main Redis hash table (the one mapping top-level# keys to values). The hash table implementation redis uses (see dict.c)# performs a lazy rehashing: the more operation you run into an hash table# that is rhashing, the more rehashing "steps" are performed, so if the# server is idle the rehashing is never complete and some more memory is used# by the hash table.# # The default is to use this millisecond 10 times every second in order to# active rehashing the main dictionaries, freeing memory when possible.## If unsure:# use "activerehashing no" if you have hard latency requirements and it is# not a good thing in your environment that Redis can reply form time to time# to queries with 2 milliseconds delay.## use "activerehashing yes" if you don't have such hard requirements but# want to free memory asap when possible.activerehashing yes ################## INCLUDES ################################### # Include one or more other config files here.  This is useful if you# have a standard template that goes to all redis server but also need# to customize a few per-server settings.  Include files can include# other files, so use this wisely.## include /path/to/local.conf# include /path/to/other.conf</span>

根據配置完成後:
4.開啟cmd->cd到redis檔案下->輸入指令:redis-server.exe redis.conf

如下圖成功


七.它的實踐


.net通過使用servicestack支援redis


1.項目中需要引用的,清單如下:

ServiceStack.Common.dllServiceStack.Interfaces.dllServiceStack.Text.dll主要:<span style="color:#FF0000;">ServiceStack.Redis.dll</span>

2..net源碼

static RedisClient Redis = new RedisClient("127.0.0.1", 6379);//redis服務IP和連接埠       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("<br/>additemtolist :" + s));           // 擷取指定索引位置資料           var item = Redis.GetItemFromList("addarrangetolist", 2);           Console.WriteLine(item);           //移除資料           var list = Redis.Lists["addarrangetolist"];           list.Clear();//清空           list.Remove("two");//移除指定索引值           //  list.RemoveAt(2);//移除指定索引位置資料           //儲存物件(JSON序列化方法)它比object序列化方法效率高           Redis.Set<UserInfo>("userinfo", new UserInfo() { UserName = "李四", Age = 45 });           UserInfo userinfo = Redis.Get<UserInfo>("userinfo");           Console.WriteLine("name=" + userinfo.UserName + "age=" + userinfo.Age);           //儲存實值型別資料           Redis.Set<int>("my_age", 12);//或Redis.Set("my_age", 12);           int age = Redis.Get<int>("my_age");           Console.WriteLine("age=" + age);           //object序列化方式儲存           var ser = new ObjectSerializer();    //位於namespace ServiceStack.Redis.Support;           bool result = Redis.Set<byte[]>("userinfo2", ser.Serialize(new UserInfo() { UserName = "張三", Age = 12 }));           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="zzl",Age=1,Id=1},            new UserInfo{UserName="zhz",Age=3,Id=2},            };           Redis.Set<byte[]>("userinfolist_serialize", ser.Serialize(userinfoList));           List<UserInfo> userList = ser.Deserialize(Redis.Get<byte[]>("userinfolist_serialize")) as List<UserInfo>;           userList.ForEach(i =>           {               Console.WriteLine("name=" + i.UserName + "age=" + i.Age);           });}


參照:http://jiangwenfeng762.iteye.com/blog/1283676

http://blog.jobbole.com/88383/

http://www.aboutyun.com/thread-9223-1-1.html

http://www.cnblogs.com/lori/p/3435483.html

相關文章

聯繫我們

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