Search in sources :

Example 36 with RedisConnection

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;
}
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 37 with RedisConnection

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);
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) java.util(java.util) com.alicp.jetcache(com.alicp.jetcache) Logger(org.slf4j.Logger) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Expiration(org.springframework.data.redis.core.types.Expiration) LoggerFactory(org.slf4j.LoggerFactory) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisStringCommands(org.springframework.data.redis.connection.RedisStringCommands) Function(java.util.function.Function) AbstractExternalCache(com.alicp.jetcache.external.AbstractExternalCache) RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 38 with RedisConnection

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);
    }
}
Also used : RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 39 with RedisConnection

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);
    }
}
Also used : RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 40 with RedisConnection

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);
    }
}
Also used : RedisConnectionFailureException(org.springframework.data.redis.RedisConnectionFailureException) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Aggregations

RedisConnection (org.springframework.data.redis.connection.RedisConnection)41 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)9 RedisConnectionFailureException (org.springframework.data.redis.RedisConnectionFailureException)8 Properties (java.util.Properties)7 DataAccessException (org.springframework.dao.DataAccessException)7 RedisCallback (org.springframework.data.redis.core.RedisCallback)7 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 Test (org.junit.Test)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)3 com.alicp.jetcache (com.alicp.jetcache)2 AbstractExternalCache (com.alicp.jetcache.external.AbstractExternalCache)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2 Logger (org.slf4j.Logger)2