<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.6.0version>
dependency>
加入Spring和Jedis的整合配置:
具体配置如下:
加入外部redis配置文件:
配置分析:
我们先配置redis.clients.jedis.ShardedJedisPool集群连接池
然后里面有两个构造参数
ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig, shards);
分别是poolConfig和shards,连接池配置信息和集群信息。
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
public class RedisService {
@Autowired
private ShardedJedisPool shardedJedisPool;
/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(String key, String value) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.set(key, value);
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
/**
* 执行get操作
*
* @param key
* @return
*/
public String get(String key) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return shardedJedis.get(key);
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
}
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
public class RedisService {
@Autowired
private ShardedJedisPool shardedJedisPool;
private T execute(Function fun) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value) {
return this.execute(new Function() {
@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() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**
* 执行删除操作
*
* @param key
* @return
*/
public Long del(final String key) {
return this.execute(new Function() {
@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() {
@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() {
@Override
public String callback(ShardedJedis e) {
String str = e.set(key, value);
e.expire(key, seconds);
return str;
}
});
}
}
原则:缓存逻辑不能影响原有的业务逻辑执行。
package com.taotao.manage.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.bean.ItemCatData;
import com.taotao.common.bean.ItemCatResult;
import com.taotao.common.service.RedisService;
import com.taotao.manage.mapper.ItemCatMapper;
import com.taotao.manage.pojo.ItemCat;
@Service
public class ItemCatService extends BaseService{
@Autowired
private ItemCatMapper itemCatMapper;
// public List queryItemCat(Long parentId) {
// ItemCat record = new ItemCat();
// record.setParentId(parentId);
// return this.itemCatMapper.select(record );
// }
// @Override
// public Mapper getMapper() {
// // TODO Auto-generated method stub
// return this.itemCatMapper;
// }
@Autowired
private RedisService redisService;
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String REDIS_KEY = "TAOTAO_MANAGE_ITEM_CAT_API"; // 规则:项目名_模块名_业务名
private static final Integer REDIS_TIME = 60 * 60 * 24 * 30 * 3;
/**
* 全部查询,并且生成树状结构
*
* @return
*/
public ItemCatResult queryAllToTree() {
ItemCatResult result = new ItemCatResult();
try {
// 先从缓存中命中,如果命中就返回,没有命中继续执行
String cacheData = this.redisService.get(REDIS_KEY);
if (StringUtils.isNotEmpty(cacheData)) {
// 命中
return MAPPER.readValue(cacheData, ItemCatResult.class);
}
} catch (Exception e) {
e.printStackTrace();
}
// 全部查出,并且在内存中生成树形结构
List cats = super.queryAll();
// 转为map存储,key为父节点ID,value为数据集合
Map> itemCatMap = new HashMap>();
for (ItemCat itemCat : cats) {
if (!itemCatMap.containsKey(itemCat.getParentId())) {
itemCatMap.put(itemCat.getParentId(), new ArrayList());
}
itemCatMap.get(itemCat.getParentId()).add(itemCat);
}
// 封装一级对象
List itemCatList1 = itemCatMap.get(0L);
for (ItemCat itemCat : itemCatList1) {
ItemCatData itemCatData = new ItemCatData();
itemCatData.setUrl("/products/" + itemCat.getId() + ".html");
itemCatData.setName("" + itemCat.getName() + "");
result.getItemCats().add(itemCatData);
if (!itemCat.getIsParent()) {
continue;
}
// 封装二级对象
List itemCatList2 = itemCatMap.get(itemCat.getId());
List itemCatData2 = new ArrayList();
itemCatData.setItems(itemCatData2);
for (ItemCat itemCat2 : itemCatList2) {
ItemCatData id2 = new ItemCatData();
id2.setName(itemCat2.getName());
id2.setUrl("/products/" + itemCat2.getId() + ".html");
itemCatData2.add(id2);
if (itemCat2.getIsParent()) {
// 封装三级对象
List itemCatList3 = itemCatMap.get(itemCat2.getId());
List itemCatData3 = new ArrayList();
id2.setItems(itemCatData3);
for (ItemCat itemCat3 : itemCatList3) {
itemCatData3.add("/products/" + itemCat3.getId() + ".html|" + itemCat3.getName());
}
}
}
if (result.getItemCats().size() >= 14) {
break;
}
}
try {
// 将数据库查询结果集写入到缓存中
this.redisService.set(REDIS_KEY, MAPPER.writeValueAsString(result), REDIS_TIME);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
由于我们封装的RedisService是通用工具,所以我们还是把它移动到common中:
导入依赖:
package com.taotao.common.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
public class RedisService {
@Autowired(required = false)//从Spring容器中查找bean,找到就注入,找不到就忽略
private ShardedJedisPool shardedJedisPool;
private T execute(Function fun) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value) {
return this.execute(new Function() {
@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() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**
* 执行删除操作
*
* @param key
* @return
*/
public Long del(final String key) {
return this.execute(new Function() {
@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() {
@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() {
@Override
public String callback(ShardedJedis e) {
String str = e.set(key, value);
e.expire(key, seconds);
return str;
}
});
}
}
package com.taotao.common.service;
public interface Function {
public T callback(E e);
}
现在RedisService是在共用的Common中,而现在RedisService的使用对我们的环境有要求,必须在Spring容器中要有ShardedJedisPool这个bean才能使用RedisService,如果有个工程依赖了Common,但是这个工程中的Spring容器中并没有ShardedJedisPool这个bean,那么他就启动不起来。所以作为一个共用的Common工程,不应该对使用的环境有要求。所以我们需要设置required = false,告诉这个bean不是必须的,从Spring容器中查找bean,找到就注入,找不到就忽略。