redis在项目中的使用(单机版、集群版)
1.下载jar包:jedis-2.6.2.jar
2.代码:
JedisDao.java:
package com.test.www.dao; public interface JedisDao { //抽取jedis访问redis服务器常用方法 //String public String set(String key,String value); public String get(String key); //自增 public Long incr(String key); //自减 public Long decr(String key); //过期时间 public Long expire(String key, int seconds); //查询过期剩余时间 public Long ttl(String key); //HASH public Long hset(String key, String field, String value); public String hget(String key, String field); //删除 public Long hdel(String key, String field); // public String setObject(String key, Object value);Object改成泛型T public <T> String setObject(String key, T value); public Object getObject(String key); }
SingleJedisDaoImpl.java:(单机版)
/*package com.test.www.dao.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import com.test.www.dao.JedisDao; import com.test.www.utils.SerializeUtil; @Repository public class SingleJedisDaoImpl implements JedisDao { //注入单机版jedis连接池对象 @Autowired private JedisPool jedisPool; @Override public String set(String key, String value) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); String set = jedis.set(key, value); return set; } @Override public String get(String key) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); String value = jedis.get(key); return value; } @Override public Long incr(String key) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); Long incr = jedis.incr(key); return incr; } @Override public Long decr(String key) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); Long decr = jedis.decr(key); return decr; } @Override public Long expire(String key, int seconds) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); Long expire = jedis.expire(key, seconds); return expire; } @Override public Long ttl(String key) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); Long ttl = jedis.ttl(key); return ttl; } @Override public Long hset(String key, String field, String value) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); Long hset = jedis.hset(key, field, value); return hset; } @Override public String hget(String key, String field) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); String hget = jedis.hget(key, field); return hget; } @Override public Long hdel(String key, String field) { // TODO Auto-generated method stub Jedis jedis = jedisPool.getResource(); Long hdel = jedis.hdel(key, field); return hdel; } @Override public <T> String setObject(String key, T value) { Jedis jedis = jedisPool.getResource(); String set = jedis.set(SerializeUtil.serialize(key), SerializeUtil.serialize(value)); return set; } @Override public Object getObject(String key) { Jedis jedis = jedisPool.getResource(); byte[] value = jedis.get(SerializeUtil.serialize(key)); return SerializeUtil.unserialize(value); } } */
ClusterJedisDaoImpl.java(集群)(集群的搭建详见redis集群搭建,地址:https://www.cnblogs.com/super-chao/p/9329018.html)
package com.test.www.dao.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; import com.test.www.dao.JedisDao; import com.test.www.utils.SerializeUtil; /** * redis集群 * @className ClusterJedisDaoImpl * @author liuyachao * @date 2018-7-18 */ @Repository public class ClusterJedisDaoImpl implements JedisDao { //注解集群对象 @Autowired private JedisCluster jc; @Override public String set(String key, String value) { // TODO Auto-generated method stub String set = jc.set(key, value); return set; } @Override public String get(String key) { // TODO Auto-generated method stub String string = jc.get(key); System.out.println(jc.getClusterNodes()); //System.out.println(jc.getDB()); //System.out.println(jc.info()); return string; } @Override public Long incr(String key) { // TODO Auto-generated method stub Long incr = jc.incr(key); return incr; } @Override public Long decr(String key) { // TODO Auto-generated method stub Long decr = jc.decr(key); return decr; } @Override public Long expire(String key, int seconds) { // TODO Auto-generated method stub Long expire = jc.expire(key, seconds); return expire; } @Override public Long ttl(String key) { // TODO Auto-generated method stub Long ttl = jc.ttl(key); return ttl; } @Override public Long hset(String key, String field, String value) { // TODO Auto-generated method stub Long hset = jc.hset(key, field, value); return hset; } @Override public String hget(String key, String field) { // TODO Auto-generated method stub String value = jc.hget(key, field); return value; } @Override public Long hdel(String key, String field) { // TODO Auto-generated method stub Long hdel = jc.hdel(key, field); return hdel; } @Override public <T> String setObject(String key, T value) { // TODO Auto-generated method stub return null; } @Override public Object getObject(String key) { // TODO Auto-generated method stub return null; } }
PageController.java:
package com.test.www.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/page") public class PageController { /** * 跳转到大区欢迎页 * @param map * @return */ @RequestMapping("/toArea") public String toArea( ModelMap map ){ return "area"; } @RequestMapping("/redis") public String redis( ModelMap map ){ return "redis"; } }
配置:
applicationContext-redis.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <!-- 读取配置文件信息 --> <!-- <context:property-placeholder location="classpath:redis.properties" file-encoding="UTF-8" ignore-unresolvable="true"/> --> <!-- Jedis 连接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> </bean> <!-- 单机版 --> <!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg ref="jedisPoolConfig"/> <constructor-arg value="${jedis.host}" type="java.lang.String"/> <constructor-arg type="int" value="${jedis.port}"/> </bean> --> <!-- 集群 --> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <!-- <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> --> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6384"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6383"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6382"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6381"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6380"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="127.0.0.1"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg> </bean> </beans>
redis.properties:
# 控制一个pool可分配多少个jedis实例 redis.pool.maxTotal=1000 # 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 redis.pool.maxIdle=200 # 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException redis.pool.maxWaitMillis=2000 #在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的 redis.pool.testOnBorrow=true # redis 单机 # 单机 host jedis.host=127.0.0.1 # 单机 port jedis.port=6379
页面:
redis.jsp:
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <% String ctxPath = request.getContextPath(); request.setAttribute("ctxpath", ctxPath);//项目根路径 %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> <script src="${ctxpath}/js/jquery-1.11.3.min.js"></script> <script type="text/javascript"> $(function() { }); </script> </head> <body> <div id="area"></div> redis中缓存的用户名:${username } <%-- <a href="${ctxpath}/redis/visitHistory.html">redis存数据</a> --%> <form action="${ctxpath}/redis/visitHistory.html" method="post"> username:<input type="text" name="username" /> <br> <input type="submit" value="redis存数据"> </form> <a href="${ctxpath}/redis/getHistory.html">redis获取数据</a> </body> </html>
效果图:
单机、集群可相互切换测试,如果使用单机的话,把单机的注释放开,集群注释掉。
单机实现了redis存放各种类型数据(字节流处理)set(String,Object)。
集群版存放为set(String,String),可转换为json串存放数据,取出数据再解析json串。