Java Code Examples for redis.clients.jedis.ShardedJedisPipeline#sync()
The following examples show how to use
redis.clients.jedis.ShardedJedisPipeline#sync() .
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: ShardedJedisPoolStream.java From RedisDirectory with Apache License 2.0 | 6 votes |
/** * Use transactions to add index file and then delete the old one * * @param fileLengthKey the key using for hash file length * @param fileDataKey the key using for hash file data * @param oldField the old hash field * @param newField the new hash field * @param values the data values of the old hash field * @param fileLength the data length of the old hash field */ @Override public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long fileLength) { ShardedJedis shardedJedis = getShardedJedis(); ShardedJedisPipeline pipelined = shardedJedis.pipelined(); //add new file length pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength)); //add new file content Long blockSize = getBlockSize(fileLength); for (int i = 0; i < blockSize; i++) { pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i))); } pipelined.sync(); shardedJedis.close(); values.clear(); deleteFile(fileLengthKey, fileDataKey, oldField, blockSize); }
Example 2
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 6 votes |
@Override public List<String> lrangeltrim(final String key, final int count) { Assert.hasText(key); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipeline = jedis.pipelined(); final Response<List<String>> values = pipeline.lrange(key, 0, count); pipeline.ltrim(key, count + 1, -1); pipeline.sync(); return values.get(); } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 3
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 6 votes |
@Override public Map<String, Boolean> hsetByNX(final String key, final Map<String, Object> map) { Assert.hasText(key); Assert.notEmpty(map); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipeline = jedis.pipelined(); final Map<String, Response<Long>> responses = Maps.newHashMap(); for (Entry<String, Object> entry : map.entrySet()) { responses.put(entry.getKey(), pipeline.hsetnx(key, entry.getKey(), toJSONString(entry.getValue()))); } pipeline.sync(); final Map<String, Boolean> values = Maps.newHashMap(); responses.forEach((field, response) -> values.put(field, isSuccess(response.get()))); return values; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 4
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 6 votes |
@Override public Map<String, Boolean> setByNX(final Map<String, Object> map) { Assert.notEmpty(map); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipelined = jedis.pipelined(); final Map<String, Response<Long>> responses = Maps.newHashMap(); for (Entry<String, Object> entry : map.entrySet()) { responses.put(entry.getKey(), pipelined.setnx(entry.getKey(), toJSONString(entry.getValue()))); } pipelined.sync(); final Map<String, Boolean> values = Maps.newHashMap(); responses.forEach((key, response) -> values.put(key, isSuccess(response.get()))); return values; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 5
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 6 votes |
@Override public boolean setByNX(final String key, final String value, final int timeout) { Assert.hasText(key); Assert.hasText(value); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipeline = jedis.pipelined(); final Response<Long> set = pipeline.setnx(key, value); final Response<Long> expire = pipeline.expire(key, timeout); pipeline.sync(); return isSuccess(set.get()) && isSuccess(expire.get()); } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 6
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 6 votes |
@Override public Map<String, String> get(final String... keys) { Assert.notEmpty(keys); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipelined = jedis.pipelined(); final Map<String, Response<String>> values = Maps.newHashMap(); for (String key : keys) { values.put(key, pipelined.get(key)); } pipelined.sync(); final Map<String, String> valueMap = Maps.newHashMap(); values.forEach((key, response) -> valueMap.put(key, response.get())); return valueMap; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 7
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 6 votes |
@Override public Map<String, Long> scard(final String... keys) { Assert.notEmpty(keys); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipeline = jedis.pipelined(); final Map<String, Response<Long>> responses = Maps.newHashMap(); for (String key : keys) { responses.put(key, pipeline.scard(key)); } pipeline.sync(); final Map<String, Long> values = Maps.newHashMap(); responses.forEach((key, response) -> values.put(key, response.get())); return values; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 8
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 5 votes |
@Override public long sunionstore(final String destination, final String... keys) { Assert.hasText(destination); if (keys.length == 0) { return 0; } ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final Collection<Jedis> allShards; if ((allShards = jedis.getAllShards()).size() == 1) { return allShards.iterator().next().sunionstore(destination, keys); } else if (allShards.size() > 1) { final Set<String> unionSet = sunion(keys); if (!unionSet.isEmpty()) { final ShardedJedisPipeline pipeline = jedis.pipelined(); pipeline.del(destination); final Response<Long> response = pipeline.sadd(destination, unionSet.toArray(new String[unionSet.size()])); pipeline.sync(); return response.get(); } } return 0; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 9
Source File: ShardedJedisPoolStream.java From RedisDirectory with Apache License 2.0 | 5 votes |
@Override public List<byte[]> loadFileOnce(String fileDataKey, String fileName, long blockSize) { ShardedJedis shardedJedis = getShardedJedis(); ShardedJedisPipeline pipelined = shardedJedis.pipelined(); List<byte[]> res = new ArrayList<>(); List<Response<byte[]>> temps = new ArrayList<>(); int temp = 0; //如果不分批次sync容易read time out和Java heap space while (temp < blockSize) { Response<byte[]> data = pipelined.hget(fileDataKey.getBytes(), getBlockName(fileName, temp)); temps.add(data); if (temp % Constants.SYNC_COUNT == 0) { pipelined.sync(); res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList())); temps.clear(); pipelined = shardedJedis.pipelined(); } temp++; } try { pipelined.sync(); } catch (JedisConnectionException e) { log.error("pipelined = {}, blockSize = {}!", pipelined.toString(), blockSize); log.error("", e); } finally { shardedJedis.close(); } res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList())); temps.clear(); return res; }
Example 10
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 5 votes |
@Override public long sinterstore(final String destination, final String... keys) { Assert.hasText(destination); if (keys.length == 0) { return 0; } ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final Collection<Jedis> allShards; if ((allShards = jedis.getAllShards()).size() == 1) { return allShards.iterator().next().sinterstore(destination, keys); } else if (allShards.size() > 1) { Set<String> interSet = sinter(keys); if (!interSet.isEmpty()) { final ShardedJedisPipeline pipeline = jedis.pipelined(); pipeline.del(destination); final Response<Long> response = pipeline.sadd(destination, interSet.toArray(new String[interSet.size()])); pipeline.sync(); return response.get(); } } return 0; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 11
Source File: ShardedJedisPipelineTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void canRetrieveUnsetKey() { ShardedJedisPipeline p = jedis.pipelined(); Response<String> shouldNotExist = p.get(UUID.randomUUID().toString()); p.sync(); assertNull(shouldNotExist.get()); }
Example 12
Source File: ShardedJedisDemo.java From spring-redis-plugin with Apache License 2.0 | 5 votes |
@Override @Redis(shard = true) public void incr(String key, int times) { ShardedJedisPipeline pipeline = shardedJedis.pipelined(); for (int i = 0; i < times; i++) { pipeline.incr(key + i); } Response<String> response = pipeline.get(key + 1); pipeline.sync(); LOGGER.info(response.get()); }
Example 13
Source File: ShardedJedisCacheManager.java From AutoLoadCache with Apache License 2.0 | 5 votes |
@Override public void mset(Collection<MSetParam> params) { ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); pipeline.setShardedJedis(shardedJedis); try { JedisUtil.executeMSet(pipeline, this.cacheManager, params); } catch (Exception ex) { log.error(ex.getMessage(), ex); } pipeline.sync(); }
Example 14
Source File: ShardedJedisCacheManager.java From AutoLoadCache with Apache License 2.0 | 5 votes |
@Override public void delete(Set<CacheKeyTO> keys) { ShardedJedisPipeline pipeline = new ShardedJedisPipeline(); pipeline.setShardedJedis(shardedJedis); JedisUtil.executeDelete(pipeline, keys); pipeline.sync(); }
Example 15
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 5 votes |
@Override public long del(final String... keys) { if (keys.length == 0) { return 0; } ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipeline = jedis.pipelined(); final List<Response<Long>> responses = new ArrayList<>(); for (String key : keys) { responses.add(pipeline.del(key)); } pipeline.sync(); final AtomicLong dels = new AtomicLong(0); if (!CollectionUtils.isEmpty(responses)) { responses.forEach(res -> dels.addAndGet(res.get())); } return dels.get(); } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 16
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 5 votes |
@Override public long sdiffstore(final String destination, final String... keys) { Assert.hasText(destination); Assert.notEmpty(keys); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final Collection<Jedis> allShards; if ((allShards = jedis.getAllShards()).size() == 1) { return allShards.iterator().next().sdiffstore(destination, keys); } else if (allShards.size() > 1) { final Set<String> diffSet = sdiff(keys); if (!diffSet.isEmpty()) { final ShardedJedisPipeline pipeline = jedis.pipelined(); pipeline.del(destination); final Response<Long> response = pipeline.sadd(destination, diffSet.toArray(new String[diffSet.size()])); pipeline.sync(); return response.get(); } } return 0; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 17
Source File: RedisClientImpl.java From nano-framework with Apache License 2.0 | 5 votes |
@Override public long sreplace(final String key, final String[] oldMembers, final String[] newMembers) { Assert.hasText(key); ShardedJedis jedis = null; try { jedis = POOL.getJedis(config.getRedisType()); final ShardedJedisPipeline pipeline = jedis.pipelined(); if (!ArrayUtils.isEmpty(oldMembers)) { pipeline.srem(key, oldMembers); } Response<Long> response = null; if (!ArrayUtils.isEmpty(newMembers)) { response = pipeline.sadd(key, newMembers); } pipeline.sync(); if (response != null) { return response.get(); } return 0; } catch (final Throwable e) { throw new RedisClientException(e.getMessage(), e); } finally { POOL.close(jedis); } }
Example 18
Source File: ShardedJedisPoolStream.java From RedisDirectory with Apache License 2.0 | 5 votes |
/** * Use transactions to delete index file * * @param fileLengthKey the key using for hash file length * @param fileDataKey the key using for hash file data * @param field the hash field * @param blockSize the index file data block size */ @Override public void deleteFile(String fileLengthKey, String fileDataKey, String field, long blockSize) { ShardedJedis shardedJedis = getShardedJedis(); ShardedJedisPipeline pipelined = shardedJedis.pipelined(); //delete file length pipelined.hdel(fileLengthKey.getBytes(), field.getBytes()); //delete file content for (int i = 0; i < blockSize; i++) { byte[] blockName = getBlockName(field, i); pipelined.hdel(fileDataKey.getBytes(), blockName); } pipelined.sync(); shardedJedis.close(); }
Example 19
Source File: ShardedJedisPoolStream.java From RedisDirectory with Apache License 2.0 | 5 votes |
@Override public List<byte[]> loadFileOnce(String fileDataKey, String fileName, long blockSize) { ShardedJedis shardedJedis = getShardedJedis(); ShardedJedisPipeline pipelined = shardedJedis.pipelined(); List<byte[]> res = new ArrayList<>(); List<Response<byte[]>> temps = new ArrayList<>(); int temp = 0; //如果不分批次sync容易read time out和Java heap space while (temp < blockSize) { Response<byte[]> data = pipelined.hget(fileDataKey.getBytes(), getBlockName(fileName, temp)); temps.add(data); if (temp % Constants.SYNC_COUNT == 0) { pipelined.sync(); res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList())); temps.clear(); pipelined = shardedJedis.pipelined(); } temp++; } try { pipelined.sync(); } catch (JedisConnectionException e) { log.error("pipelined = {}, blockSize = {}!", pipelined.toString(), blockSize); log.error("", e); } finally { shardedJedis.close(); } res.addAll(temps.stream().map(response -> uncompressFilter(response.get())).collect(Collectors.toList())); temps.clear(); return res; }
Example 20
Source File: ShardedJedisPoolStream.java From RedisDirectory with Apache License 2.0 | 5 votes |
/** * Use transactions to delete index file * * @param fileLengthKey the key using for hash file length * @param fileDataKey the key using for hash file data * @param field the hash field * @param blockSize the index file data block size */ @Override public void deleteFile(String fileLengthKey, String fileDataKey, String field, long blockSize) { ShardedJedis shardedJedis = getShardedJedis(); ShardedJedisPipeline pipelined = shardedJedis.pipelined(); //delete file length pipelined.hdel(fileLengthKey.getBytes(), field.getBytes()); //delete file content for (int i = 0; i < blockSize; i++) { byte[] blockName = getBlockName(field, i); pipelined.hdel(fileDataKey.getBytes(), blockName); } pipelined.sync(); shardedJedis.close(); }