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

The following examples show how to use org.springframework.data.redis.connection.RedisConnection#pSetEx() . 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: RedisSpringDataCache.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Override
protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
    RedisConnection con = null;
    try {
        con = connectionFactory.getConnection();
        CacheValueHolder<V> holder = new CacheValueHolder(value, timeUnit.toMillis(expireAfterWrite));
        byte[] keyBytes = buildKey(key);
        byte[] valueBytes = valueEncoder.apply(holder);
        Boolean result = con.pSetEx(keyBytes, timeUnit.toMillis(expireAfterWrite), valueBytes);
        if (Boolean.TRUE.equals(result)) {
            return CacheResult.SUCCESS_WITHOUT_MSG;
        } else {
            return new CacheResult(CacheResultCode.FAIL, "result:" + result);
        }
    } catch (Exception ex) {
        logError("PUT", key, ex);
        return new CacheResult(ex);
    } finally {
        closeConnection(con);
    }
}
 
Example 2
Source File: RedisSpringDataCache.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@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);
    }
}