Java Code Examples for org.springframework.data.redis.connection.RedisConnection#get()

The following examples show how to use org.springframework.data.redis.connection.RedisConnection#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisAuthorizationCodeServices.java    From springcloud-oauth2 with MIT License 6 votes vote down vote up
/**
 * 取出授权码并删除授权码(权限码只能用一次,调试时可不删除,code就可多次使用)
 *
 * @param code
 * @return org.springframework.security.oauth2.provider.OAuth2Authentication
 */
@Override
protected OAuth2Authentication remove(String code) {
    byte[] serializedKey = serializeKey(AUTHORIZATION_CODE + code);
    RedisConnection conn = getConnection();
    byte[] bytes;
    try {
        bytes = conn.get(serializedKey);
        if (bytes != null) {
            conn.del(serializedKey);
        }
    } finally {
        conn.close();
    }
    return deserializeAuthentication(bytes);
}
 
Example 2
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 6 votes vote down vote up
private void removeAccessTokenUsingRefreshToken(String refreshToken) {
    byte[] key = serializeKey(REFRESH_TO_ACCESS + refreshToken);
    List<Object> results = null;
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.get(key);
        conn.del(key);
        results = conn.closePipeline();
    } finally {
        conn.close();
    }
    if (results == null) {
        return;
    }
    byte[] bytes = (byte[]) results.get(0);
    String accessToken = deserializeString(bytes);
    if (accessToken != null) {
        removeAccessToken(accessToken);
    }
}
 
Example 3
Source File: RedisClient.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 从缓存中取对象
 *
 * @param key
 * @param <T>
 * @return
 */
public <T> T getObject(String key) {
    byte[] keyBytes = getKey(key);
    byte[] result = null;

    RedisConnection redisConnection = getConnection();

    if(null!=redisConnection){
        try {
            result = redisConnection.get(keyBytes);
        }finally {
            releaseConnection(redisConnection);
        }
    }else{
        logger.error("2. can not get valid connection");
    }

    return null!=redisConnection ? (T) redisTemplate.getValueSerializer().deserialize(result) : null;
}
 
Example 4
Source File: RedisClient.java    From blog_demos with Apache License 2.0 6 votes vote down vote up
/**
 * 从缓存中取对象
 *
 * @param key
 * @param <T>
 * @return
 */
public <T> T getObject(String key) {
    byte[] keyBytes = getKey(key);
    byte[] result = null;

    RedisConnection redisConnection = getConnection();

    if(null!=redisConnection){
        try {
            result = redisConnection.get(keyBytes);
        }finally {
            releaseConnection(redisConnection);
        }
    }else{
        logger.error("2. can not get valid connection");
    }

    return null!=redisConnection ? (T) redisTemplate.getValueSerializer().deserialize(result) : null;
}
 
Example 5
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 6 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
    String key = authenticationKeyGenerator.extractKey(authentication);
    byte[] serializedKey = serializeKey(AUTH_TO_ACCESS + key);
    byte[] bytes = null;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializedKey);
    } finally {
        conn.close();
    }
    OAuth2AccessToken accessToken = deserializeAccessToken(bytes);
    if (accessToken != null) {
        OAuth2Authentication storedAuthentication = readAuthentication(accessToken.getValue());
        if ((storedAuthentication == null || !key.equals(authenticationKeyGenerator.extractKey(storedAuthentication)))) {
            // Keep the stores consistent (maybe the same user is
            // represented by this authentication but the details have
            // changed)
            storeAccessToken(accessToken, authentication);
        }

    }
    return accessToken;
}
 
Example 6
Source File: RedisService.java    From xmfcn-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * getCache(获取缓存数据)
 *
 * @param key
 * @return
 */
@RequestMapping("getCache")
public String getCache(String key) {
    logger.info("getCache(获取缓存数据)redis 开始 " + key);
    String result = null;
    if (StringUtil.isBlank(key)) {
        return result;
    }
    RedisConnection conn = getRedisConnection();
    if (conn == null) {
        return result;
    }
    try {
        byte[] bytes = conn.get(key.getBytes());
        if (bytes != null) {
            result = new String(bytes, "utf-8");
        }
    } catch (Exception e) {
        logger.error("getCache:" + StringUtil.getExceptionMsg(e));
    }
    return result;
}
 
Example 7
Source File: RootController.java    From hcp-cloud-foundry-tutorials with Apache License 2.0 6 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
public @ResponseBody Result onRootAccess() {

    RedisConnection redisConnection = redisConnectionFactory.getConnection();
    log.info("Setting key/value pair 'hello'/'world'");
    redisConnection.set("hello".getBytes(), "world".getBytes());

    log.info("Retrieving value for key 'hello': ");
    final String value = new String(redisConnection.get("hello".getBytes()));
    Result result = new Result();
    result.setStatus("Succesfully connected to Redis and retrieved the key/value pair that was inserted");
    RedisObject redisObject = new RedisObject();
    redisObject.setKey("hello");
    redisObject.setValue(value);
    final ArrayList<RedisObject> redisObjects = new ArrayList<RedisObject>();
    redisObjects.add(redisObject);
    result.setRedisObjects(redisObjects);
    return result;
}
 
Example 8
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
private void removeAccessTokenUsingRefreshToken(String refreshToken) {
    byte[] key = serializeKey(REFRESH_TO_ACCESS + refreshToken);
    List<Object> results = null;
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.get(key);
        conn.del(key);
        results = conn.closePipeline();
    } finally {
        conn.close();
    }
    if (results == null) {
        return;
    }
    byte[] bytes = (byte[]) results.get(0);
    String accessToken = deserializeString(bytes);
    if (accessToken != null) {
        removeAccessToken(accessToken);
    }
}
 
Example 9
Source File: RedisSpringDataCache.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@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);
    }
}
 
Example 10
Source File: RedisLockProvider.java    From ShedLock with Apache License 2.0 6 votes vote down vote up
@NonNull
@Override
public Optional<SimpleLock> lock(@NonNull LockConfiguration lockConfiguration) {
    String key = buildKey(lockConfiguration.getName());
    RedisConnection redisConnection = null;
    try {
        byte[] keyBytes = key.getBytes();
        redisConnection = redisConnectionFactory.getConnection();
        if (redisConnection.setNX(keyBytes, buildValue(lockConfiguration.getLockAtMostUntil()))) {
            return Optional.of(new RedisLock(key, redisConnectionFactory, lockConfiguration));
        } else {
            byte[] value = redisConnection.get(keyBytes);
            if(isUnlocked(value)) {
                byte[] maybeOldValue = redisConnection.getSet(keyBytes, buildValue(lockConfiguration.getLockAtMostUntil()));
                if(isUnlocked(maybeOldValue)) {
                    return Optional.of(new RedisLock(key, redisConnectionFactory, lockConfiguration));
                }
            }
            return Optional.empty();
        }
    } finally {
        close(redisConnection);
    }
}
 
Example 11
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
/**
 * 获取token的总有效时长
 * @param clientId 应用id
 */
private int getAccessTokenValiditySeconds(String clientId) {
    RedisConnection conn = getConnection();
    byte[] bytes;
    try {
        bytes = conn.get(serializeKey(SecurityConstants.CACHE_CLIENT_KEY + ":" + clientId));
    } finally {
        conn.close();
    }
    if (bytes != null) {
        ClientDetails clientDetails = deserializeClientDetails(bytes);
        if (clientDetails.getAccessTokenValiditySeconds() != null) {
            return clientDetails.getAccessTokenValiditySeconds();
        }
    }

    //返回默认值
    return SecurityConstants.ACCESS_TOKEN_VALIDITY_SECONDS;
}
 
Example 12
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
    String key = authenticationKeyGenerator.extractKey(authentication);
    byte[] serializedKey = serializeKey(AUTH_TO_ACCESS + key);
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializedKey);
    } finally {
        conn.close();
    }
    OAuth2AccessToken accessToken = deserializeAccessToken(bytes);
    if (accessToken != null) {
        OAuth2Authentication storedAuthentication = readAuthentication(accessToken.getValue());
        if ((storedAuthentication == null || !key.equals(authenticationKeyGenerator.extractKey(storedAuthentication)))) {
            // Keep the stores consistent (maybe the same user is
            // represented by this authentication but the details have
            // changed)
            storeAccessToken(accessToken, authentication);
        }

    }
    return accessToken;
}
 
Example 13
Source File: RedisUtils.java    From rqueue with Apache License 2.0 6 votes vote down vote up
public static int updateAndGetVersion(
    RedisConnectionFactory redisConnectionFactory, String versionDbKey, int defaultVersion) {
  RedisConnection connection = RedisConnectionUtils.getConnection(redisConnectionFactory);
  byte[] versionKey = versionDbKey.getBytes();
  byte[] versionFromDb = connection.get(versionKey);
  if (SerializationUtils.isEmpty(versionFromDb)) {
    Long count =
        connection.eval(
            "return #redis.pcall('keys', 'rqueue-*')".getBytes(), ReturnType.INTEGER, 0);
    if (count != null && count > 0L) {
      int version = 1;
      connection.set(versionKey, String.valueOf(version).getBytes());
      return version;
    }
    connection.set(versionKey, String.valueOf(defaultVersion).getBytes());
    return defaultVersion;
  }
  return Integer.parseInt(new String(versionFromDb));
}
 
Example 14
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public OAuth2RefreshToken readRefreshToken(String tokenValue) {
    byte[] key = serializeKey(REFRESH + tokenValue);
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(key);
    } finally {
        conn.close();
    }
    return deserializeRefreshToken(bytes);
}
 
Example 15
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public OAuth2AccessToken readAccessToken(String tokenValue) {
    byte[] key = serializeKey(ACCESS + tokenValue);
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(key);
    } finally {
        conn.close();
    }
    return deserializeAccessToken(bytes);
}
 
Example 16
Source File: CustomRedisTokenStore.java    From microservices-platform with Apache License 2.0 5 votes vote down vote up
@Override
public OAuth2Authentication readAuthentication(String token) {
    byte[] bytes;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(serializeKey(SecurityConstants.REDIS_TOKEN_AUTH + token));
    } finally {
        conn.close();
    }
    return deserializeAuthentication(bytes);
}
 
Example 17
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 5 votes vote down vote up
public OAuth2Authentication readAuthenticationForRefreshToken(String token) {
    RedisConnection conn = getConnection();
    try {
        byte[] bytes = conn.get(serializeKey(REFRESH_AUTH + token));
        OAuth2Authentication auth = deserializeAuthentication(bytes);
        return auth;
    } finally {
        conn.close();
    }
}
 
Example 18
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 5 votes vote down vote up
@Override
public OAuth2AccessToken readAccessToken(String tokenValue) {
    byte[] key = serializeKey(ACCESS + tokenValue);
    byte[] bytes = null;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(key);
    } finally {
        conn.close();
    }
    OAuth2AccessToken accessToken = deserializeAccessToken(bytes);
    return accessToken;
}
 
Example 19
Source File: CustomRedisTokenStore.java    From Auth-service with MIT License 5 votes vote down vote up
@Override
public OAuth2RefreshToken readRefreshToken(String tokenValue) {
    byte[] key = serializeKey(REFRESH + tokenValue);
    byte[] bytes = null;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(key);
    } finally {
        conn.close();
    }
    OAuth2RefreshToken refreshToken = deserializeRefreshToken(bytes);
    return refreshToken;
}
 
Example 20
Source File: StringDataRedisTest.java    From tac with MIT License 3 votes vote down vote up
@Test
public void testIncre() {

    RedisConnection connection = template.getConnectionFactory().getConnection();
    byte[] key = Bytes.toBytes("ljinshuan123");
    Long incr = connection.incr(key);

    byte[] bytes = connection.get(key);

    connection.set(key, bytes);

    connection.incr(key);
    bytes = connection.get(key);

}