第一:二者之間的介紹html
Redis:屬於獨立的運行程序,須要單獨安裝後,使用Java中的Jedis來操縱。由於它是獨立,因此若是你寫個單元測試程序,放一些數據在Redis中,而後又寫一個程序去拿數據,那麼是能夠拿到這個數據的。,java
ehcache:與Redis明顯不一樣,它與java程序是綁在一塊兒的,java程序活着,它就活着。譬如,寫一個獨立程序放數據,再寫一個獨立程序拿數據,那麼是拿不到數據的。只能在獨立程序中才能拿到數據。redis
第二:使用及各類配置:緩存
二者均可以集羣:app
1.Redis能夠作主歷來集羣,例如,在A電腦上裝個Redis,做爲主庫;在其餘電腦上裝Redis,做爲從庫;這樣主庫擁有讀和寫的功能,而從庫只擁有讀的功能。每次主庫的數據都會同步到從庫中。socket
1.默認方式啓動ide
[html] view plain copy測試
- Linux下使用Redis
- 安裝:從官網上下載tar.gz格式的包,而後使用tar zxvf redis-2.8.24.tar.gz命令解壓,而後進入Redis文件夾目錄下的src目錄,使用make編譯一下
- 1.開啓:進入/usr/local/redis-3.2.1/src
- 而後./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
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.7.3</version>
- </dependency>
簡單的寫個類玩玩吧
[html] view plain copy
- public class RedisMain {
- public static void main(String [] str)
- {
- Jedis jedis = new Jedis("114.215.125.42",6379);
- jedis.auth("123"); //密碼認證
- System.out.println("Connection to server sucessfully");
- //查看服務是否運行
- jedis.set("user","namess");
- // System.out.println("Server is running: "+jedis.ping());
- System.out.println(jedis.get("user").toString());
- jedis.set("user","name");
- System.out.println(jedis.get("user"));
- }
- }
Redis完畢
下面說說Ehcache:
Ehcache的使用:
1.首先引入包
[html] view plain copy
- <dependency>
- <groupId>net.sf.ehcache</groupId>
- <artifactId>ehcache-core</artifactId>
- <version>2.6.6</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.6.6</version>
- </dependency>
2.建立一個ehcache.xml文件,裏面配置cache的信息,這個配置是包含了集羣的配置:與192.168.93.129:40001的機器集羣了:Ip爲192.168.93.129機子的配置要將rmiUrls對應的數據改成這個配置文件的機子的IP地址,和對應的緩存名字
[html] view plain copy
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd">
- <cacheManagerPeerProviderFactory
- class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
- properties="peerDiscovery=manual,rmiUrls=//192.168.93.129:40001/demoCache"/> <!--另外一臺機子的ip緩存信息-->
- <cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
- properties="hostName=localhost,port=40001,socketTimeoutMillis=2000" /> <!--hostName表明本機子的ip-->
- <diskStore path="java.io.tmpdir"/>
- <defaultCache
- maxElementsInMemory="10000"
- maxElementsOnDisk="0"
- eternal="true"
- overflowToDisk="true"
- diskPersistent="false"
- timeToIdleSeconds="0"
- timeToLiveSeconds="0"
- diskSpoolBufferSizeMB="50"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LFU"
- >
- <cacheEventListenerFactory
- class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
- </defaultCache>
- <cache name="demoCache"
- maxElementsInMemory="100"
- maxElementsOnDisk="0"
- eternal="false"
- overflowToDisk="false"
- diskPersistent="false"
- timeToIdleSeconds="119"
- timeToLiveSeconds="119"
- diskSpoolBufferSizeMB="50"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="FIFO"
- >
- <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/> <!--監聽這個cache-->
- </cache>
- </ehcache>
配置完後寫代碼:
放數據:
[html] view plain copy
- @RequestMapping("/testehcache.do")
- public void testehcache(HttpServletResponse response) throws IOException
- {
- URL url = getClass().getResource("ehcache.xml");
- CacheManager singletonmanager = CacheManager.create(url);
- Cache cache = singletonmanager.getCache("demoCache");
- //使用緩存
- Element element = new Element("key1", "value1");
- cache.put(element);
- cache.put(new Element("key2", "value2"));
- response.getWriter().println("我存放了數據");
- }
拿數據:
- @RequestMapping("/getcache.do")
- public void getcache(HttpServletResponse response) throws IOException
- {
- CacheManager singletonmanager = CacheManager.create();
- Cache cache = singletonmanager.getCache("demoCache");
- String one=cache.get("key1").getObjectValue().toString();
- String two=cache.get("key2").getObjectValue().toString();
- response.getWriter().println(one+two);
- }
配置集羣后,A機器放數據,在B機器上能拿到數據,B機器放數據,A機器也能夠拿到數據
本文結束!!!!!
源碼來源: minglisoft.cn/technology