use of org.springframework.data.redis.connection.RedisConnection in project redisson by redisson.
the class RedissonSubscribeTest method testPatterTopic.
@Test
public void testPatterTopic() throws IOException, InterruptedException {
RedisRunner.RedisProcess instance = new RedisRunner().nosave().randomPort().randomDir().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.K, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g, RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.$).run();
Config config = new Config();
config.useSingleServer().setAddress(instance.getRedisServerAddressAndPort()).setPingConnectionInterval(0);
RedissonClient redisson = Redisson.create(config);
RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory);
AtomicInteger counterTest = new AtomicInteger();
container.addMessageListener(new MessageListener() {
@Override
public void onMessage(Message message, byte[] pattern) {
counterTest.incrementAndGet();
}
}, new PatternTopic("__keyspace@0__:mykey"));
container.addMessageListener(new MessageListener() {
@Override
public void onMessage(Message message, byte[] pattern) {
counterTest.incrementAndGet();
}
}, new PatternTopic("__keyevent@0__:del"));
container.afterPropertiesSet();
container.start();
Assertions.assertThat(container.isRunning()).isTrue();
RedisConnection c = factory.getConnection();
c.set("mykey".getBytes(), "2".getBytes());
c.del("mykey".getBytes());
Awaitility.await().atMost(Duration.FIVE_SECONDS).until(() -> {
return counterTest.get() == 3;
});
container.stop();
redisson.shutdown();
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_REMOVE_ALL.
@Override
protected CacheResult do_REMOVE_ALL(Set<? extends K> keys) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
byte[][] newKeys = keys.stream().map((k) -> buildKey(k)).toArray((len) -> new byte[keys.size()][]);
Long result = con.del(newKeys);
if (result != null) {
return CacheResult.SUCCESS_WITHOUT_MSG;
} else {
return new CacheResult(CacheResultCode.FAIL, "result:" + result);
}
} catch (Exception ex) {
logError("REMOVE_ALL", "keys(" + keys.size() + ")", ex);
return new CacheResult(ex);
} finally {
closeConnection(con);
}
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_PUT.
@Override
protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite));
byte[] keyBytes = buildKey(key);
byte[] valueBytes = valueEncoder.apply(holder);
Boolean result = con.pSetEx(keyBytes, timeUnit.toMillis(expireAfterWrite), valueBytes);
if (Boolean.TRUE.equals(result)) {
return CacheResult.SUCCESS_WITHOUT_MSG;
} else {
return new CacheResult(CacheResultCode.FAIL, "result:" + result);
}
} catch (Exception ex) {
logError("PUT", key, ex);
return new CacheResult(ex);
} finally {
closeConnection(con);
}
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_PUT_IF_ABSENT.
@Override
protected CacheResult do_PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite));
byte[] newKey = buildKey(key);
Boolean result = con.set(newKey, valueEncoder.apply(holder), Expiration.from(expireAfterWrite, timeUnit), RedisStringCommands.SetOption.ifAbsent());
if (Boolean.TRUE.equals(result)) {
return CacheResult.SUCCESS_WITHOUT_MSG;
} else /* else if (result == null) {
return CacheResult.EXISTS_WITHOUT_MSG;
} */
{
return CacheResult.EXISTS_WITHOUT_MSG;
}
} catch (Exception ex) {
logError("PUT_IF_ABSENT", key, ex);
return new CacheResult(ex);
} finally {
closeConnection(con);
}
}
use of org.springframework.data.redis.connection.RedisConnection in project redisson by redisson.
the class RedissonSubscribeTest method testMultipleSubscribers.
@Test
public void testMultipleSubscribers() {
RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory);
AtomicInteger counterTest = new AtomicInteger();
AtomicInteger counterTest2 = new AtomicInteger();
container.addMessageListener(new MessageListener() {
@Override
public void onMessage(Message message, byte[] pattern) {
counterTest.incrementAndGet();
}
}, new ChannelTopic("test"));
container.addMessageListener(new MessageListener() {
@Override
public void onMessage(Message message, byte[] pattern) {
counterTest.incrementAndGet();
}
}, new ChannelTopic("test"));
container.addMessageListener(new MessageListener() {
@Override
public void onMessage(Message message, byte[] pattern) {
counterTest2.incrementAndGet();
}
}, new ChannelTopic("test2"));
container.afterPropertiesSet();
container.start();
Assertions.assertThat(container.isRunning()).isTrue();
RedisConnection c = factory.getConnection();
c.publish("test".getBytes(), "sdfdsf".getBytes());
Awaitility.await().atMost(Duration.FIVE_SECONDS).until(() -> {
return counterTest.get() == 2;
});
Assertions.assertThat(counterTest2.get()).isZero();
container.stop();
}
Aggregations