本文就來講一下Redis安裝的方法和Redis生成主鍵的優(yōu)點(diǎn)以及和其他幾種方式生成主鍵的對(duì)比.
1,Redis安裝
首先將Redis的tar包拷貝到Linux下的根目錄
解壓后的目錄結(jié)構(gòu):
編譯: 使用Make命令
安裝:
安裝好之后的目錄:
6379 下的目錄結(jié)構(gòu):(這個(gè)rdb文件時(shí): redis database, 暫時(shí)不用管它, 重啟后自動(dòng)生成的)
配置后臺(tái)運(yùn)行:(將redis-3.0.0目錄下的redis.conf文件拷貝到6379目錄下, 使用cp命令)
啟動(dòng)redis服務(wù):(啟動(dòng)及停止命令)
客服端連接服務(wù)器:(如果是遠(yuǎn)程連接: ./bin/redis-cli -h 192.168.200.128 -p 6379)
命令行演示:
可以使用Redis集群來獲取更高的吞吐量。假如一個(gè)集群中有5臺(tái)Redis??梢猿跏蓟颗_(tái)Redis的值分別是1,2,3,4,5,然后步長都是5。各個(gè)Redis生成的ID為:
A:1,6,11,16,21
B:2,7,12,17,22
C:3,8,13,18,23
D:4,9,14,19,24
E:5,10,15,20,25
這個(gè),隨便負(fù)載到哪個(gè)機(jī)確定好,未來很難做修改。但是3-5臺(tái)服務(wù)器基本能夠滿足器上,都可以獲得不同的ID。但是步長和初始值一定需要事先需要了。使用Redis集群也可以方式單點(diǎn)故障的問題。
另外,比較適合使用Redis來生成每天從0開始的流水號(hào)。比如訂單號(hào)=日期 當(dāng)日自增長號(hào)??梢悦刻煸赗edis中生成一個(gè)Key,使用INCR進(jìn)行累加。
優(yōu)點(diǎn):
1)不依賴于數(shù)據(jù)庫,靈活方便,且性能優(yōu)于數(shù)據(jù)庫。
2)數(shù)字ID天然排序,對(duì)分頁或者需要排序的結(jié)果很有幫助。
缺點(diǎn):
1)如果系統(tǒng)中沒有Redis,還需要引入新的組件,增加系統(tǒng)復(fù)雜度。
2)需要編碼和配置的工作量比較大。
用INT做主鍵的優(yōu)點(diǎn):
1、需要很小的數(shù)據(jù)存儲(chǔ)空間,僅僅需要4 byte 。
2、insert和update操作時(shí)使用INT的性能比GUID好,所以使用int將會(huì)提高應(yīng)用程序的性能。
3、index和Join 操作,int的性能最好。
4、容易記憶。
5、支持通過函數(shù)獲取最新的值,如:Scope_Indentity() 。
使用INT做主鍵的缺點(diǎn)
1、如果經(jīng)常有合并表的操作,就可能會(huì)出現(xiàn)主鍵重復(fù)的情況。
2、使用INT數(shù)據(jù)范圍有限制。如果存在大量的數(shù)據(jù),可能會(huì)超出INT的取值范圍。
3、很難處理分布式存儲(chǔ)的數(shù)據(jù)表。
使用GUID做主鍵的優(yōu)點(diǎn):
1、它是獨(dú)一無二的。
2、出現(xiàn)重復(fù)的機(jī)會(huì)少。
3、適合大量數(shù)據(jù)中的插入和更新操作。
4、跨服務(wù)器數(shù)據(jù)合并非常方便。
使用GUID做主鍵的缺點(diǎn):
1、存儲(chǔ)空間大(16 byte),因此它將會(huì)占用更多的磁盤大小。
2、很難記憶。join操作性能比int要低。
3、沒有內(nèi)置的函數(shù)獲取最新產(chǎn)生的guid主鍵。
4、GUID做主鍵將會(huì)添加到表上的所以其他索引中,因此會(huì)降低性能。
3. 代碼中演示使用Redis
Java接口測試Redis:
Spring和Redis整合:
Service層:(ProductServiceIml.java)
@Autowired
private Jedis jedis;
看到這個(gè)最上面使用了Jdis去調(diào)用Redis服務(wù), 然后使用incr對(duì)pno(在redis中可以對(duì)pno設(shè)置值)加1操作. 之前使用的都是自增長ID, 在mapper.xml中insert完成之后自動(dòng)返回主鍵id到product中, 在級(jí)聯(lián)保存的時(shí)候可以直接使用product.getId(). 下面就來看一下redis與spring整合的配置.
1 <beans xmlns='http://www.springframework.org/schema/beans' 2 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:mvc='http://www.springframework.org/schema/mvc' 3 xmlns:context='http://www.springframework.org/schema/context' 4 xmlns:aop='http://www.springframework.org/schema/aop' 5 xmlns:tx='http://www.springframework.org/schema/tx' 6 xmlns:task='http://www.springframework.org/schema/task' 7 xmlns:dubbo='http://code.alibabatech.com/schema/dubbo' 8 xsi:schemaLocation='http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context-4.0.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 16 http://www.springframework.org/schema/tx 17 http://www.springframework.org/schema/tx/spring-tx-4.0.xsd18 http://www.springframework.org/schema/task19 http://www.springframework.org/schema/task/spring-task-4.0.xsd20 http://code.alibabatech.com/schema/dubbo 21 http://code.alibabatech.com/schema/dubbo/dubbo.xsd'>22 23 <!-- 整合Redis-->24 <bean id='jdis' class='redis.clients.jedis.Jedis'>25 <constructor-arg value='192.168.200.128' index='0' type='java.lang.String'/>26 <constructor-arg value='6379' index='1'/>27 </bean> 28 29 </beans>
聯(lián)系客服