第一:二者之间的介绍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