| | |
| | | import lombok.AccessLevel; |
| | | import lombok.NoArgsConstructor; |
| | | import org.redisson.api.*; |
| | | import org.redisson.config.Config; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.Collection; |
| | |
| | | public class RedisUtils { |
| | | |
| | | private static final RedissonClient CLIENT = SpringUtils.getBean(RedissonClient.class); |
| | | |
| | | public static NameMapper getNameMapper() { |
| | | Config config = CLIENT.getConfig(); |
| | | if (config.isClusterConfig()) { |
| | | return config.useClusterServers().getNameMapper(); |
| | | } |
| | | return config.useSingleServer().getNameMapper(); |
| | | } |
| | | |
| | | /** |
| | | * 限流 |
| | |
| | | * @return 对象列表 |
| | | */ |
| | | public static Collection<String> keys(final String pattern) { |
| | | Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(getNameMapper().map(pattern)); |
| | | return stream.map(key -> getNameMapper().unmap(key)).collect(Collectors.toList()); |
| | | Stream<String> stream = CLIENT.getKeys().getKeysStreamByPattern(pattern); |
| | | return stream.collect(Collectors.toList()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param pattern 字符串前缀 |
| | | */ |
| | | public static void deleteKeys(final String pattern) { |
| | | CLIENT.getKeys().deleteByPattern(getNameMapper().map(pattern)); |
| | | CLIENT.getKeys().deleteByPattern(pattern); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public static Boolean hasKey(String key) { |
| | | RKeys rKeys = CLIENT.getKeys(); |
| | | return rKeys.countExists(getNameMapper().map(key)) > 0; |
| | | return rKeys.countExists(key) > 0; |
| | | } |
| | | } |