jedis连接池配置
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;
import java.util.LinkedList;
import java.util.List;
public class JedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.password}")
private String password;
public ShardedJedisPool pool = null;
public ShardedJedisPool getJedisPool() {
if (pool == null) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(10);
config.setMaxIdle(10);
config.setMaxWaitMillis(-1);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
JedisShardInfo jedisShardInfo1 = new JedisShardInfo(host, port);
if (password != null) {
jedisShardInfo1.setPassword(password);
}
List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
list.add(jedisShardInfo1);
pool = new ShardedJedisPool(config, list);
}
return pool;
}
}
具体使用jedis
@Autowired
JedisConfig jedisConfig;
ShardedJedis jedis = jedisConfig.getJedisPool().getResource();
jedis.setex(redisKey.getBytes(),CommonConstant.EXPORTTIME, jsonContent);
byte[] bytes = jedis.get(redisKey.getBytes());
实战导出,数据量过大时方式处理时间过长中间存入reids再进行下载
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.entity.ExportParams;
import java.io.ByteArrayOutputStream;
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, secondTitle, sheetName), Class, Collection, exportFields);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
String redisKey = CommonConstant.EXCELPORTPRE;
byte[] jsonContent = bos.toByteArray();
ShardedJedis jedis = jedisConfig.getJedisPool().getResource();
jedis.setex(redisKey.getBytes(),CommonConstant.EXPORTTIME, jsonContent);
workbook.close();
bos.close();
jedis.close();
下载方法
String title ="";
ShardedJedis jedis = jedisConfig.getJedisPool().getResource();
String redisKey = CommonConstant.EXCELPORTPRE;
byte[] bytes = jedis.get(redisKey.getBytes());
try {
response.setContentType("application/force-download");
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(title.getBytes("GBK"), "iso-8859-1"));
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
jedis.del(redisKey.getBytes());
jedis.close();
} catch (IOException e) {
e.printStackTrace();
}