转载自:http://blog.csdn.net/jesting1988/article/details/55803915
一:导入依赖
<dependency>
<groupId>Redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
二:简单示例
public class JedisDemo{
public static void main(String [ ] args){
//创建jedis对象
Jedis jedis=new Jedis("127.0.0.1",6379);
//向redis中添加数据
jedis.set("mytest","123");
//从redis中读取数据
String value=jedis.get("mytest");
//关闭连接
jedis.close();
}
}
三:连接池的使用
public class JedisPoolDemo{
public static void main(String [] args){
//构建连接池配置信息
JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
//设置最大连接数
jedisPoolConfig.setMaxTotal(50);
//从连接池中获取连接
Jedis jedis=jedisPool.getResource();
//从redis中读取数据
String value=jedis.get("mytest");
//将连接还到连接池中 jedisPool.returnResource(jedis);
//释放连接池
jedisPool.close();
}
}
四:分片式集群的使用
1.1.1. 分片式集群的使用
publicclass ShardedJedisPoolDemo {
publicstaticvoid main(String[]args) {
// 构建连接池配置信息
JedisPoolConfig poolConfig =new JedisPoolConfig();
// 设置最大连接数
poolConfig.setMaxTotal(50);
// 定义集群信息
List<JedisShardInfo> shards =newArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6379));
shards.add(new JedisShardInfo("192.168.29.112", 6379));
// 定义集群连接池
ShardedJedisPool shardedJedisPool =new ShardedJedisPool(poolConfig,shards);
ShardedJedis shardedJedis =null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
// for (int i = 0; i < 100;i++) {
// shardedJedis.set("key_" + i, "value_" + i);
// }
System.out.println(shardedJedis.get("key_49"));
System.out.println(shardedJedis.get("key_7"));
} catch (Exceptione) {
e.printStackTrace();
} finally {
if (null !=shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
// 关闭连接池
shardedJedisPool.close();
}
}
五:将jedis集成到项目中(一)添加依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
(二)spring和jedis的整合
<!—连接池的配置-->
<bean id =”jedisPoolConfig” class=”redis.clients.jedis.JedisPoolConfig”>
<!—设置最大连接数-->
<propertyname=”maxTotal” value=”${redis.maxTotal}”/>
<!--分片式连接池-->
<bean class=”redis.clients.jedis.ShardedJedisPool”>
<constructor-argindex=”0” ref=”jedisPoolConfig”/>
<constructor-argindex=”1”>
<list>
<beanclass=”redis.clients.jedis.JedisSharedInfo”>
<constructor-argindex=”0” value=”${redis.node1.ip}”/>
<constructor-argindex=”1” value=”${redis.node1.port}”/>
</bean>
</list>
</constructor-arg>
</bean>
(三)封装redisService
1.1.1. 封装RedisService
package com.taotao.manage.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
publicclass RedisService {
@Autowired
private ShardedJedisPoolshardedJedisPool;
/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(Stringkey, String value) {
ShardedJedis shardedJedis =null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
returnshardedJedis.set(key,value);
} finally {
if (null !=shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
/**
* 执行get操作
*
* @param key
* @return
*/
public String get(Stringkey) {
ShardedJedis shardedJedis =null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
returnshardedJedis.get(key);
} finally {
if (null !=shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
}
(四)优化redisservice定义接口
public interface Function<T,E>{
public T callback(E,e);
}
RedisService的实现:
package com.taotao.manage.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
publicclass RedisService {
@Autowired
private ShardedJedisPoolshardedJedisPool;
private <T> Texecute(Function<T, ShardedJedis>fun) {
ShardedJedis shardedJedis =null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
returnfun.callback(shardedJedis);
} finally {
if (null !=shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(final String key, final Stringvalue) {
returnthis.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedise) {
returne.set(key,value);
}
});
}
/**
* 执行get操作
*
* @param key
* @return
*/
public String get(final String key) {
returnthis.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedise) {
returne.get(key);
}
});
}
/**
* 执行删除操作
*
* @param key
* @return
*/
public Long del(final String key) {
returnthis.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedise) {
returne.del(key);
}
});
}
/**
* 设置生存时间,单位为:秒
*
* @param key
* @param seconds
* @return
*/
public Long expire(final String key, final Integerseconds) {
returnthis.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedise) {
returne.expire(key,seconds);
}
});
}
/**
* 执行set操作并且设置生存时间,单位为:秒
*
* @param key
* @param value
* @return
*/
public String set(final String key, final Stringvalue, final Integerseconds) {
returnthis.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedise) {
String str = e.set(key,value);
e.expire(key,seconds);
returnstr;
}
});
}
}
(五)实现缓存逻辑
1.先从缓存中命中,命中则返回
2.没有命中则把结果放入缓存中
public ItemCatResult queryItemCatList(){
//从缓存中命中
String key=“REDIS_KEY”;
String cacheData=this.redisService.get(key);
if(StringUtils.isNotEmpty(cacheData)){
try{
return MAPPER.readValue(cacheData,ItemCatResult.class);
}cache(Exception e){
e.printStatckTrace();
}
}
//如果没有命中,则从数据库中查找,并把结果写入缓存中
try{
//将结果集写入到缓存中
this.redisService.set(REDIS_KEY,MAPPER.writerValueAsString(result),REDIS_TIME);
}cache(Exception e){
e,printStatckTrace();
}
}