Search in sources :

Example 1 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback 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 2 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback 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 3 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback 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 4 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback 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 5 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project spring-boot-mybatis-with-redis by Lovelcp.

the class RedisCache method clear.

/**
 * Clears this cache instance
 */
@Override
public void clear() {
    RedisTemplate redisTemplate = getRedisTemplate();
    redisTemplate.execute((RedisCallback) connection -> {
        connection.flushDb();
        return null;
    });
    logger.debug("Clear all the cached query result from redis");
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) ValueOperations(org.springframework.data.redis.core.ValueOperations) Logger(org.slf4j.Logger) RedisCallback(org.springframework.data.redis.core.RedisCallback) Cache(org.apache.ibatis.cache.Cache) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) LoggerFactory(org.slf4j.LoggerFactory) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) RedisTemplate(org.springframework.data.redis.core.RedisTemplate)

Aggregations

RedisCallback (org.springframework.data.redis.core.RedisCallback)11 RedisConnection (org.springframework.data.redis.connection.RedisConnection)10 DataAccessException (org.springframework.dao.DataAccessException)7 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 Jedis (redis.clients.jedis.Jedis)3 TimeUnit (java.util.concurrent.TimeUnit)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Cache (org.apache.ibatis.cache.Cache)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)1 ValueOperations (org.springframework.data.redis.core.ValueOperations)1