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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations