Example usage for org.springframework.data.redis.connection RedisConnection exists

List of usage examples for org.springframework.data.redis.connection RedisConnection exists

Introduction

In this page you can find the example usage for org.springframework.data.redis.connection RedisConnection exists.

Prototype

@Nullable
default Boolean exists(byte[] key) 

Source Link

Document

Determine if given key exists.

Usage

From source file:example.springdata.redis.repositories.PersonRepositoryTests.java

/**
 * Save a single entity and verify that a key for the given keyspace/prefix exists. <br />
 * Print out the hash structure within Redis.
 *//*w  ww .  j a v  a 2s .c  om*/
@Test
public void saveSingleEntity() {

    repository.save(eddard);

    assertThat(operations.execute((RedisConnection connection) -> connection
            .exists(new String("persons:" + eddard.getId()).getBytes(CHARSET))), is(true));
}

From source file:grails.plugin.cache.redis.GrailsRedisCache.java

@SuppressWarnings("unchecked")
@Override//from www  .j a va2 s. c o m
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;
            }

            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:grails.plugin.cache.redis.GrailsRedisCache.java

protected boolean waitForLock(RedisConnection connection) {
    boolean foundLock = false;
    boolean retry = true;
    while (retry) {
        retry = false;//w ww. jav  a  2s. c om
        if (connection.exists(cacheLockName)) {
            foundLock = true;
            try {
                Thread thread = Thread.currentThread();

                synchronized (thread) {
                    thread.wait(WAIT_FOR_LOCK);
                }
            } catch (InterruptedException ex) {
                // ignore
            }
            retry = true;
        }
    }
    return foundLock;
}

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

/**
 * Determine if given {@code key} exists.
 * <p>//from  w ww  . j a va2 s  .  c  om
 * See http://redis.io/commands/exists
 * 
 * @param key key
 * @return Boolean
 */
public static Boolean exists(byte[] key) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.exists(key);
        }
    });
}

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

/**
 * Determine if given {@code key} exists.
 * <p>//from   w ww.j a va 2s  .  c  o m
 * See http://redis.io/commands/exists
 * 
 * @param key key
 * @return Boolean
 */
public Boolean exists(byte[] key) {
    return redisTemplate.execute(new RedisCallback<Boolean>() {
        @Override
        public Boolean doInRedis(RedisConnection redis) throws DataAccessException {
            return redis.exists(key);
        }
    });
}

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  v  a  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.RedisCache.java

private boolean waitForLock(RedisConnection connection) {

    boolean retry;
    boolean foundLock = false;
    do {/* ww  w.j a v a 2s . c  o  m*/
        retry = false;
        if (connection.exists(cacheLockName)) {
            foundLock = true;
            try {
                Thread.sleep(WAIT_FOR_LOCK);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            retry = true;
        }
    } while (retry);

    return foundLock;
}