From 2ff58b20eea0462eb23b0036ab27ffcac1e95258 Mon Sep 17 00:00:00 2001 From: 79493 <794930212@qq.com> Date: Tue, 18 Oct 2022 11:36:52 +0800 Subject: [PATCH] =?UTF-8?q?redis=E6=93=8D=E4=BD=9Capi=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{RedisPoolConfig.java => RedisConfig.java} | 2 +- .../main/java/com/rzyc/config/RedisUtil.java | 25 +- .../com/rzyc/controller/BaseController.java | 14 +- .../rzyc/controller/PersonalController.java | 13 +- .../com/rzyc/service/PcBusinessService.java | 8 +- inventory-gov/pom.xml | 11 + .../java/com/rzyc/config/RedisConfig.java | 45 ++ .../main/java/com/rzyc/config/RedisUtil.java | 606 ++++++++++++++++++ 8 files changed, 712 insertions(+), 12 deletions(-) rename inventory-ent/src/main/java/com/rzyc/config/{RedisPoolConfig.java => RedisConfig.java} (98%) create mode 100644 inventory-gov/src/main/java/com/rzyc/config/RedisConfig.java create mode 100644 inventory-gov/src/main/java/com/rzyc/config/RedisUtil.java diff --git a/inventory-ent/src/main/java/com/rzyc/config/RedisPoolConfig.java b/inventory-ent/src/main/java/com/rzyc/config/RedisConfig.java similarity index 98% rename from inventory-ent/src/main/java/com/rzyc/config/RedisPoolConfig.java rename to inventory-ent/src/main/java/com/rzyc/config/RedisConfig.java index 209cb0b..4dfc9ed 100644 --- a/inventory-ent/src/main/java/com/rzyc/config/RedisPoolConfig.java +++ b/inventory-ent/src/main/java/com/rzyc/config/RedisConfig.java @@ -22,7 +22,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; * */ @Configuration -public class RedisPoolConfig { +public class RedisConfig { /** diff --git a/inventory-ent/src/main/java/com/rzyc/config/RedisUtil.java b/inventory-ent/src/main/java/com/rzyc/config/RedisUtil.java index ef55f08..91bfdad 100644 --- a/inventory-ent/src/main/java/com/rzyc/config/RedisUtil.java +++ b/inventory-ent/src/main/java/com/rzyc/config/RedisUtil.java @@ -11,12 +11,23 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; +/** + * Redis操作api工具 + * @author Xuwanxin + * @date 2022/10/17 + * */ + @Component public class RedisUtil { - @Autowired + RedisTemplate redisTemplate; + @Autowired + public RedisUtil(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + /** * get 多参数分隔 : * @param key 键 @@ -132,6 +143,8 @@ public class RedisUtil { */ public boolean set(String key, Object value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); @@ -216,6 +229,8 @@ public class RedisUtil { * @return true成功 false失败 */ public boolean hmset(String key, Map map, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; try { redisTemplate.opsForHash().putAll(key, map); if (time > 0) { @@ -257,6 +272,8 @@ public class RedisUtil { * @return true 成功 false失败 */ public boolean hset(String key, String item, Object value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; try { redisTemplate.opsForHash().put(key, item, value); if (time > 0) { @@ -376,6 +393,8 @@ public class RedisUtil { * @return 成功个数 */ public long sSetAndTime(String key, long time, Object... values) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) { @@ -496,6 +515,8 @@ public class RedisUtil { * @param time 时间(秒) */ public boolean lSet(String key, Object value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; try { redisTemplate.opsForList().rightPush(key, value); if (time > 0) { @@ -538,6 +559,8 @@ public class RedisUtil { * @return */ public boolean lSet(String key, List value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) { diff --git a/inventory-ent/src/main/java/com/rzyc/controller/BaseController.java b/inventory-ent/src/main/java/com/rzyc/controller/BaseController.java index 196c1ad..c50fbf1 100644 --- a/inventory-ent/src/main/java/com/rzyc/controller/BaseController.java +++ b/inventory-ent/src/main/java/com/rzyc/controller/BaseController.java @@ -448,8 +448,6 @@ public class BaseController { @Autowired protected EntCertificatesMapper entCertificatesMapper; - - //企业下企业用户 @Autowired protected EntUserMapper entUserMapper; @@ -466,12 +464,24 @@ public class BaseController { @Autowired protected EntPostTaskMapper entPostTaskMapper; + //企业岗位职责 @Autowired protected EntPostDutyMapper entPostDutyMapper; + //企业用户证件 @Autowired protected EntUserCredentialMapper entUserCredentialMapper; + //企业设备 + @Autowired + protected EntDeviceMapper entDeviceMapper; + + //企业设备类型 + @Autowired + protected EntDeviceTypeMapper entDeviceTypeMapper; + + + diff --git a/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java b/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java index dee9108..43e4cdd 100644 --- a/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java +++ b/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java @@ -23,17 +23,13 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import javax.validation.constraints.NotNull; -import java.time.LocalTime; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Objects; @@ -52,15 +48,18 @@ import java.util.Objects; @Validated public class PersonalController extends BaseController{ - @Autowired UserLoginService userLoginService; - @Autowired PcBusinessService pcBusinessService; - @Autowired RedisUtil redisUtil; + @Autowired + public PersonalController(UserLoginService userLoginService, PcBusinessService pcBusinessService, RedisUtil redisUtil) { + this.userLoginService = userLoginService; + this.pcBusinessService = pcBusinessService; + this.redisUtil = redisUtil; + } /** * 用户登录 diff --git a/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java b/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java index 8e99e10..f0b3029 100644 --- a/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java +++ b/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java @@ -86,7 +86,7 @@ public class PcBusinessService extends BaseController { if (null != enterpriseId && postId == null){ redisUtil.set(enterpriseId,posts); }else if (null != enterpriseId && postId != enterpriseId){ - redisUtil.set(redisUtil.appendSymbol(enterpriseId,postId),posts); + redisUtil.set(redisUtil.appendSymbol(enterpriseId,postId),posts,0); } return singleResult; } @@ -263,6 +263,12 @@ public class PcBusinessService extends BaseController { return singleResult; } + public SingleResult entEquipmentTypeList(String enterpriseId){ + SingleResult singleResult = new SingleResult(); + entDeviceTypeMapper.selectEntEquipmentTypeList(enterpriseId); + return singleResult; + } + diff --git a/inventory-gov/pom.xml b/inventory-gov/pom.xml index 43591b2..27ddf1e 100644 --- a/inventory-gov/pom.xml +++ b/inventory-gov/pom.xml @@ -217,6 +217,17 @@ + + + org.springframework.boot + spring-boot-starter-data-redis + + + org.apache.commons + commons-pool2 + + + diff --git a/inventory-gov/src/main/java/com/rzyc/config/RedisConfig.java b/inventory-gov/src/main/java/com/rzyc/config/RedisConfig.java new file mode 100644 index 0000000..754ea74 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/config/RedisConfig.java @@ -0,0 +1,45 @@ +package com.rzyc.config; + + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * Redis 连接及序列化 + * @author Xuwanxin + * @date 2022/10/13 + * */ + +@Configuration +public class RedisConfig { + + + /** + * 在使用localTime对象时,GenericJackson2JsonRedisSerializer反序列化会报转换错误,遇到之后还需要再单独处理 + * */ + @Bean + public RedisTemplate strRedisTemplate(RedisConnectionFactory redisConnectionFactory){ + RedisTemplate redisTemplate = new RedisTemplate<>(); + // key 序列化方式 + redisTemplate.setKeySerializer(new StringRedisSerializer()); + // value 序列化 + redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + // hash 类型 key序列化 + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + // hash 类型 value序列化方式 + redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); + // 注入连接工厂 + redisTemplate.setConnectionFactory(redisConnectionFactory); + // 让设置生效 + redisTemplate.afterPropertiesSet(); + return redisTemplate; + } + + + + +} diff --git a/inventory-gov/src/main/java/com/rzyc/config/RedisUtil.java b/inventory-gov/src/main/java/com/rzyc/config/RedisUtil.java new file mode 100644 index 0000000..31d7fb6 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/config/RedisUtil.java @@ -0,0 +1,606 @@ +package com.rzyc.config; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Component +public class RedisUtil { + + @Autowired + RedisTemplate redisTemplate; + + /** + * get 多参数分隔 : + * @param key 键 + */ + public String appendSymbol(String ... key) { + StringBuilder stringBuilder = new StringBuilder(); + try { + for (String k:key) { + stringBuilder.append(k); + stringBuilder.append(":"); + } + return stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(":")).toString(); + } catch (Exception e) { + e.printStackTrace(); + return "error"; + } + } + + + /** + * 指定缓存失效时间 + * @param key 键 + * @param time 时间(秒) + */ + public boolean expire(String key, long time) { + try { + if (time > 0) { + redisTemplate.expire(key, time, TimeUnit.SECONDS); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 根据key 获取过期时间 + * @param key 键 不能为null + * @return 时间(秒) 返回0代表为永久有效 + */ + public long getExpire(String key) { + return redisTemplate.getExpire(key, TimeUnit.SECONDS); + } + + + /** + * 判断key是否存在 + * @param key 键 + * @return true 存在 false不存在 + */ + public boolean hasKey(String key) { + try { + return redisTemplate.hasKey(key); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 删除缓存 + * @param key 可以传一个值 或多个 + */ + @SuppressWarnings("unchecked") + public void del(String... key) { + if (key != null && key.length > 0) { + if (key.length == 1) { + redisTemplate.delete(key[0]); + } else { + redisTemplate.delete(String.valueOf(CollectionUtils.arrayToList(key))); + } + } + } + + + // ============================String============================= + + /** + * 普通缓存获取 + * @param key 键 + * @return 值 + */ + public Object get(String key) { + return key == null ? null : redisTemplate.opsForValue().get(key); + } + + /** + * 普通缓存放入 + * @param key 键 + * @param value 值 + * @return true成功 false失败 + */ + + public boolean set(String key, Object value) { + try { + redisTemplate.opsForValue().set(key, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 普通缓存放入并设置时间 + * @param key 键 + * @param value 值 + * @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 + * @return true成功 false 失败 + */ + + public boolean set(String key, Object value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; + try { + if (time > 0) { + redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); + } else { + set(key, value); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 递增 + * @param key 键 + * @param delta 要增加几(大于0) + */ + public long incr(String key, long delta) { + if (delta < 0) { + throw new RuntimeException("递增因子必须大于0"); + } + return redisTemplate.opsForValue().increment(key, delta); + } + + + /** + * 递减 + * @param key 键 + * @param delta 要减少几(小于0) + */ + public long decr(String key, long delta) { + if (delta < 0) { + throw new RuntimeException("递减因子必须大于0"); + } + return redisTemplate.opsForValue().increment(key, -delta); + } + + + // ================================Map================================= + + /** + * HashGet + * @param key 键 不能为null + * @param item 项 不能为null + */ + public Object hget(String key, String item) { + return redisTemplate.opsForHash().get(key, item); + } + + /** + * 获取hashKey对应的所有键值 + * @param key 键 + * @return 对应的多个键值 + */ + public Map hmget(String key) { + return redisTemplate.opsForHash().entries(key); + } + + /** + * HashSet + * @param key 键 + * @param map 对应多个键值 + */ + public boolean hmset(String key, Map map) { + try { + redisTemplate.opsForHash().putAll(key, map); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * HashSet 并设置时间 + * @param key 键 + * @param map 对应多个键值 + * @param time 时间(秒) + * @return true成功 false失败 + */ + public boolean hmset(String key, Map map, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; + try { + redisTemplate.opsForHash().putAll(key, map); + if (time > 0) { + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 向一张hash表中放入数据,如果不存在将创建 + * + * @param key 键 + * @param item 项 + * @param value 值 + * @return true 成功 false失败 + */ + public boolean hset(String key, String item, Object value) { + try { + redisTemplate.opsForHash().put(key, item, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + /** + * 向一张hash表中放入数据,如果不存在将创建 + * + * @param key 键 + * @param item 项 + * @param value 值 + * @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 + * @return true 成功 false失败 + */ + public boolean hset(String key, String item, Object value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; + try { + redisTemplate.opsForHash().put(key, item, value); + if (time > 0) { + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 删除hash表中的值 + * + * @param key 键 不能为null + * @param item 项 可以使多个 不能为null + */ + public void hdel(String key, Object... item) { + redisTemplate.opsForHash().delete(key, item); + } + + + /** + * 判断hash表中是否有该项的值 + * + * @param key 键 不能为null + * @param item 项 不能为null + * @return true 存在 false不存在 + */ + public boolean hHasKey(String key, String item) { + return redisTemplate.opsForHash().hasKey(key, item); + } + + + /** + * hash递增 如果不存在,就会创建一个 并把新增后的值返回 + * + * @param key 键 + * @param item 项 + * @param by 要增加几(大于0) + */ + public double hincr(String key, String item, double by) { + return redisTemplate.opsForHash().increment(key, item, by); + } + + + /** + * hash递减 + * + * @param key 键 + * @param item 项 + * @param by 要减少记(小于0) + */ + public double hdecr(String key, String item, double by) { + return redisTemplate.opsForHash().increment(key, item, -by); + } + + + // ============================set============================= + + /** + * 根据key获取Set中的所有值 + * @param key 键 + */ + public Set sGet(String key) { + try { + return redisTemplate.opsForSet().members(key); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + /** + * 根据value从一个set中查询,是否存在 + * + * @param key 键 + * @param value 值 + * @return true 存在 false不存在 + */ + public boolean sHasKey(String key, Object value) { + try { + return redisTemplate.opsForSet().isMember(key, value); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 将数据放入set缓存 + * + * @param key 键 + * @param values 值 可以是多个 + * @return 成功个数 + */ + public long sSet(String key, Object... values) { + try { + return redisTemplate.opsForSet().add(key, values); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + + /** + * 将set数据放入缓存 + * + * @param key 键 + * @param time 时间(秒) + * @param values 值 可以是多个 + * @return 成功个数 + */ + public long sSetAndTime(String key, long time, Object... values) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; + try { + Long count = redisTemplate.opsForSet().add(key, values); + if (time > 0) { + expire(key, time); + } + return count; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + + /** + * 获取set缓存的长度 + * + * @param key 键 + */ + public long sGetSetSize(String key) { + try { + return redisTemplate.opsForSet().size(key); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + + /** + * 移除值为value的 + * + * @param key 键 + * @param values 值 可以是多个 + * @return 移除的个数 + */ + + public long setRemove(String key, Object... values) { + try { + Long count = redisTemplate.opsForSet().remove(key, values); + return count; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + // ===============================list================================= + + /** + * 获取list缓存的内容 + * + * @param key 键 + * @param start 开始 + * @param end 结束 0 到 -1代表所有值 + */ + public List lGet(String key, long start, long end) { + try { + return redisTemplate.opsForList().range(key, start, end); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + /** + * 获取list缓存的长度 + * + * @param key 键 + */ + public long lGetListSize(String key) { + try { + return redisTemplate.opsForList().size(key); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + + /** + * 通过索引 获取list中的值 + * + * @param key 键 + * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 + */ + public Object lGetIndex(String key, long index) { + try { + return redisTemplate.opsForList().index(key, index); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + + /** + * 将list放入缓存 + * + * @param key 键 + * @param value 值 + */ + public boolean lSet(String key, Object value) { + try { + redisTemplate.opsForList().rightPush(key, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 将list放入缓存 + * @param key 键 + * @param value 值 + * @param time 时间(秒) + */ + public boolean lSet(String key, Object value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; + try { + redisTemplate.opsForList().rightPush(key, value); + if (time > 0) { + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + + /** + * 将list放入缓存 + * + * @param key 键 + * @param value 值 + * @return + */ + public boolean lSet(String key, List value) { + try { + redisTemplate.opsForList().rightPushAll(key, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + + } + + + /** + * 将list放入缓存 + * + * @param key 键 + * @param value 值 + * @param time 时间(秒) + * @return + */ + public boolean lSet(String key, List value, long time) { + //默认8小时ttl时间 + time = time > 0 ? time : 28800; + try { + redisTemplate.opsForList().rightPushAll(key, value); + if (time > 0) { + expire(key, time); + } + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 根据索引修改list中的某条数据 + * + * @param key 键 + * @param index 索引 + * @param value 值 + * @return + */ + + public boolean lUpdateIndex(String key, long index, Object value) { + try { + redisTemplate.opsForList().set(key, index, value); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + + + /** + * 移除N个值为value + * + * @param key 键 + * @param count 移除多少个 + * @param value 值 + * @return 移除的个数 + */ + + public long lRemove(String key, long count, Object value) { + try { + Long remove = redisTemplate.opsForList().remove(key, count, value); + return remove; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + + } + +}