Search in sources :

Example 6 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project dq-easy-cloud by dq-open-cloud.

the class EcRedisTemplateHandler method pipelineSample.

/**
 * 使用jedis管道测试用例
 */
public void pipelineSample() {
    // pipeline
    RedisCallback<Object> pipelineCallback = new RedisCallback<Object>() {

        @Override
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            Jedis jedis = (Jedis) connection.getNativeConnection();
            jedis.pipelined().lpush("", "");
            return null;
        }
    };
    stringRedisTemplate.execute(pipelineCallback);
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisCallback(org.springframework.data.redis.core.RedisCallback) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 7 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project dq-easy-cloud by dq-open-cloud.

the class EcLock method gainDistributedLock.

/**
 * 获取分布式锁
 */
private boolean gainDistributedLock(final long leaseTime, final TimeUnit timeUnit) {
    RedisCallback<String> redisCallback = new RedisCallback<String>() {

        @Override
        public String doInRedis(RedisConnection connection) throws DataAccessException {
            String px = SET_WITH_EXPIRE_TIME_MILL;
            if (TimeUnit.SECONDS.equals(timeUnit)) {
                px = SET_WITH_EXPIRE_TIME_SEC;
            }
            Jedis jedis = (Jedis) connection.getNativeConnection();
            return jedis.set(lockName, lockValue, SET_IF_NOT_EXIST, px, leaseTime);
        }
    };
    String result = stringRedisTemplateLock.execute(redisCallback);
    if (EcStringUtils.equals(LOCK_SUCCESS, result)) {
        return true;
    }
    return false;
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisCallback(org.springframework.data.redis.core.RedisCallback) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 8 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project springBoot-learn-demo by nbfujx.

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) {
        logger.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 9 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project Spring-Family by Sierou-Java.

the class RedisLock method expire.

private boolean expire(final String lockKey, final Long expireTime) {
    Object obj = null;
    try {
        obj = redisTemplate.execute(new RedisCallback<Boolean>() {

            @Override
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
                StringRedisSerializer serializer = new StringRedisSerializer();
                boolean bool = connection.expire(serializer.serialize(lockKey), expireTime);
                connection.close();
                return bool;
            }
        });
        return (Boolean) obj;
    } catch (Exception e) {
        log.error("expire redis error, key : {}", lockKey);
    }
    return 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 10 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project Spring-Family by Sierou-Java.

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[] data = connection.getSet(serializer.serialize(key), serializer.serialize(value));
                connection.close();
                return serializer.deserialize(data);
            }
        });
    } catch (Exception e) {
        log.error("getSet 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

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