use of org.springframework.data.redis.connection.RedisConnection 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;
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_GET_ALL.
@Override
protected MultiGetResult<K, V> do_GET_ALL(Set<? extends K> keys) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
ArrayList<K> keyList = new ArrayList<>(keys);
byte[][] newKeys = keyList.stream().map((k) -> buildKey(k)).toArray(byte[][]::new);
Map<K, CacheGetResult<V>> resultMap = new HashMap<>();
if (newKeys.length > 0) {
List mgetResults = con.mGet(newKeys);
for (int i = 0; i < mgetResults.size(); i++) {
Object value = mgetResults.get(i);
K key = keyList.get(i);
if (value != null) {
CacheValueHolder<V> holder = (CacheValueHolder<V>) valueDecoder.apply((byte[]) value);
if (System.currentTimeMillis() >= holder.getExpireTime()) {
resultMap.put(key, CacheGetResult.EXPIRED_WITHOUT_MSG);
} else {
CacheGetResult<V> r = new CacheGetResult<>(CacheResultCode.SUCCESS, null, holder);
resultMap.put(key, r);
}
} else {
resultMap.put(key, CacheGetResult.NOT_EXISTS_WITHOUT_MSG);
}
}
}
return new MultiGetResult<>(CacheResultCode.SUCCESS, null, resultMap);
} catch (Exception ex) {
logError("GET_ALL", "keys(" + keys.size() + ")", ex);
return new MultiGetResult<>(ex);
} finally {
closeConnection(con);
}
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_PUT_ALL.
@Override
protected CacheResult do_PUT_ALL(Map<? extends K, ? extends V> map, long expireAfterWrite, TimeUnit timeUnit) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
int failCount = 0;
for (Map.Entry<? extends K, ? extends V> en : map.entrySet()) {
CacheValueHolder<V> holder = new CacheValueHolder(en.getValue(), timeUnit.toMillis(expireAfterWrite));
Boolean result = con.pSetEx(buildKey(en.getKey()), timeUnit.toMillis(expireAfterWrite), valueEncoder.apply(holder));
if (!Boolean.TRUE.equals(result)) {
failCount++;
}
}
return failCount == 0 ? CacheResult.SUCCESS_WITHOUT_MSG : failCount == map.size() ? CacheResult.FAIL_WITHOUT_MSG : CacheResult.PART_SUCCESS_WITHOUT_MSG;
} catch (Exception ex) {
logError("PUT_ALL", "map(" + map.size() + ")", ex);
return new CacheResult(ex);
} finally {
closeConnection(con);
}
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_GET.
@Override
protected CacheGetResult<V> do_GET(K key) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
byte[] newKey = buildKey(key);
byte[] resultBytes = con.get(newKey);
if (resultBytes != null) {
CacheValueHolder<V> holder = (CacheValueHolder<V>) valueDecoder.apply((byte[]) resultBytes);
if (System.currentTimeMillis() >= holder.getExpireTime()) {
return CacheGetResult.EXPIRED_WITHOUT_MSG;
}
return new CacheGetResult(CacheResultCode.SUCCESS, null, holder);
} else {
return CacheGetResult.NOT_EXISTS_WITHOUT_MSG;
}
} catch (Exception ex) {
logError("GET", key, ex);
return new CacheGetResult(ex);
} finally {
closeConnection(con);
}
}
use of org.springframework.data.redis.connection.RedisConnection in project jetcache by alibaba.
the class RedisSpringDataCache method do_REMOVE.
@Override
protected CacheResult do_REMOVE(K key) {
RedisConnection con = null;
try {
con = connectionFactory.getConnection();
byte[] keyBytes = buildKey(key);
Long result = con.del(keyBytes);
if (result == null) {
return new CacheResult(CacheResultCode.FAIL, "result:" + result);
} else if (result == 1) {
return CacheResult.SUCCESS_WITHOUT_MSG;
} else if (result == 0) {
return new CacheResult(CacheResultCode.NOT_EXISTS, null);
} else {
return CacheResult.FAIL_WITHOUT_MSG;
}
} catch (Exception ex) {
logError("REMOVE", key, ex);
return new CacheResult(ex);
} finally {
closeConnection(con);
}
}
Aggregations