java使用jedis存储向redis存储二进制流(excel存储redis)

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;

/**
* jedis连接池配置文件
* @param host 配置文件中redis连接host
* @param port 配置文件中redis连接port
* @param password 配置文件中redis连接password
**/
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) {
    
    
            // 配置Redis信息
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(10); //在指定时刻通过pool能够获取到的最大的连接的jedis个数
            config.setMaxIdle(10);  //最大能够保持idle的数量,控制一个pool最多有多少个状态为idle的jedis实例
            config.setMaxWaitMillis(-1);  //当连接池内的连接耗尽时,getBlockWhenExhausted为true时,连接会阻塞,超过了阻塞的时间(设定的maxWaitMillis,单位毫秒)时会报错
            config.setTestOnBorrow(true); //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;默认是false
            config.setTestOnReturn(true);

            // 集群
            JedisShardInfo jedisShardInfo1 = new JedisShardInfo(host, port);
            if (password != null) {
    
    
                // 设置Redis的密码
                jedisShardInfo1.setPassword(password);
            }

            List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
            list.add(jedisShardInfo1);
            pool = new ShardedJedisPool(config, list);
        }
        return pool;
    }
}

具体使用jedis

@Autowired
JedisConfig jedisConfig; //使用注解注入到当前使用页面

/**获取jedis连接信息**/
ShardedJedis jedis = jedisConfig.getJedisPool().getResource();
/**
* setex参数(byte[]类型的key,存放时间(单位秒),二进制byte值)
* 方法挺多的就不一一介绍了
**/
jedis.setex(redisKey.getBytes(),CommonConstant.EXPORTTIME, jsonContent);
/**
* 获取方法跟redis获取方法差不多
**/
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;

/**
* @param title 导出内容第一行标题
* @param secondTitle 导出内容第二行标题
* @param sheetName 导出excel标签名
* @param Class 导出实体对应class
* @param Collection 导出list
* @param exportFields 导出字段(可为空)
**/
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, secondTitle, sheetName), Class, Collection, exportFields);
/**
* 将workbook转为二进制流存储
**/
ByteArrayOutputStream bos = new ByteArrayOutputStream();
workbook.write(bos);
String redisKey = CommonConstant.EXCELPORTPRE;//自定义key前缀,可以加上自己的动态参数,以防重复
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();
}

猜你喜欢

转载自blog.csdn.net/weixin_43876684/article/details/121516422