JAVA 中 Redis与ehcache对比与使用
第一:两者之间的介绍
Redis:属于独立的运行程序,需要单独安装后,使用Java中的 Jedis 来操纵。因为它是独立,所以如果你写个单元测试程序,放一些数据在 Redis 中,然后又写一个程序去拿数据,那么是可以拿到这个数据的。,
ehcache:与 Redis 明显不同,它与 java 程序是绑在一起的,java 程序活着,它就活着。譬如,写一个独立程序放数据,再写一个独立程序拿数据,那么是拿不到数据的。只能在独立程序中才能拿到数据。
第二:使用及各种配置:
两者都可以集群:
1.Redis 可以做主从来集群,例如,在 A 电脑上装个 Redis,作为主库;在其他电脑上装 Redis,作为从库;这样主库拥有读和写的功能,而从库只拥有读的功能。每次主库的数据都会同步到从库中。
1. 默认方式启动
[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.conf
port 6379 // 端口修改
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