码迷,mamicode.com
首页 > 编程语言 > 详细

spring redis 整合

时间:2016-12-22 22:47:33      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:front   sse   sch   framework   加载   tac   except   style   ann   

<import resource="cache/applicationContext-redis.xml"/>

applicationContext-redis.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
">

<!-- 集群的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>

<!-- 集群连接池 -->
<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.node1.host}"/>
<constructor-arg index="1" value="${redis.node1.port}"/>
</bean>
</list>
</constructor-arg>
</bean>

</beans>

<!-- 加载 httpclient 文件-->
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:/config/redis.properties"/>

<cache:annotation-driven/>




redis.maxTotal=100
redis.node1.host=127.0.0.1
redis.node1.port=6379




java code


//规则 : 项目名_模块名_业务名
private static final String REDIS_KEY = "MYPROJECT_FRONT_INDEX_DETAIL_";
private static final Integer REDIS_TIME = 60 * 60 * 24;
     // 从缓存中命中
// String key = REDIS_KEY + "weather";
// String cacheData = this.redisService.get(key);
// if (StringUtils.isNotEmpty(cacheData)) {
// //命中
// System.out.println(cacheData);
// } else {







@Service
public class RedisService {

@Autowired(required = false)//运行的Spring环境中如果存在就注入,没有就忽略
private ShardedJedisPool shardedJedisPool;

private <T> T execute(Function<T, ShardedJedis> fun) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
return null;
}

/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}

/**
* 执行GET操作
*
* @param key
* @return
*/
public String get(final String key) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}

/**
* 执行DEL操作
*
* @param key
* @return
*/
public Long del(final String key) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.del(key);
}
});
}

/**
* 设置生存时间,单位为秒
*
* @param key
* @param seconds
* @return
*/
public Long expire(final String key, final Integer seconds) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.expire(key, seconds);
}
});
}

/**
* 执行set操作并且设置生存时间,单位为秒
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value, final Integer seconds) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
String str = e.set(key, value);
e.expire(key, seconds);
return str;
}
});
}

}




maven

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>




做个记录,打算用spring data redis







spring redis 整合

标签:front   sse   sch   framework   加载   tac   except   style   ann   

原文地址:http://www.cnblogs.com/yaoyao66123/p/6212971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!