@Override public void removeCommitTxGroup() { final Set<String> keys = redisTemplate.keys(Constant.REDIS_KEYS); keys.parallelStream().forEach(key -> { final Map<Object, TxTransactionItem> entries = redisTemplate.opsForHash().entries(key); final Collection<TxTransactionItem> values = entries.values(); final boolean present = values.stream() .anyMatch(item -> item.getStatus() != TransactionStatusEnum.COMMIT.getCode()); if (!present) { redisTemplate.delete(key); } }); }
@Override public void removeRollBackTxGroup() { final Set<String> keys = redisTemplate.keys(Constant.REDIS_KEYS); keys.parallelStream().forEach(key -> { final Map<Object, TxTransactionItem> entries = redisTemplate.opsForHash().entries(key); final Collection<TxTransactionItem> values = entries.values(); final Optional<TxTransactionItem> any = values.stream().filter(item -> item.getRole() == TransactionRoleEnum.START.getCode() && item.getStatus() == TransactionStatusEnum.ROLLBACK.getCode()) .findAny(); if (any.isPresent()) { redisTemplate.delete(key); } }); }
rangeKeys = Sets.newHashSet(txTransactionQuery.getTxGroupId()); } else { keys = redisTemplate.keys(CommonConstant.REDIS_KEYS); rangeKeys = redisTemplate.opsForZSet() .range(CommonConstant.REDIS_KEY_SET, start, end - 1);
@Override public Set<String> keys(final String pattern) { return redisTemplate.keys(pattern); }
@Override protected Collection<?> doListKeys(String keyPattern) { Assert.hasText(keyPattern, "'keyPattern' must not be empty"); return this.redisTemplate.keys(keyPattern); }
@ManagedAttribute public int getMessageGroupCount() { Set<Object> keys = this.redisTemplate.keys(this.beanName + ":*"); return keys == null ? 0 : keys.size(); }
/** * 查询在以keyPatten的所有 key * * @param keyPatten the key patten * @return the set */ public Set<String> keys(final String keyPatten) { return redisTemplate.execute((RedisCallback<Set<String>>) connection -> redisTemplate.keys(keyPatten + "*")); }
private List<String> sortedKeys(String groupId) { Set<Object> keys = this.getRedisTemplate().keys(groupId == null ? (this.getBeanName() + ":*") : (groupId + "*")); List<String> list = new LinkedList<>(); if (keys != null) { for (Object key : keys) { Assert.isInstanceOf(String.class, key); list.add((String) key); } Collections.sort(list, this.keysComparator); } return list; }
private Set<Object> narrowedKeys() { Set<Object> keys = this.getRedisTemplate().keys(this.getBeanName() + ":*"); Set<Object> narrowedKeys = new HashSet<>(); if (keys != null) { for (Object key : keys) { Assert.isInstanceOf(String.class, key); String keyString = (String) key; int lastIndexOfColon = keyString.lastIndexOf(":"); if (keyString.indexOf(":") != lastIndexOfColon) { narrowedKeys.add(keyString.substring(0, lastIndexOfColon)); } else { narrowedKeys.add(key); } } } return narrowedKeys; }
@ManagedAttribute public int getMessageCountForAllMessageGroups() { Set<?> keys = this.redisTemplate.keys(this.beanName + ":*"); if (keys == null) { return 0; } int count = 0; for (Object key : keys) { count += this.messageGroupSize(key); } return count; }
/** * 查询在这个时间段内即将过期的key * * @param key the key * @param time the time * @return the list */ public List<String> willExpire(final String key, final long time) { final List<String> keysList = new ArrayList<>(); redisTemplate.execute((RedisCallback<List<String>>) connection -> { Set<String> keys = redisTemplate.keys(key + "*"); for (String key1 : keys) { Long ttl = connection.ttl(key1.getBytes(DEFAULT_CHARSET)); if (0 <= ttl && ttl <= 2 * time) { keysList.add(key1); } } return keysList; }); return keysList; }
/** * 根据key获取对象 * * @param keyPatten the key patten * @return the keys values */ public Map<String, String> getKeysValues(final String keyPatten) { LOGGER.info("[redisTemplate redis] getValues() patten={} ", keyPatten); return redisTemplate.execute((RedisCallback<Map<String, String>>) connection -> { RedisSerializer<String> serializer = getRedisSerializer(); Map<String, String> maps = new HashMap<>(); Set<String> keys = redisTemplate.keys(keyPatten + "*"); for (String key : keys) { byte[] bKeys = serializer.serialize(key); byte[] bValues = connection.get(bKeys); String value = serializer.deserialize(bValues); maps.put(key, value); } return maps; }); }
@Test @RedisAvailable public void testLockSingleGroup() throws Exception { this.releaseStrategy.reset(1); Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1)); Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 1)); assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS)); assertEquals(1, this.template.keys("aggregatorWithRedisLocksTests:*").size()); this.releaseStrategy.latch1.countDown(); assertNotNull(this.out.receive(10000)); assertEquals(1, this.releaseStrategy.maxCallers.get()); this.assertNoLocksAfterTest(); assertNull("Unexpected exception:" + (this.exception != null ? this.exception.toString() : ""), this.exception); }
@Test @RedisAvailable public void testLockThreeGroups() throws Exception { this.releaseStrategy.reset(3); Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1)); Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 1)); Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 2)); Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 2)); Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 3)); Executors.newSingleThreadExecutor().execute(asyncSend("bar", 2, 3)); assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS)); assertEquals(3, this.template.keys("aggregatorWithRedisLocksTests:*").size()); this.releaseStrategy.latch1.countDown(); this.releaseStrategy.latch1.countDown(); this.releaseStrategy.latch1.countDown(); assertNotNull(this.out.receive(10000)); assertNotNull(this.out.receive(10000)); assertNotNull(this.out.receive(10000)); assertEquals(3, this.releaseStrategy.maxCallers.get()); this.assertNoLocksAfterTest(); assertNull("Unexpected exception:" + (this.exception != null ? this.exception.toString() : ""), this.exception); }
@Before @After public void setup() { this.template = this.createTemplate(); Set<String> keys = template.keys("aggregatorWithRedisLocksTests:*"); for (String key : keys) { template.delete(key); } }
@Test @RedisAvailable @Repeat(10) public void testDistributedAggregator() throws Exception { this.releaseStrategy.reset(1); Executors.newSingleThreadExecutor().execute(asyncSend("foo", 1, 1)); Executors.newSingleThreadExecutor().execute(() -> { try { in2.send(new GenericMessage<String>("bar", stubHeaders(2, 2, 1))); } catch (Exception e) { exception = e; } }); assertTrue(this.releaseStrategy.latch2.await(10, TimeUnit.SECONDS)); assertEquals(1, this.template.keys("aggregatorWithRedisLocksTests:*").size()); this.releaseStrategy.latch1.countDown(); assertNotNull(this.out.receive(10000)); assertEquals(1, this.releaseStrategy.maxCallers.get()); this.assertNoLocksAfterTest(); assertNull("Unexpected exception:" + (this.exception != null ? this.exception.toString() : ""), this.exception); }
@Override public void clear() { logger.debug("清空缓存"); try { Set<String> keys = redisTemplate.keys("*:" + this.id + "*"); if (!CollectionUtils.isEmpty(keys)) { redisTemplate.delete(keys); } } catch (Exception e) { } }
/** * 批量删除key * @param pattern */ public void removePattern(final String pattern) { Set<Serializable> keys = redisTemplate.keys(pattern); if (keys.size() > 0) { redisTemplate.delete(keys); } }
private void clear(List<String> compositeWildcardKeys) { for (String compositeWildcardKey : compositeWildcardKeys) { Set<String> keys = redisTemplate.keys(compositeWildcardKey); for (String k : keys) { redisTemplate.delete(k); } } } }