Search in sources :

Example 16 with RedisConnection

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();
}
Also used : Message(org.springframework.data.redis.connection.Message) PatternTopic(org.springframework.data.redis.listener.PatternTopic) Config(org.redisson.config.Config) MessageListener(org.springframework.data.redis.connection.MessageListener) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedisRunner(org.redisson.RedisRunner) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Test(org.junit.Test)

Example 17 with RedisConnection

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);
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) java.util(java.util) com.alicp.jetcache(com.alicp.jetcache) Logger(org.slf4j.Logger) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Expiration(org.springframework.data.redis.core.types.Expiration) LoggerFactory(org.slf4j.LoggerFactory) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisStringCommands(org.springframework.data.redis.connection.RedisStringCommands) Function(java.util.function.Function) AbstractExternalCache(com.alicp.jetcache.external.AbstractExternalCache) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 18 with RedisConnection

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);
    }
}
Also used : RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 19 with RedisConnection

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);
    }
}
Also used : RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 20 with RedisConnection

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();
}
Also used : Message(org.springframework.data.redis.connection.Message) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(org.springframework.data.redis.connection.MessageListener) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Test(org.junit.Test)

Aggregations

RedisConnection (org.springframework.data.redis.connection.RedisConnection)41 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)9 RedisConnectionFailureException (org.springframework.data.redis.RedisConnectionFailureException)8 Properties (java.util.Properties)7 DataAccessException (org.springframework.dao.DataAccessException)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 Test (org.junit.Test)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)3 com.alicp.jetcache (com.alicp.jetcache)2 AbstractExternalCache (com.alicp.jetcache.external.AbstractExternalCache)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Logger (org.slf4j.Logger)2