本文整理匯總了Java中org.springframework.data.redis.connection.RedisConnection類的典型用法代碼示例。如果您正苦於以下問題:Java RedisConnection類的具體用法?Java RedisConnection怎麽用?Java RedisConnection使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
RedisConnection類屬於org.springframework.data.redis.connection包,在下文中一共展示了RedisConnection類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: updateAll
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public void updateAll() {
try {
final List<Viapp> list = appDao.getAllApp();
logger.info("BeginNewAddAppToRedis...");
long start = System.currentTimeMillis();
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.flushDb();
//connection.multi();
commonRedis.saveCommonRedis(list,connection);
// connection.exec();
return null;
}
});
logger.info("redis init finish cost:{}ms!", System.currentTimeMillis() - start);
}catch(Exception e){
e.printStackTrace();
}
}
示例2: doReadSession
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
protected Session doReadSession(Serializable sessionId) {
byte[] sessionKey = buildRedisSessionKey(sessionId);
RedisConnection redisConnection = getRedisConnection();
try {
byte[] value = redisConnection.get(sessionKey);
if (value == null) {
return null;
}
Session session = SerializeUtil.deserialize(value, SimpleSession.class);
return session;
} finally {
redisConnection.close();
}
}
示例3: put
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public void put(Object key, Object value) {
final String keyf = (String) key;
final Object valuef = value;
final long liveTime = 86400;
redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
byte[] keyb = keyf.getBytes();
byte[] valueb = toByteArray(valuef);
connection.set(keyb, valueb);
if (liveTime > 0) {
connection.expire(keyb, liveTime);
}
return 1L;
}
});
}
示例4: evict
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public void evict(Object key) {
final String keyf = (String) key;
redisTemplate.execute(new RedisCallback<Long>() {
public Long doInRedis(RedisConnection connection) throws DataAccessException {
return connection.del(keyf.getBytes());
}
});
}
示例5: getConnection
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public RedisConnection getConnection() {
initDbType();
RedisConnection redisConnection = loadConnection();
if(redisConnection==null) {
redisConnection = initLCNConnection(redisConnectionFactory.getConnection());
if(redisConnection==null){
throw new RuntimeException("connection was overload");
}
return redisConnection;
}else {
return redisConnection;
}
}
示例6: tryLock
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
public Boolean tryLock(){
String uuid = UUID.randomUUID().toString();
Boolean success = (Boolean) redisTemplate.execute((RedisConnection connection) -> {
Jedis jedis = (Jedis)connection.getNativeConnection();
String res = jedis.set(lockKey, uuid, NX, PX, maxLockTime);
if (res != null && res.equals("OK")) {
return true;
}else{
return false;
}
});
if(success){
this.lockValue = uuid;
return true;
}else{
return false;
}
}
示例7: releaseLock
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
public Boolean releaseLock(){
Boolean success = (Boolean) redisTemplate.execute((RedisConnection connection) -> {
Jedis jedis = (Jedis)connection.getNativeConnection();
Long res = (Long)jedis.eval(SCRIPT_CHECK_THEN_DELETE, Collections.singletonList(lockKey), Collections.singletonList(lockValue));
if (res != null && res.equals(1L)) {
return true;
}else{
return false;
}
});
if(success){
return true;
}else{
return false;
}
}
示例8: testCallback
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Test
public void testCallback() {
final Properties[] result = {null};
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
Properties properties = connection.info();
System.out.println(properties);
properties = connection.info("CPU");
System.out.println(properties);
result[0] = properties;
return null;
}
});
System.out.println(result[0]);
}
示例9: testCallback2
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Test
public void testCallback2() {
final Properties[] result = {null};
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
Properties properties = connection.info("keyspace");
System.out.println(properties);
Enumeration<String> nameList = (Enumeration<String>) properties.propertyNames();
while (nameList.hasMoreElements()) {
System.out.println(nameList.nextElement());
}
result[0] = properties;
return null;
}
});
}
示例10: testCallback3
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Test
public void testCallback3() {
final Properties[] result = {null};
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
Properties properties = connection.info();
System.out.println(properties);
Enumeration<String> nameList = (Enumeration<String>) properties.propertyNames();
while (nameList.hasMoreElements()) {
System.out.println(nameList.nextElement());
}
result[0] = properties;
return null;
}
});
}
示例11: get
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public <T> T get(final String key, final Class clazz) {
T result = redisTemplate.execute(new RedisCallback<T>() {
public T doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = getRedisSerializer();
byte[] keyStr = serializer.serialize(key);
byte[] value = connection.get(keyStr);
if (value == null) {
return null;
}
String valueStr = serializer.deserialize(value);
return (T) JSON.parseObject(valueStr, clazz);
}
});
return result;
}
示例12: getQueued
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public List<Execution> getQueued(String queue) {
return redis.execute((RedisConnection connection) -> {
// Get all ids from queue.
List<byte[]> idsBytes = connection.lRange(key(QUEUE, queue), 0, -1);
if (isEmpty(idsBytes)) {
return emptyList();
}
// Lookup all executions for all ids at once.
List<byte[]> executionsBytes = connection.hMGet(key(JOBS), idsBytes.toArray(new byte[idsBytes.size()][]));
if (isEmpty(executionsBytes)) {
return emptyList();
}
Assert.isTrue(executionsBytes.size() == idsBytes.size(),
"Precondition violated: Redis response has the expected length.");
return executionsBytes.stream()
.map(this::parseExecution)
.filter(Objects::nonNull)
.collect(toList());
});
}
示例13: redisIsUp
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Test
public void redisIsUp() throws Exception {
Properties info = new Properties();
info.put("redis_version", "2.8.9");
RedisConnection redisConnection = mock(RedisConnection.class);
RedisConnectionFactory redisConnectionFactory = mock(
RedisConnectionFactory.class);
given(redisConnectionFactory.getConnection()).willReturn(redisConnection);
given(redisConnection.info()).willReturn(info);
RedisHealthIndicator healthIndicator = new RedisHealthIndicator(
redisConnectionFactory);
Health health = healthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("version")).isEqualTo("2.8.9");
verify(redisConnectionFactory).getConnection();
verify(redisConnection).info();
}
示例14: hGetString
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
public static String hGetString(final String key, final String field, final Long seconds) {
String result = (String) redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
try {
byte[] byteKey = key.getBytes("utf-8");
byte[] value = connection.hGet(byteKey, field.getBytes("utf-8"));
if (value == null) {
return null;
}
if (seconds != null) {
connection.expire(byteKey, seconds);
}
return new String(value, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("不支持的編碼類型utf-8", e);
}
}
}, true, false);
return result;
}
示例15: hMGet
import org.springframework.data.redis.connection.RedisConnection; //導入依賴的package包/類
@Override
public List<byte[]> hMGet(final String redisKey, final List<String> mapKeys) {
return redisTemplate.execute(new RedisCallback<List<byte[]>>() {
@Override
public List<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {
byte[][] keys = new byte[mapKeys.size()][];
for (int i = 0; i < mapKeys.size(); i++) {
keys[i] = mapKeys.get(i).getBytes();
}
return connection.hMGet(redisKey.getBytes(), keys);
}
});
}