Search in sources :

Example 11 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project springBoot-learn-demo by nbfujx.

the class RedisConfig method redisTemplate.

@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new RedisObjectSerializer());
    return template;
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) Bean(org.springframework.context.annotation.Bean)

Example 12 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer in project springBoot-learn-demo by nbfujx.

the class RedisLock method getSet.

private String getSet(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();
                byte[] ret = connection.getSet(serializer.serialize(key), serializer.serialize(value));
                connection.close();
                return serializer.deserialize(ret);
            }
        });
    } catch (Exception e) {
        logger.error("setNX redis error, key : {}", key);
    }
    return obj != null ? (String) obj : 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 13 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer 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 14 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer 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 15 with StringRedisSerializer

use of org.springframework.data.redis.serializer.StringRedisSerializer 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)

Aggregations

StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)46 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)30 Bean (org.springframework.context.annotation.Bean)17 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)16 Test (org.junit.Test)14 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)14 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)13 DataAccessException (org.springframework.dao.DataAccessException)7 RedisConnection (org.springframework.data.redis.connection.RedisConnection)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7 BeanFactory (org.springframework.beans.factory.BeanFactory)5 Date (java.util.Date)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Jackson2JsonRedisSerializer (org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer)3 Message (org.springframework.messaging.Message)3 PollableChannel (org.springframework.messaging.PollableChannel)3 ErrorMessage (org.springframework.messaging.support.ErrorMessage)3 HashMap (java.util.HashMap)2