玩了一下 redis ,因此使用 jedis 。。。
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
但是这个jedis 的版本比较高,,, 百度时候的 其他 redisUtil 或者是 整合方案已经过时了。。。 因此 看了 一下jedis 这个版本的代码 改造了一下。 首先是 spring的 redis 配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
>
<context:property-placeholder location="classpath:/conf/app-web.properties" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 可用连接实例的最大数目,默认值为8;
如果赋值为-1,则表示不限制;如果pool已经分配了maxTotal个jedis实例,
则此时pool的状态为exhausted(耗尽)。 -->
<property name="maxTotal" value="${redis.pool.maxActive}" />
<!-- 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- 控制一个pool最少有多少个状态为idle(空闲的)的jedis实例,默认值也是0个。 -->
<property name="minIdle" value="1" />
<!-- 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。
如果超过等待时间,则直接抛出JedisConnectionException; -->
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<!-- 在borrow一个jedis实例时,是否提前进行validate操作;
如果为true,则得到的jedis实例均是可用的 -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<!-- 当调用return Object方法时,是否进行有效性检查 -->
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
</bean>
<!--
redis 集群 ,或者是分布式 使用的连接池 ,这里用不上不管
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.ip}" />
<constructor-arg index="1" value="${redis.port}"
type="int" />
</bean>
</list>
</constructor-arg>
</bean>
-->
<!-- redis 单机使用连接池,初始化spring启动的时候 这个连接池是空的,
不会连接redis的,就算IP和密码错误都没有问题的。除非代码里面初始化了一个redis的连接 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy">
<constructor-arg ref="jedisPoolConfig" />
<constructor-arg value="${redis.ip}" />
<constructor-arg type="int" value="${redis.port}" />
<constructor-arg type="int" value="10000" />
<constructor-arg type="String" value="123456" />
</bean>
</beans>
然后是 redisUtil 当然了这个只是单机的,不算完全,参考即可,,,而且代码 也是百度 改造了,,,分布式的也是差不多的,百度去吧, 只是释放连接的时候,方法换一下,,,低版本的方式已经过时了。
package com.skg.redis;
import org.apache.log4j.Logger;
import com.skg.base.core.helper.SpringHelper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
*
* @author : oumin
* @date : redis 调用类, 单机的目前主要是,,而不是集群的
* @desc :2017年5月4日
*
*/
public class RedisUtil {
private static Logger log = Logger.getLogger(RedisUtil.class);
private static JedisPool jedisPool = null;
public synchronized static Jedis getJedis() {
// 初始化池,获取 spring 容器里面初始化的redis池的对象
if (null == jedisPool) {
jedisPool = (JedisPool) SpringHelper.getBean("jedisPool");
}
try {
if (jedisPool != null) {
Jedis resource = jedisPool.getResource();
return resource;
} else {
log.error("连接池获取redis 连接失败。redis对象为空!");
return null;
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
/**
* 释放jedis资源
*
* @param jedis
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
// jedisPool.returnResource(jedis); 过时了,不要使用这个释放资源
jedis.close();// 使用close回收
}
}
public static void put(String key, String value) {
Jedis jedis = null;
// 从池中获取一个jedis实例
try {
jedis = getJedis();
jedis.set(key, value);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
// 还回到连接池
returnResource(jedis);
}
}
public static String get(String key) {
String value = "";
Jedis jedis = null;
// 从池中获取一个jedis实例
try {
jedis = getJedis();
value = jedis.get(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
// 还回到连接池
returnResource(jedis);
}
return value;
}
public static boolean exists(String key) {
boolean bool = false;
Jedis jedis = null;
// 从池中获取一个jedis实例
try {
jedis = getJedis();
bool = jedis.exists(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
// 还回到连接池
returnResource(jedis);
}
return bool;
}
public static void incr(String key) {
Jedis jedis = null;
// 从池中获取一个jedis实例
try {
jedis = getJedis();
jedis.incr(key);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
// 还回到连接池
returnResource(jedis);
}
}
public static void sadd(String key, String members) {
Jedis jedis = null;
// 从池中获取一个jedis实例
try {
jedis = getJedis();
jedis.sadd(key, members);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
// 还回到连接池
returnResource(jedis);
}
}
public static boolean sismember(String key, String members) {
Jedis jedis = null;
boolean bool = false;
// 从池中获取一个jedis实例
try {
jedis = getJedis();
bool = jedis.sismember(key, members);
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
// 还回到连接池
returnResource(jedis);
}
return bool;
}
}
以后 我继续玩redis的时候再继续完善了。当然了能够开始玩redis的,相信看了一下redis的 pool的 源码也是知道在新版本里面是怎么使用和配置的。