1 import com.bdzl.common.validation.SaveAction; 2 import lombok.Data; 3 import org.hibernate.validator.constraints.NotBlank; 4 5 /** 6 * @program: mse 7 * @description: 草稿箱 8 * @author: @Dog_Elder 9 * @create: 2021-03-23 17:48 10 **/ 11 @Data 12 public class DraftBoxDTO { 13 /** 14 * 类型中的 唯一标识 用户id 15 */ 16 @NotBlank(message = "唯一标识不能为空 例如 用户id ",groups = {SaveAction.class,Inquiry.class}) 17 private String id; 18 /** 19 * 类型 对应 redis的kye 例如 packageNames:methodName: 20 */ 21 @NotBlank(message = "类型不能为空 对应 redis的kye 例如 packageNames:methodName:",groups = {SaveAction.class,Inquiry.class}) 22 private String type; 23 /** 24 * 参数值 数据 25 */ 26 @NotBlank(message = "数据不能为空",groups = {SaveAction.class}) 27 private String str; 28 /** 29 * 过期时间 30 */ 31 private Integer expireDate; 32 33 /** 34 * 查询 35 */ 36 public interface Inquiry{} 37 38 /** 39 * 保存 40 */ 41 public interface SaveAction{} 42 }
1 import com.dog.common.utils.StringUtils; 2 import lombok.Data; 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 7 import org.springframework.boot.context.properties.ConfigurationProperties; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 import redis.clients.jedis.JedisPool; 11 import redis.clients.jedis.JedisPoolConfig; 12 13 /** 14 * Jedis配置,项目启动注入JedisPool 15 * http://www.cnblogs.com/GodHeng/p/9301330.html 16 * @author dolyw.com 17 * @date 2018/9/5 10:35 18 */ 19 @Configuration 20 @EnableAutoConfiguration 21 @ConfigurationProperties(prefix = "redis") 22 @Data 23 public class JedisConfig { 24 25 /** 26 * logger 27 */ 28 private static final Logger logger = LoggerFactory.getLogger(JedisConfig.class); 29 30 private String host; 31 32 private int port; 33 34 private String password; 35 36 private int timeout; 37 38 @Value("${redis.pool.max-active}") 39 private int maxActive; 40 41 @Value("${redis.pool.max-wait}") 42 private int maxWait; 43 44 @Value("${redis.pool.max-idle}") 45 private int maxIdle; 46 47 @Value("${redis.pool.min-idle}") 48 private int minIdle; 49 50 @Bean 51 public JedisPool redisPoolFactory() { 52 try { 53 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); 54 jedisPoolConfig.setMaxIdle(maxIdle); 55 jedisPoolConfig.setMaxWaitMillis(maxWait); 56 jedisPoolConfig.setMaxTotal(maxActive); 57 // 密码为空设置为null 58 if (StringUtils.isBlank(password)) { 59 password = null; 60 } 61 JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password); 62 logger.info("初始化Redis连接池JedisPool成功!地址: " + host + ":" + port); 63 return jedisPool; 64 } catch (Exception e) { 65 logger.error("初始化Redis连接池JedisPool异常:" + e.getMessage()); 66 } 67 return null; 68 } 69 70 public String getHost() { 71 return host; 72 } 73 74 public void setHost(String host) { 75 this.host = host; 76 } 77 78 public int getPort() { 79 return port; 80 } 81 82 public void setPort(int port) { 83 this.port = port; 84 } 85 86 public String getPassword() { 87 return password; 88 } 89 90 public void setPassword(String password) { 91 this.password = password; 92 } 93 94 public int getTimeout() { 95 return timeout; 96 } 97 98 public void setTimeout(int timeout) { 99 this.timeout = timeout; 100 } 101 102 public int getMaxActive() { 103 return maxActive; 104 } 105 106 public void setMaxActive(int maxActive) { 107 this.maxActive = maxActive; 108 } 109 110 public int getMaxWait() { 111 return maxWait; 112 } 113 114 public void setMaxWait(int maxWait) { 115 this.maxWait = maxWait; 116 } 117 118 public int getMaxIdle() { 119 return maxIdle; 120 } 121 122 public void setMaxIdle(int maxIdle) { 123 this.maxIdle = maxIdle; 124 } 125 126 public int getMinIdle() { 127 return minIdle; 128 } 129 130 public void setMinIdle(int minIdle) { 131 this.minIdle = minIdle; 132 } 133 }
1 # redis 配置 2 redis: 3 # 地址 4 host: 127.0.0.1 5 # 端口,默认为6379 6 port: 6379 7 # 数据库索引 8 database: 0 9 # 密码 10 password: 123456 11 # 连接超时时间 12 timeout: 10000 13 pool: 14 # 连接池中的最小空闲连接 15 min-idle: 0 16 # 连接池中的最大空闲连接 17 max-idle: 8 18 # 连接池的最大数据库连接数 19 max-active: 200 20 # #连接池最大阻塞等待时间(使用负值表示没有限制) 21 max-wait: -1
ps: CustomException 这是个自定义异常 修改成自己额即可
1 2 3 4 import com.dog.common.constant.Constant; 5 import com.dog.common.exception.CustomException; 6 import lombok.extern.slf4j.Slf4j; 7 import org.springframework.beans.BeansException; 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.context.ApplicationContext; 10 import org.springframework.context.ApplicationContextAware; 11 import org.springframework.stereotype.Component; 12 import redis.clients.jedis.Jedis; 13 import redis.clients.jedis.JedisPool; 14 15 import java.util.Set; 16 17 /** 18 * redis工具类 19 * @author @Dog_Elder 20 */ 21 @Component 22 @Slf4j 23 public class JedisUtil implements ApplicationContextAware { 24 25 private static ApplicationContext applicationContext = null; 26 public JedisUtil() { 27 } 28 29 /** 30 * 获取redis实例 31 * @return 32 */ 33 public Jedis getJedis() { 34 Jedis jedis = null; 35 synchronized (Jedis.class) { 36 try { 37 jedis = getJedisPool().getResource(); 38 } finally { 39 if (jedis != null) { 40 getJedisPool().returnBrokenResource(jedis); 41 } 42 } 43 } 44 return jedis; 45 } 46 47 public JedisPool getJedisPool() { 48 JedisPool jedisPool = null; 49 if (jedisPool == null) { 50 synchronized (JedisPool.class) { 51 if (jedisPool == null) { 52 jedisPool = applicationContext.getBean(JedisPool.class); 53 } 54 } 55 } 56 return jedisPool; 57 } 58 59 @Override 60 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 61 if (JedisUtil.applicationContext == null) { 62 JedisUtil.applicationContext = applicationContext; //初始化 spring applicationContext 63 } 64 } 65 66 /** 67 * 根据key查看是否存在 68 * 69 * @param key 70 * @return 71 */ 72 public boolean hasKey(String key) { 73 Jedis jedis=null; 74 try{ 75 jedis = getJedis(); 76 return jedis.exists(key); 77 }catch (Exception e){ 78 log.info("JedisUtil.hasKey异常:"+e.getMessage()); 79 return false; 80 }finally { 81 disConnect(jedis); 82 } 83 } 84 85 /** 86 * 设置key -value 形式数据 87 * 88 * @param key 89 * @param value 90 * @return 91 */ 92 public String set(String key, String value) { 93 Jedis jedis=null; 94 try{ 95 jedis = getJedis(); 96 return jedis.set(key, value); 97 }catch (Exception e){ 98 log.info("JedisUtil.set(String key, String value)异常:"+e.getMessage()); 99 return null; 100 }finally { 101 disConnect(jedis); 102 } 103 104 } 105 106 /** 107 * 设置 一个过期时间 108 * 109 * @param key 110 * @param value 111 * @param timeOut 单位秒 112 * @return 113 */ 114 public String set(String key, String value, int timeOut) { 115 Jedis jedis=null; 116 try{ 117 jedis = getJedis(); 118 return jedis.setex(key, timeOut, value); 119 }catch (Exception e){ 120 log.info("JedisUtil.set(String key, String value, int timeOut)异常:"+e.getMessage()); 121 return null; 122 }finally { 123 disConnect(jedis); 124 } 125 } 126 127 /** 128 * 根据key获取value 129 * 130 * @param key 131 * @return 132 */ 133 public String getByKey(String key) { 134 Jedis jedis=null; 135 try{ 136 jedis = getJedis(); 137 return jedis.get(key); 138 }catch (Exception e){ 139 log.info("JedisUtil.getByKey(String key)异常:"+e.getMessage()); 140 return null; 141 }finally { 142 disConnect(jedis); 143 } 144 } 145 146 /** 147 * 根据通配符获取所有匹配的key 148 * @param pattern 149 * @return 150 */ 151 public Set<String> getKesByPattern(String pattern) { 152 Jedis jedis=null; 153 try{ 154 jedis = getJedis(); 155 return jedis.keys(pattern); 156 }catch (Exception e){ 157 log.info("JedisUtil.getKesByPattern(String pattern)异常:"+e.getMessage()); 158 return null; 159 }finally { 160 disConnect(jedis); 161 } 162 } 163 164 /** 165 * 根据key删除 166 * @param key 167 */ 168 public void delByKey(String key) { 169 Jedis jedis=null; 170 try{ 171 jedis = getJedis(); 172 if (jedis.exists(key)) { 173 jedis.del(key); 174 } 175 }catch (Exception e){ 176 log.info("JedisUtil.delByKey(String key)异常:"+e.getMessage()); 177 }finally { 178 disConnect(jedis); 179 } 180 } 181 182 /** 183 * 根据key获取过期时间 184 * 185 * @param key 186 * @return 187 */ 188 public long getTimeOutByKey(String key) { 189 Jedis jedis=null; 190 try{ 191 jedis = getJedis(); 192 return jedis.ttl(key); 193 }catch (Exception e){ 194 log.info("JedisUtil.getTimeOutByKey(String key)异常:"+e.getMessage()); 195 return 0; 196 }finally { 197 disConnect(jedis); 198 } 199 200 } 201 202 /** 203 * 清空数据 【慎用啊!】 204 */ 205 public void flushDB(){ 206 Jedis jedis=null; 207 try{ 208 jedis = getJedis(); 209 jedis.flushDB(); 210 }catch (Exception e){ 211 log.info("JedisUtil.flushDB() 异常:"+e.getMessage()); 212 }finally { 213 disConnect(jedis); 214 } 215 216 } 217 218 /** 219 * 刷新过期时间 220 * @param key 221 * @param timeOut 222 * @return 223 */ 224 public long refreshLiveTime(String key, int timeOut) { 225 Jedis jedis=null; 226 try{ 227 jedis = getJedis(); 228 return jedis.expire(key, timeOut); 229 }catch (Exception e){ 230 log.info("JedisUtil.hasKey异常:"+e.getMessage()); 231 return -1; 232 }finally { 233 disConnect(jedis); 234 } 235 } 236 237 /** 238 * 释放资源 239 */ 240 public void disConnect(Jedis jedis) { 241 if (jedis!=null){ 242 jedis.disconnect(); 243 // jedis.close(); 244 } 245 } 246 247 248 249 /** 250 * 静态注入JedisPool连接池 251 * 本来是正常注入JedisUtil,可以在Controller和Service层使用,但是重写Shiro的CustomCache无法注入JedisUtil 252 * 现在改为静态注入JedisPool连接池,JedisUtil直接调用静态方法即可 253 * https://blog.csdn.net/W_Z_W_888/article/details/79979103 254 */ 255 private static JedisPool jedisPool; 256 @Autowired 257 public void setJedisPool(JedisPool jedisPool) { 258 JedisUtil.jedisPool = jedisPool; 259 } 260 261 /** 262 * 获取Jedis实例 263 * @param 264 * @return redis.clients.jedis.Jedis 265 * @author dog_E 266 * @date 2018/9/4 15:47 267 */ 268 public static synchronized Jedis getResource() { 269 try { 270 if (jedisPool != null) { 271 return jedisPool.getResource(); 272 } else { 273 return null; 274 } 275 } catch (Exception e) { 276 throw new CustomException("获取Jedis资源异常:" + e.getMessage()); 277 } 278 } 279 280 /** 281 * 释放Jedis资源 282 * @param 283 * @return void 284 * @author dog_E 285 * @date 2018/9/5 9:16 286 */ 287 public static void closePool() { 288 try { 289 jedisPool.close(); 290 } catch (Exception e) { 291 throw new CustomException("释放Jedis资源异常:" + e.getMessage()); 292 } 293 } 294 295 /** 296 * 获取redis键值-object 297 * @param key 298 * @return java.lang.Object 299 * @author dog_E 300 * @date 2018/9/4 15:47 301 */ 302 public static Object getObject(String key) { 303 try (Jedis jedis = jedisPool.getResource()) { 304 String bytes = jedis.get(key); 305 if (StringUtils.isNotBlank(bytes)) { 306 return bytes; 307 } 308 } catch (Exception e) { 309 throw new CustomException("获取Redis键值getObject方法异常:key=" + key + " cause=" + e.getMessage()); 310 } 311 return null; 312 } 313 314 /** 315 * 设置redis键值-object 316 * @param key 317 * @param value 318 * @return java.lang.String 319 * @author dog_E 320 * @date 2018/9/4 15:49 321 */ 322 public static String setObject(String key, Object value) { 323 try (Jedis jedis = jedisPool.getResource()) { 324 return jedis.set(key.getBytes(), SerializableUtil.serializable(value)); 325 } catch (Exception e) { 326 throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage()); 327 } 328 } 329 330 /** 331 * 设置redis键值-object-expiretime 332 * @param key 333 * @param value 334 * @param expiretime 335 * @return java.lang.String 336 * @author dog_E 337 * @date 2018/9/4 15:50 338 */ 339 public static String setObject(String key, Object value, int expiretime) { 340 String result; 341 try (Jedis jedis = jedisPool.getResource()) { 342 result = jedis.set(key, String.valueOf(value)); 343 if (Constant.RedisPrefixConstant.OK.equals(result)) { 344 jedis.expire(key.getBytes(), expiretime); 345 } 346 return result; 347 } catch (Exception e) { 348 throw new CustomException("设置Redis键值setObject方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage()); 349 } 350 } 351 352 /** 353 * 获取redis键值-Json 354 * @param key 355 * @return java.lang.Object 356 * @author dog_E 357 * @date 2018/9/4 15:47 358 */ 359 public static String getJson(String key) { 360 try (Jedis jedis = jedisPool.getResource()) { 361 return jedis.get(key); 362 } catch (Exception e) { 363 throw new CustomException("获取Redis键值getJson方法异常:key=" + key + " cause=" + e.getMessage()); 364 } 365 } 366 367 /** 368 * 设置redis键值-Json 369 * @param key 370 * @param value 371 * @return java.lang.String 372 * @author dog_E 373 * @date 2018/9/4 15:49 374 */ 375 public static String setJson(String key, String value) { 376 try (Jedis jedis = jedisPool.getResource()) { 377 return jedis.set(key, value); 378 } catch (Exception e) { 379 throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage()); 380 } 381 } 382 383 /** 384 * 设置redis键值-Json-expiretime 385 * @param key 386 * @param value 387 * @param expiretime 388 * @return java.lang.String 389 * @author dog_E 390 * @date 2018/9/4 15:50 391 */ 392 public static String setJson(String key, String value, int expiretime) { 393 String result; 394 try (Jedis jedis = jedisPool.getResource()) { 395 result = jedis.set(key, value); 396 if (Constant.RedisPrefixConstant.OK.equals(result)) { 397 jedis.expire(key, expiretime); 398 } 399 return result; 400 } catch (Exception e) { 401 throw new CustomException("设置Redis键值setJson方法异常:key=" + key + " value=" + value + " cause=" + e.getMessage()); 402 } 403 } 404 405 /** 406 * 删除key 407 * @param key 408 * @return java.lang.Long 409 * @author dog_E 410 * @date 2018/9/4 15:50 411 */ 412 public static Long delKey(String key) { 413 try (Jedis jedis = jedisPool.getResource()) { 414 return jedis.del(key.getBytes()); 415 } catch (Exception e) { 416 throw new CustomException("删除Redis的键delKey方法异常:key=" + key + " cause=" + e.getMessage()); 417 } 418 } 419 420 /** 421 * 删除key 422 * @param key 423 * @return java.lang.Long 424 * @author dog_E 425 * @date 2018/9/4 15:50 426 */ 427 public static Boolean delKeyBoolean(String key) { 428 try (Jedis jedis = jedisPool.getResource()) { 429 return true; 430 } catch (Exception e) { 431 return false; 432 } 433 } 434 435 /** 436 * key是否存在 437 * @param key 438 * @return java.lang.Boolean 439 * @author dog_E 440 * @date 2018/9/4 15:51 441 */ 442 public static Boolean exists(String key) { 443 try (Jedis jedis = jedisPool.getResource()) { 444 return jedis.exists(key); 445 } catch (Exception e) { 446 throw new CustomException("查询Redis的键是否存在exists方法异常:key=" + key + " cause=" + e.getMessage()); 447 } 448 } 449 450 /** 451 * 模糊查询获取key集合(keys的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,生产不推荐使用) 452 * @param key 453 * @return java.util.Set<java.lang.String> 454 * @author dog_E 455 * @date 2018/9/6 9:43 456 */ 457 public static Set<String> keysS(String key) { 458 try (Jedis jedis = jedisPool.getResource()) { 459 return jedis.keys(key); 460 } catch (Exception e) { 461 throw new CustomException("模糊查询Redis的键集合keysS方法异常:key=" + key + " cause=" + e.getMessage()); 462 } 463 } 464 465 /** 466 * 模糊查询获取key集合(keys的速度非常快,但在一个大的数据库中使用它仍然可能造成性能问题,生产不推荐使用) 467 * @param key 468 * @return java.util.Set<java.lang.String> 469 * @author dog_E 470 * @date 2018/9/6 9:43 471 */ 472 public static Set<byte[]> keysB(String key) { 473 try (Jedis jedis = jedisPool.getResource()) { 474 return jedis.keys(key.getBytes()); 475 } catch (Exception e) { 476 throw new CustomException("模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage()); 477 } 478 } 479 480 481 /** 482 * redis做hash的添加 483 */ 484 public static boolean hset(String key, String field, String value){ 485 if(StringUtils.isBlank(key) || StringUtils.isBlank(field)){ 486 return false; 487 } 488 try (Jedis jedis = jedisPool.getResource()) { 489 //If the field already exists, and the HSET just produced an update of the value, 0 is returned, 490 //otherwise if a new field is created 1 is returned. 491 Long statusCode = jedis.hset(key, field, value); 492 if(statusCode > -1){ 493 return true; 494 } 495 } catch (Exception e) { 496 throw new CustomException("模糊查询Redis的键集合keysB方法异常:key=" + key + " cause=" + e.getMessage()); 497 } 498 return false; 499 } 500 501 /** 502 * @Description: 获取hsah中field的value 503 * @Author: @Dog_Elder 504 * @Date: 2020/11/18 14:09 505 * @param key: key 506 * @param field: map中key 507 * @return: map中value 508 **/ 509 public static String hget(String key, String field){ 510 try (Jedis jedis = jedisPool.getResource()) { 511 return jedis.hget(key, field); 512 } catch (Exception e) { 513 throw new CustomException("获取hsah中field的value方法异常:key=" + key + "field=" + field + " cause=" + e.getMessage()); 514 } 515 } 516 517 518 /** 519 * @Description: hash 值 增或加 520 * @Author: @Dog_Elder 521 * @Date: 2020/11/19 14:35 522 * @param key: key 523 * @param field: map中key 524 * @param value: 可以是正数 也可以是负数 525 * @return: java.lang.Long 526 **/ 527 public static Long hincrBy(String key, String field,long value){ 528 try (Jedis jedis = jedisPool.getResource()) { 529 return jedis.hincrBy(key,field,value); 530 } catch (Exception e) { 531 throw new CustomException("获取hsah中field的value方法异常:key=" + key + "field=" + field + " cause=" + e.getMessage()); 532 } 533 } 534 535 536 /** 537 * 获取过期剩余时间 538 * @param key 539 * @return java.lang.String 540 * @author dog_E 541 * @date 2018/9/11 16:26 542 */ 543 public static Long ttl(String key) { 544 Long result = -2L; 545 try (Jedis jedis = jedisPool.getResource()) { 546 result = jedis.ttl(key); 547 return result; 548 } catch (Exception e) { 549 throw new CustomException("获取Redis键过期剩余时间ttl方法异常:key=" + key + " cause=" + e.getMessage()); 550 } 551 } 552 553 554 555 }
1 import com.bdzl.common.redis.JedisUtil; 2 import com.bdzl.common.utils.param.R; 3 import com.bdzl.common.utils.string.StringUtils; 4 import com.bdzl.common.validation.SaveAction; 5 import com.bdzl.domain.vo.DraftBoxDTO; 6 import net.sf.json.util.JSONUtils; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Controller; 9 import org.springframework.validation.annotation.Validated; 10 import org.springframework.web.bind.annotation.GetMapping; 11 import org.springframework.web.bind.annotation.PostMapping; 12 import org.springframework.web.bind.annotation.RequestMapping; 13 import org.springframework.web.bind.annotation.ResponseBody; 14 15 /** 16 * @program: mse 17 * @description: 草稿箱 18 * @author: @Dog_Elder 19 * @create: 2021-03-24 17:26 20 **/ 21 @Controller 22 @RequestMapping("/draftBox") 23 public class DraftBoxController { 24 25 @Autowired 26 JedisUtil jedisUtil; 27 28 /** 29 * @param dto: 草稿箱对象通用 30 * @Description: 保存草稿箱 31 * @Author: @Dog_Elder 32 * @Date: 2021-3-23 17:52 33 * @return: com.bdzl.common.utils.param.R 34 **/ 35 @PostMapping 36 @ResponseBody 37 public R draftBox(@Validated(SaveAction.class) DraftBoxDTO dto) { 38 if (StringUtils.isNull(dto.getExpireDate())) { 39 JedisUtil.setJson(dto.getType() + dto.getId(), dto.getStr()); 40 } else { 41 JedisUtil.setJson(dto.getType() + dto.getId(), dto.getStr(), dto.getExpireDate()); 42 } 43 return R.ok(); 44 } 45 46 /** 47 * @param dto: 草稿箱对象通用 48 * @Description: 获取草稿箱内容 49 * @Author: @Dog_Elder 50 * @Date: 2021-3-23 17:52 51 * @return: com.bdzl.common.utils.param.R 52 **/ 53 @GetMapping 54 @ResponseBody 55 public R getDraftBox(DraftBoxDTO dto) { 56 String byKey = jedisUtil.getByKey(dto.getType() + dto.getId()); 57 String s = JSONUtils.convertToJavaIdentifier(byKey); 58 return R.ok(s); 59 } 60 61 /** 62 * @param dto: 草稿箱对象通用 63 * @Description: 删除草稿箱 64 * @Author: @Dog_Elder 65 * @Date: 2021-3-23 17:52 66 * @return: com.bdzl.common.utils.param.R 67 **/ 68 @PostMapping("/remove") 69 @ResponseBody 70 public R removeDraftBox(DraftBoxDTO dto) { 71 if (jedisUtil.hasKey(dto.getType() + dto.getId())) { 72 jedisUtil.delKey(dto.getType() + dto.getId()); 73 } 74 return R.ok(); 75 } 76 77 }
ps: 使用form表单主要是后台只是中转不用再处理
让前端直接传json串即可 取的也是json串 取出的转义让前端处理即可
原文:https://www.cnblogs.com/doge-elder/p/14605313.html