JAVA 中 Redis與ehcache對比與使用

第一:二者之間的介紹html

Redis:屬於獨立的運行程序,須要單獨安裝後,使用Java中的Jedis來操縱。由於它是獨立,因此若是你寫個單元測試程序,放一些數據在Redis中,而後又寫一個程序去拿數據,那麼是能夠拿到這個數據的。,java

ehcache:與Redis明顯不一樣,它與java程序是綁在一塊兒的,java程序活着,它就活着。譬如,寫一個獨立程序放數據,再寫一個獨立程序拿數據,那麼是拿不到數據的。只能在獨立程序中才能拿到數據。redis

 

第二:使用及各類配置:緩存

二者均可以集羣:app

1.Redis能夠作主歷來集羣,例如,在A電腦上裝個Redis,做爲主庫;在其餘電腦上裝Redis,做爲從庫;這樣主庫擁有讀和寫的功能,而從庫只擁有讀的功能。每次主庫的數據都會同步到從庫中。socket

1.默認方式啓動ide

[html] view plain copy測試

 在CODE上查看代碼片派生到個人代碼片

  1. Linux下使用Redis  
  2. 安裝:從官網上下載tar.gz格式的包,而後使用tar zxvf redis-2.8.24.tar.gz命令解壓,而後進入Redis文件夾目錄下的src目錄,使用make編譯一下  
  3.   
  4. 1.開啓:進入/usr/local/redis-3.2.1/src  
  5. 而後./redis-server  

 

2.若是咱們想修改端口,設置密碼:那麼得修改配置文件的redis.confui

port 6379                                         //端口修改url

requirepass redis123                  //設置密碼  redis123爲密碼

 

配置主從:主庫的配置文件不用修改,從庫的配置文件須要修改,由於從庫須要綁定主庫,以即可以獲取主庫的數據

slaveof 192.168.1.100 6379                              //主庫的IP地址和端口號
masterauth redis123                                      //主庫設定的密碼

3.要讓配置文件的屬性生效,那麼啓動的redis的時候,要將配置文件加上去

進入/usr/local/redis-3.2.1/src

而後 ./redis-server  redis.conf

那麼將成功的啓動redis,若是沒有加入配置的話,按照普通方式啓動的話,端口仍然仍是6379.

 

4.客戶端鏈接遠程的Redis
第一步:在遠程端處設置密碼:config set requirepass 123       //123爲密碼
第二步:能夠在客戶端登陸  redis-cli.exe -h 114.215.125.42 -p 6379 
第三步:認證:auth 123                                       //123爲密碼
本地端設置密碼後,要使用密碼登陸;若是Redis重啓的話,密碼須要從新設置

5.主從配置後,爲保證主庫寫的能力,通常不在主庫作持久化,而是在從庫作持久化:

主庫配置:

將save註釋,不使用rdb

# save 900 1
# save 300 10
# save 60 10000

 

appendonly no        不使用aof

 

從庫配置:

save 900 1
save 300 10
save 60 10000

appendonly yes

 

這樣作的優缺點:

優勢:保證了主庫寫的能力。

缺點:主庫掛掉後,重啓主庫,而後進行第一次寫的動做後,主庫會先生成rdb文件,而後傳輸給從庫,從而覆蓋掉從庫原先的rdb文件,形成數據丟失。可是第二次寫的時候,主庫會以快照方式直接傳數據給從庫,不會從新生成rdb文件。

解決方案:先複製從庫中的數據到主庫後,再啓動主庫。

使用:

引入jedis包

[html] view plain copy

 在CODE上查看代碼片派生到個人代碼片

  1. <dependency>  
  2.     <groupId>redis.clients</groupId>  
  3.     <artifactId>jedis</artifactId>  
  4.     <version>2.7.3</version>  
  5. </dependency>  

 

 

簡單的寫個類玩玩吧

 

[html] view plain copy

 在CODE上查看代碼片派生到個人代碼片

  1. public class RedisMain {  
  2.       
  3.       
  4.     public static void main(String [] str)  
  5.     {  
  6.           
  7.          Jedis jedis = new Jedis("114.215.125.42",6379);  
  8.          jedis.auth("123");     //密碼認證  
  9.           System.out.println("Connection to server sucessfully");  
  10.           //查看服務是否運行  
  11.           jedis.set("user","namess");  
  12.          // System.out.println("Server is running: "+jedis.ping());  
  13.           System.out.println(jedis.get("user").toString());  
  14.           jedis.set("user","name");  
  15.           System.out.println(jedis.get("user"));  
  16.             
  17.   
  18.     }  
  19.   
  20. }  

Redis完畢

 

 

下面說說Ehcache:

Ehcache的使用:

1.首先引入包

[html] view plain copy

 在CODE上查看代碼片派生到個人代碼片

  1. <dependency>  
  2.            <groupId>net.sf.ehcache</groupId>  
  3.            <artifactId>ehcache-core</artifactId>  
  4.            <version>2.6.6</version>  
  5.        </dependency>  
  6.    
  7.        <dependency>  
  8.            <groupId>org.slf4j</groupId>  
  9.            <artifactId>slf4j-log4j12</artifactId>  
  10.            <version>1.6.6</version>  
  11.        </dependency>  

2.建立一個ehcache.xml文件,裏面配置cache的信息,這個配置是包含了集羣的配置:與192.168.93.129:40001的機器集羣了:Ip爲192.168.93.129機子的配置要將rmiUrls對應的數據改成這個配置文件的機子的IP地址,和對應的緩存名字

[html] view plain copy

 在CODE上查看代碼片派生到個人代碼片

  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.    xsi:noNamespaceSchemaLocation="ehcache.xsd">  
  3.     <cacheManagerPeerProviderFactory   
  4.    class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"  
  5. properties="peerDiscovery=manual,rmiUrls=//192.168.93.129:40001/demoCache"/>           <!--另外一臺機子的ip緩存信息-->  
  6. <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  
  7. properties="hostName=localhost,port=40001,socketTimeoutMillis=2000" />                  <!--hostName表明本機子的ip-->     
  8.   <diskStore path="java.io.tmpdir"/>  
  9.   <defaultCache  
  10.     maxElementsInMemory="10000"  
  11.     maxElementsOnDisk="0"  
  12.     eternal="true"  
  13.     overflowToDisk="true"  
  14.     diskPersistent="false"  
  15.     timeToIdleSeconds="0"  
  16.     timeToLiveSeconds="0"  
  17.     diskSpoolBufferSizeMB="50"  
  18.     diskExpiryThreadIntervalSeconds="120"  
  19.     memoryStoreEvictionPolicy="LFU"  
  20.     >  
  21.      <cacheEventListenerFactory    
  22.                 class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>                
  23.     </defaultCache>  
  24.   <cache name="demoCache"  
  25.     maxElementsInMemory="100"  
  26.     maxElementsOnDisk="0"  
  27.     eternal="false"  
  28.     overflowToDisk="false"  
  29.     diskPersistent="false"  
  30.     timeToIdleSeconds="119"  
  31.     timeToLiveSeconds="119"  
  32.     diskSpoolBufferSizeMB="50"  
  33.     diskExpiryThreadIntervalSeconds="120"  
  34.     memoryStoreEvictionPolicy="FIFO"  
  35.     >  
  36.     <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>          <!--監聽這個cache-->  
  37.     </cache>  
  38. </ehcache>  

 

 

配置完後寫代碼:

放數據:

[html] view plain copy

 在CODE上查看代碼片派生到個人代碼片

  1. @RequestMapping("/testehcache.do")  
  2.     public void testehcache(HttpServletResponse response) throws IOException  
  3.     {  
  4.         URL url = getClass().getResource("ehcache.xml");   
  5.          CacheManager singletonmanager = CacheManager.create(url);  
  6.         Cache cache = singletonmanager.getCache("demoCache");  
  7.         //使用緩存  
  8.         Element element = new Element("key1", "value1");  
  9.         cache.put(element);  
  10.         cache.put(new Element("key2", "value2"));  
  11.          
  12.         response.getWriter().println("我存放了數據");  
  13.     }  

 

拿數據:

 在CODE上查看代碼片派生到個人代碼片

  1. @RequestMapping("/getcache.do")  
  2.     public void getcache(HttpServletResponse response) throws IOException  
  3.     {  
  4.         CacheManager singletonmanager = CacheManager.create();   
  5.         Cache cache = singletonmanager.getCache("demoCache");  
  6.         String one=cache.get("key1").getObjectValue().toString();  
  7.         String two=cache.get("key2").getObjectValue().toString();  
  8.         response.getWriter().println(one+two);  
  9.     }  

 

 

配置集羣后,A機器放數據,在B機器上能拿到數據,B機器放數據,A機器也能夠拿到數據

本文結束!!!!!

源碼來源: minglisoft.cn/technology

相關文章
相關標籤/搜索