准备工作:
jedis连接
添加jar支持:
<!-- redis依赖 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.0.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.3</version> </dependency>
如果redis服务想被外部链接访问,需要修改redis.conf配置 (69行)
69 行上面bind 相应本机的IP
136 行 daemonize yes
启动服务:
./bin/redis-server bin/redis.conf
命令测试:
./bin/redis-cli -h 192.168.182.20
编码测试:
Jedis jedis=new Jedis("192.168.23.111",6379);
//jedis.ping();
JedisPool jedisPool=new JedisPool("192.168.23.111",6379);
Jedis resource = jedisPool.getResource();
单机版
1,在web.xml里面添加
配置多个spring配置文件的路径
2,添加spring-redis.xml
<!-- 开启扫描 --> <context:component-scan base-package="com.duguangming.util"></context:component-scan> <!-- 初始化Jedis连接池--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大连接数, 默认8个--> <property name="maxTotal" value="50" /> <!--最大空闲连接数, 默认8--> <property name="maxIdle" value="10" /> <!--连接时的最大等待毫秒数--> <property name="maxWaitMillis" value="1000" /> <!--获得一个jedis实例的时候是否检查连接可用性--> <property name="testOnBorrow" value="true" /> </bean> <!-- 把jedisPool交给spring管理 --> <bean class="redis.clients.jedis.JedisPool" > <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> <constructor-arg name="host" value="192.168.239.20"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> </bean>
3,编写JedisUtil及实现类,序列化工具
public interface JedisUtil { /** * 放入缓存 * @param key * @param value */ void putObject(Object key, Object value); /** * 清除缓存 * @param arg0 * @return */ Object removeObject(Object arg0); /** * 从缓存中获取 * @param arg0 * @return */ Object getObject(Object arg0); }
@Component public class JedisUtilImpl implements JedisUtil{ @Autowired private JedisPool jedisPool; //放入缓存 public void putObject(Object key, Object value) { // TODO Auto-generated method stub Jedis resource = jedisPool.getResource(); resource.set(SerializeUtil.serialize(key.toString()), SerializeUtil.serialize(value)); resource.close(); } //清楚缓存 public Object removeObject(Object arg0) { // TODO Auto-generated method stub Jedis resource = jedisPool.getResource(); Object expire = resource.expire( SerializeUtil.serialize(arg0.toString()), 0); resource.close(); return expire; } //从缓存中取 public Object getObject(Object arg0) { // TODO Auto-generated method stub Jedis resource = jedisPool.getResource(); Object value = SerializeUtil.unserialize(resource.get( SerializeUtil.serialize(arg0.toString()))); resource.close(); return value; } }
序列化工具:
public class SerializeUtil { public static byte[] serialize(Object object) { ObjectOutputStream oos = null; ByteArrayOutputStream baos = null; try { // 序列化 baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(object); byte[] bytes = baos.toByteArray(); return bytes; } catch (Exception e) { e.printStackTrace(); } return null; } public static Object unserialize(byte[] bytes) { if (bytes == null) return null; ByteArrayInputStream bais = null; try { // 反序列化 bais = new ByteArrayInputStream(bytes); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception e) { e.printStackTrace(); } return null; } }
4,在服务层注入接口测试缓存功能
service层实现类具体代码:
public List<Class> listAll() { List<Class> classListByParam = null; //从缓存中获取班级列表 Object classList = jedisUtil.getObject("ddd"); //判断缓存中是否存在 if(classList!=null){//不空,强转返回 System.out.println("redis缓存中存在,直接返回"); classListByParam= (List<Class>)SerializeUtil.unserialize((byte[])classList); }else{ System.out.println("redis缓存中不存在,从数据库中取出,并且放入缓存"); //查询数据库,取出 classListByParam = classDAO.listAll(); //放入redis缓存 jedisUtil.putObject("ddd", SerializeUtil.serialize(classListByParam)); } return classListByParam; }
redis集群版(实体类需要实现序列化接口)
注意:在项目中使用集群( 要求jedis2.8以上版本,更换pom版本)
ssm项目中,spring-redis-cluster.xml主配置文件的配置
<!--扫描包--> <context:component-scan base-package="com.duguangming.util"></context:component-scan> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <!--配置集群任意一台节点就可以--> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.20"/> <constructor-arg name="port" value="7001"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.20"/> <constructor-arg name="port" value="7002"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.20"/> <constructor-arg name="port" value="7003"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.21"/> <constructor-arg name="port" value="7004"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.21"/> <constructor-arg name="port" value="7005"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.239.21"/> <constructor-arg name="port" value="7006"/> </bean> </set> </constructor-arg> </bean> </beans>
ssm项目中,web.xml的路径配置
ssm项目中,JedisUtilImpl实现类的
@Component public class JedisUtilImpl implements JedisUtil{ @Autowired private JedisCluster jedisCluster; public void putObject(Object key, Object value) { jedisCluster.set(SerializeUtil.serialize(key),SerializeUtil.serialize(value)); } public Object removeObject(Object arg0) { return jedisCluster.expire(SerializeUtil.serialize(arg0),1); } public Object getObject(Object arg0) { byte[] bytes = jedisCluster.get(SerializeUtil.serialize(arg0)); return SerializeUtil.unserialize(bytes); } }