@ClassName RedisUtil @Description RedisUtil @Author luojiarui @Date 2022/8/14 17:14 @Version 1.0
| 18 | * @Version 1.0 |
| 19 | **/ |
| 20 | @Component |
| 21 | public final class RedisUtil { |
| 22 | |
| 23 | @Resource |
| 24 | private RedisTemplate<String, Object> redisTemplate; |
| 25 | |
| 26 | public Set<String> keys(String keys){ |
| 27 | try { |
| 28 | return redisTemplate.keys(keys); |
| 29 | }catch (Exception e){ |
| 30 | e.printStackTrace(); |
| 31 | return Sets.newHashSet(); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * 指定缓存失效时间 |
| 37 | * @param key 键 |
| 38 | * @param time 时间(秒) |
| 39 | * @return |
| 40 | */ |
| 41 | public boolean expire(String key, long time) { |
| 42 | try { |
| 43 | if (time > 0) { |
| 44 | redisTemplate.expire(key, time, TimeUnit.SECONDS); |
| 45 | } |
| 46 | return true; |
| 47 | } catch (Exception e) { |
| 48 | e.printStackTrace(); |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | /** |
| 53 | * 根据key 获取过期时间 |
| 54 | * @param key 键 不能为null |
| 55 | * @return 时间(秒) 返回0代表为永久有效 |
| 56 | */ |
| 57 | public long getExpire(String key) { |
| 58 | return redisTemplate.getExpire(key, TimeUnit.SECONDS); |
| 59 | } |
| 60 | /** |
| 61 | * 判断key是否存在 |
| 62 | * @param key 键 |
| 63 | * @return true 存在 false不存在 |
| 64 | */ |
| 65 | public boolean hasKey(String key) { |
| 66 | try { |
| 67 | return redisTemplate.hasKey(key); |
| 68 | } catch (Exception e) { |
| 69 | e.printStackTrace(); |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | /** |
| 74 | * 删除缓存 |
| 75 | * @param key 可以传一个值 或多个 |
| 76 | */ |
| 77 | @SuppressWarnings("unchecked") |
nothing calls this directly
no outgoing calls
no test coverage detected