Search in sources :

Example 11 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project springBoot-learn-demo by nbfujx.

the class RedisLock method setNX.

private boolean setNX(final String key, final String value) {
    Object obj = null;
    try {
        obj = redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisSerializer serializer = new StringRedisSerializer();
                Boolean success = connection.setNX(serializer.serialize(key), serializer.serialize(value));
                connection.close();
                return success;
            }
        });
    } catch (Exception e) {
        logger.error("setNX redis error, key : {}", key);
    }
    return obj != null ? (Boolean) obj : false;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisCallback(org.springframework.data.redis.core.RedisCallback) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 12 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project Spring-Family by Sierou-Java.

the class RedisLock method setNX.

private boolean setNX(final String key, final String value) {
    Object obj = null;
    try {
        obj = redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisSerializer serializer = new StringRedisSerializer();
                Boolean success = connection.setNX(serializer.serialize(key), serializer.serialize(value));
                connection.close();
                return success;
            }
        });
    } catch (Exception e) {
        log.error("setNX redis error, key : {}", key);
    }
    return obj != null ? (Boolean) obj : false;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisCallback(org.springframework.data.redis.core.RedisCallback) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 13 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project Spring-Family by Sierou-Java.

the class RedisLock method get.

private String get(final String key) {
    Object obj = null;
    try {
        obj = redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisSerializer serializer = new StringRedisSerializer();
                byte[] data = connection.get(serializer.serialize(key));
                connection.close();
                if (data == null) {
                    return null;
                }
                return serializer.deserialize(data);
            }
        });
    } catch (Exception e) {
        log.error("get redis error, key : {}", key);
    }
    return obj != null ? obj.toString() : null;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisCallback(org.springframework.data.redis.core.RedisCallback) DataAccessException(org.springframework.dao.DataAccessException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 14 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project spring-session by spring-projects.

the class AbstractHttpSessionListenerTests method createMockRedisConnection.

static RedisConnectionFactory createMockRedisConnection() {
    RedisConnectionFactory factory = mock(RedisConnectionFactory.class);
    RedisConnection connection = mock(RedisConnection.class);
    given(factory.getConnection()).willReturn(connection);
    given(connection.getConfig(anyString())).willReturn(new Properties());
    return factory;
}
Also used : Properties(java.util.Properties) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 15 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection in project spring-session by spring-projects.

the class RedisHttpSessionConfigurationXmlTests method connectionFactory.

static RedisConnectionFactory connectionFactory() {
    RedisConnectionFactory factory = mock(RedisConnectionFactory.class);
    RedisConnection connection = mock(RedisConnection.class);
    given(factory.getConnection()).willReturn(connection);
    given(connection.getConfig(anyString())).willReturn(new Properties());
    return factory;
}
Also used : Properties(java.util.Properties) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

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