目录
1:redis连接池配置
2:简单案例使用
1:redis连接池配置
1:redis.properties配置文件
redis.hostname=39.105.173.150
redis.port=6379
redis.database=0
redis.pool.maxActive=100
redis.pool.maxIdle=20
redis.pool.maxWait=3000
redis.pool.testOnBorrow=true
2:spring_redis.xml配置文件
<!-- Redis连接池的设置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 控制一个pool可分配多少个jedis实例 -->
<property name="maxTotal" value="${redis.pool.maxActive}" />
<!-- 连接池中最多可空闲maxIdle个连接 ,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。 -->
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间则抛出异常 -->
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<!-- 在获取连接的时候检查有效性 -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- 创建jedisPoolWriper类 构造函数注入属性文件-->
<bean id="jedisPoolWriper" class="com.thit.util.JedisPoolWriper">
<constructor-arg index="0" ref="jedisPoolConfig"></constructor-arg>
<constructor-arg index="1" value="${redis.hostname}" ></constructor-arg>
<constructor-arg index="2" value="${redis.port}" type="int"></constructor-arg>
</bean>
<!-- 创建工具类,注入jedisPoolWriper -->
<bean id="jedisUtil" class="com.thit.util.JedisUtil" scope="singleton">
<property name="jedispool" ref="jedisPoolWriper"></property>
</bean>
3:JedisPoolConfig代码实现
public class JedisPoolWriper {
//创建jedis对象
JedisPool jedisPool;
//构造函数注入JedisPoolConfig和地址和端口信息
public JedisPoolWriper(final JedisPoolConfig jedisPoolConfig,
final String host, final int port) {
try {
jedisPool=new JedisPool(jedisPoolConfig,host,port);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public JedisPool getJedisPool() {
return jedisPool;
}
public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
}
4:JedisUtil代码实现,用来从外部获取操作对象jedis
public class JedisUtil {
private JedisPool jedispool;
public JedisPool getJedispool() {
return jedispool;
}
//通过setx注入poolWriper 获取jedispool
public void setJedispool(JedisPoolWriper poolWriper) {
this.jedispool = poolWriper.getJedisPool();
}
//获取jedis对象
public Jedis getJedis() {
return jedispool.getResource();
}
}
2:简单案例使用
在serviceImpl中使用redis代码如下:
//自动注入jedisUtil
@Autowired
private JedisUtil JedisUtil;
//serviceImpl的方法
@Override
public List<Area> selectAll() {
ObjectMapper mapper=new ObjectMapper();
List< Area> list=null;
//获取jedis,并且获取key的值
//如果不存在,查询数据库,放到reids中,存在直接读取
//获取jedis
Jedis jedis=JedisUtil.getJedis();
boolean b=jedis.exists(AreaListKey);
if(b) {
//存在,读取redis,转换为list
String string=jedis.get(AreaListKey);
JavaType javaType=mapper.getTypeFactory().
constructParametricType(ArrayList.class, Area.class);
try {
list=mapper.readValue(string, javaType);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}else {
//不存在查询,并且放到jedis中
list=areaDao.selectAll();
try {
jedis.set(AreaListKey, mapper.writeValueAsString(list));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}
return list;
}