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

2022-07-07 07:59:25

jedis连接池配置

importorg.springframework.beans.factory.annotation.Value;importorg.springframework.stereotype.Component;importorg.springframework.stereotype.Service;importredis.clients.jedis.JedisPoolConfig;importredis.clients.jedis.JedisShardInfo;importredis.clients.jedis.ShardedJedisPool;importjava.util.LinkedList;importjava.util.List;/**
* jedis连接池配置文件
* @param host 配置文件中redis连接host
* @param port 配置文件中redis连接port
* @param password 配置文件中redis连接password
**/publicclassJedisConfig{@Value("${spring.redis.host}")privateString host;@Value("${spring.redis.port}")privateString port;@Value("${spring.redis.password}")privateString password;publicShardedJedisPool pool=null;publicShardedJedisPoolgetJedisPool(){if(pool==null){// 配置Redis信息JedisPoolConfig config=newJedisPoolConfig();
            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=newJedisShardInfo(host, port);if(password!=null){// 设置Redis的密码
                jedisShardInfo1.setPassword(password);}List<JedisShardInfo> list=newLinkedList<JedisShardInfo>();
            list.add(jedisShardInfo1);
            pool=newShardedJedisPool(config, list);}return pool;}}

具体使用jedis

@AutowiredJedisConfig 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再进行下载

importorg.jeecgframework.poi.excel.ExcelExportUtil;importorg.apache.poi.ss.usermodel.Workbook;importorg.jeecgframework.poi.excel.entity.ExportParams;importjava.io.ByteArrayOutputStream;/**
* @param title 导出内容第一行标题
* @param secondTitle 导出内容第二行标题
* @param sheetName 导出excel标签名
* @param Class 导出实体对应class
* @param Collection 导出list
* @param exportFields 导出字段(可为空)
**/Workbook workbook=ExcelExportUtil.exportExcel(newExportParams(title, secondTitle, sheetName),Class,Collection, exportFields);/**
* 将workbook转为二进制流存储
**/ByteArrayOutputStream bos=newByteArrayOutputStream();
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="+newString(title.getBytes("GBK"),"iso-8859-1"));// 设置文件名ServletOutputStream outputStream= response.getOutputStream();
    outputStream.write(bytes);
    jedis.del(redisKey.getBytes());
    jedis.close();}catch(IOException e){
    e.printStackTrace();}
  • 作者:sc_爬坑之路
  • 原文链接:https://blog.csdn.net/weixin_43876684/article/details/121516422
    更新时间:2022-07-07 07:59:25