Example usage for org.springframework.data.redis.core RedisCallback RedisCallback

List of usage examples for org.springframework.data.redis.core RedisCallback RedisCallback

Introduction

In this page you can find the example usage for org.springframework.data.redis.core RedisCallback RedisCallback.

Prototype

RedisCallback

Source Link

Usage

From source file:com.zxy.commons.cache.RedisHelper.java

/**
 * Return the approximated cardinality of the structures observed by the HyperLogLog at {@literal key(s)}.
 * /*from   ww w  .j  av  a  2 s.  co m*/
 * @param keys keys
 * @return Long
 */
public Long pfCount(byte[]... keys) {
    return redisTemplate.execute(new RedisCallback<Long>() {
        @Override
        public Long doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.pfCount(keys);
        }
    });
}

From source file:com.zxy.commons.cache.RedisUtils.java

/**
 * Return the approximated cardinality of the structures observed by the HyperLogLog at {@literal key(s)}.
 * //w ww  .j a v a 2s.  c om
 * @param keys keys
 * @return Long
 */
public static Long pfCount(byte[]... keys) {
    return redisTemplate.execute(new RedisCallback<Long>() {
        @Override
        public Long doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.pfCount(keys);
        }
    });
}

From source file:com.zxy.commons.cache.RedisHelper.java

/**
 * Merge N different HyperLogLogs at {@literal sourceKeys} into a single {@literal destinationKey}.
 * /*from   w  w w .j a  va 2  s  .  co m*/
 * @param destinationKey destinationKey
 * @param sourceKeys sourceKeys
 */
public void pfMerge(byte[] destinationKey, byte[]... sourceKeys) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.pfMerge(destinationKey, sourceKeys);
            return null;
        }
    });
}

From source file:com.zxy.commons.cache.RedisUtils.java

/**
 * Merge N different HyperLogLogs at {@literal sourceKeys} into a single {@literal destinationKey}.
 * //from   ww  w .  j a va2  s  .  c  om
 * @param destinationKey destinationKey
 * @param sourceKeys sourceKeys
 */
public static void pfMerge(byte[] destinationKey, byte[]... sourceKeys) {
    redisTemplate.execute(new RedisCallback<Void>() {
        @Override
        public Void doInRedis(RedisConnection redis) throws DataAccessException {
            redis.pfMerge(destinationKey, sourceKeys);
            return null;
        }
    });
}

From source file:org.mitre.mpf.wfm.data.RedisImpl.java

/** Removes everything in the Redis datastore. */
public void clear() {
    redisTemplate.execute(new RedisCallback() {
        @Override// w w w  .  j a v a  2s . co  m
        public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
            redisConnection.flushAll();
            return null;
        }
    });
}

From source file:org.mitre.mpf.wfm.data.RedisImpl.java

@SuppressWarnings("unchecked")
public synchronized SortedSet<DetectionProcessingError> getDetectionProcessingErrors(long jobId, long mediaId,
        int taskIndex, int actionIndex) {
    final String key = key(JOB, jobId, MEDIA, mediaId, ERRORS, taskIndex, actionIndex);
    int length = (Integer) (redisTemplate.execute(new RedisCallback() {
        @Override//from  w  ww  .j  av a2 s .c om
        public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
            return Integer.valueOf(redisConnection.execute("llen", key.getBytes()).toString());
        }
    }));

    if (length == 0) {
        log.debug("No detection processing errors for JOB:{}:MEDIA:{}:{}:{}.", jobId, mediaId, taskIndex,
                actionIndex);
        return new TreeSet<>();
    } else {
        log.debug("{} detection processing errors for JOB:{}:MEDIA:{}:{}:{}.", length, jobId, mediaId,
                taskIndex, actionIndex);
        SortedSet<DetectionProcessingError> errors = new TreeSet<>();
        for (Object errorJson : redisTemplate.boundListOps(key).range(0, length)) {
            try {
                errors.add(jsonUtils.deserialize((byte[]) (errorJson), DetectionProcessingError.class));
            } catch (Exception exception) {
                log.warn("Failed to deserialize '{}'.", errorJson);
            }
        }
        return errors;
    }
}

From source file:org.mitre.mpf.wfm.data.RedisImpl.java

@SuppressWarnings("unchecked")
public synchronized SortedSet<Track> getTracks(long jobId, long mediaId, int taskIndex, int actionIndex) {
    final String key = key(JOB, jobId, MEDIA, mediaId, TRACK, taskIndex, actionIndex);
    int length = (Integer) (redisTemplate.execute(new RedisCallback() {
        @Override/*from  w  w w.j a  v a2s . c om*/
        public Object doInRedis(RedisConnection redisConnection) throws DataAccessException {
            return Integer.valueOf(redisConnection.execute("llen", key.getBytes()).toString());
        }
    }));

    if (length == 0) {
        log.debug("No tracks for JOB:{}:MEDIA:{}:{}:{}.", jobId, mediaId, taskIndex, actionIndex);
        return new TreeSet<>();
    } else {
        log.debug("{} tracks for JOB:{}:MEDIA:{}:{}:{}.", length, jobId, mediaId, taskIndex, actionIndex);
        SortedSet<Track> tracks = new TreeSet<>();
        for (Object trackJson : redisTemplate.boundListOps(key).range(0, length)) {
            try {
                tracks.add(jsonUtils.deserialize((byte[]) (trackJson), Track.class));
            } catch (Exception exception) {
                log.warn("Failed to deserialize '{}'.", trackJson);
            }
        }
        return tracks;
    }
}

From source file:org.redis.cache.RedisCacheManager.java

@SuppressWarnings("unchecked")
protected Set<String> loadRemoteCacheKeys() {
    return (Set<String>) redisOperations.execute(new RedisCallback<Set<String>>() {

        @Override//from  w w w  . ja v a  2 s . co  m
        public Set<String> doInRedis(RedisConnection connection) throws DataAccessException {

            // we are using the ~keys postfix as defined in
            // RedisCache#setName
            Set<byte[]> keys = connection.keys(redisOperations.getKeySerializer().serialize("*~keys"));
            Set<String> cacheKeys = new LinkedHashSet<String>();

            if (!CollectionUtils.isEmpty(keys)) {
                for (byte[] key : keys) {
                    cacheKeys.add(redisOperations.getKeySerializer().deserialize(key).toString()
                            .replace("~keys", ""));
                }
            }

            return cacheKeys;
        }
    });
}

From source file:org.springframework.data.redis.cache.RedisCache.java

public ValueWrapper get(final Object key) {
    return (ValueWrapper) template.execute(new RedisCallback<ValueWrapper>() {

        public ValueWrapper doInRedis(RedisConnection connection) throws DataAccessException {
            waitForLock(connection);/*from  w w  w . j a  va  2 s .  c  o  m*/
            byte[] bs = connection.get(computeKey(key));
            Object value = template.getValueSerializer() != null ? template.getValueSerializer().deserialize(bs)
                    : bs;
            return (bs == null ? null : new SimpleValueWrapper(value));
        }
    }, true);
}

From source file:org.springframework.data.redis.cache.RedisCache.java

public void put(final Object key, final Object value) {

    final byte[] keyBytes = computeKey(key);
    final byte[] valueBytes = convertToBytesIfNecessary(template.getValueSerializer(), value);

    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {

            waitForLock(connection);//from w  w w .ja  v a 2s  .c  o  m

            connection.multi();

            connection.set(keyBytes, valueBytes);
            connection.zAdd(setName, 0, keyBytes);

            if (expiration > 0) {
                connection.expire(keyBytes, expiration);
                // update the expiration of the set of keys as well
                connection.expire(setName, expiration);
            }
            connection.exec();

            return null;
        }
    }, true);
}