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:org.springframework.data.redis.cache.RedisCache.java

public ValueWrapper putIfAbsent(Object key, final Object value) {

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

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

            waitForLock(connection);/* ww  w  .java2 s  .  com*/

            Object resultValue = value;
            boolean valueWasSet = connection.setNX(keyBytes, valueBytes);
            if (valueWasSet) {
                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);
                }
            } else {
                resultValue = deserializeIfNecessary(template.getValueSerializer(), connection.get(keyBytes));
            }

            return resultValue;
        }
    }, true));
}

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

public void evict(Object key) {
    final byte[] k = computeKey(key);

    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            connection.del(k);// w ww.  jav  a 2s. co m
            // remove key from set
            connection.zRem(setName, k);
            return null;
        }
    }, true);
}

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

public void clear() {
    // need to del each key individually
    template.execute(new RedisCallback<Object>() {
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            // another clear is on-going
            if (connection.exists(cacheLockName)) {
                return null;
            }/* ww w  .j  a  va 2  s  . co  m*/

            try {
                connection.set(cacheLockName, cacheLockName);

                int offset = 0;
                boolean finished = false;

                do {
                    // need to paginate the keys
                    Set<byte[]> keys = connection.zRange(setName, (offset) * PAGE_SIZE,
                            (offset + 1) * PAGE_SIZE - 1);
                    finished = keys.size() < PAGE_SIZE;
                    offset++;
                    if (!keys.isEmpty()) {
                        connection.del(keys.toArray(new byte[keys.size()][]));
                    }
                } while (!finished);

                connection.del(setName);
                return null;

            } finally {
                connection.del(cacheLockName);
            }
        }
    }, true);
}

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

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

        @Override/*from  w w  w.  j  a  va2 s .c  om*/
        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;
        }
    });
}