redis.clients.util.SafeEncoder Java Examples
The following examples show how to use
redis.clients.util.SafeEncoder.
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: JRedisCache.java From springJredisCache with Apache License 2.0 | 6 votes |
/** * 取消订阅的channel * * @param patterns */ @Override public void punsubscribe(String... patterns) { Jedis jedis = null; try { jedis = jedisPool.getResource(); final byte[][] ps = new byte[patterns.length][]; for (int i = 0; i < ps.length; i++) { ps[i] = SafeEncoder.encode(patterns[i]); } jRedisBinaryPubSub.punsubscribe(ps); } catch (Exception ex) { coverException(ex, jedisPool, jedis); } finally { if (jedis != null && jedis.isConnected()) { jedisPool.returnResource(jedis); if (LOGGER.isDebugEnabled()) { LOGGER.debug("close redis connection-{" + jedis.toString() + "}"); } } } }
Example #2
Source File: HyperLogLogCommandsTest.java From cachecloud with Apache License 2.0 | 6 votes |
@Test public void pfcountBinary() { byte[] bHll = SafeEncoder.encode("hll"); byte[] bFoo = SafeEncoder.encode("foo"); byte[] bBar = SafeEncoder.encode("bar"); byte[] bZap = SafeEncoder.encode("zap"); long status = jedis.pfadd(bHll, bFoo, bBar, bZap); assertEquals(1, status); status = jedis.pfadd(bHll, bZap, bZap, bZap); assertEquals(0, status); status = jedis.pfadd(bHll, bFoo, bBar); assertEquals(0, status); status = jedis.pfcount(bHll); assertEquals(3, status); }
Example #3
Source File: SingleRedis.java From ns4_frame with Apache License 2.0 | 6 votes |
@Override public void lpushWithExpired(String queueName, byte[] data, int second) throws MQRedisException { Jedis jedis = this.jedisPool.getResource(); try { String sha1 = jedis.scriptLoad("local retCode = redis.call('LPUSH', KEYS[1], KEYS[2]);\n redis.call('EXPIRE', KEYS[1], KEYS[3]);\n return retCode"); jedis.evalsha(SafeEncoder.encode(sha1), 3, SafeEncoder.encode(queueName), data, SafeEncoder.encode(String.valueOf(second))); } catch (Exception e) { commandLog.error("mq lpushWithExpired error", e); throw new MQRedisException(e); } finally { if (jedis != null) { jedis.close(); } } }
Example #4
Source File: BuilderFactory.java From cachecloud with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings("unchecked") public Set<String> build(Object data) { if (null == data) { return null; } List<byte[]> l = (List<byte[]>) data; final Set<String> result = new LinkedHashSet<String>(l.size()); for (final byte[] barray : l) { if (barray == null) { result.add(null); } else { result.add(SafeEncoder.encode(barray)); } } return result; }
Example #5
Source File: RedisBatchCommand.java From jeesuite-libs with Apache License 2.0 | 6 votes |
/** * 指定组批量写入对象 * @param groupName 缓存组 * @param keyValueMap * @return */ public static boolean setObjectsWithGroup(String groupName,Map<String, Object> keyValueMap){ if(keyValueMap == null || keyValueMap.isEmpty())return false; byte[][] keysValues = new byte[keyValueMap.size() * 2][]; int index = 0; for (String key : keyValueMap.keySet()) { if(keyValueMap.get(key) == null)continue; keysValues[index++] = SafeEncoder.encode(key); keysValues[index++] = SerializeUtils.serialize(keyValueMap.get(key)); } try { if(JedisProviderFactory.isCluster(groupName)){ return JedisProviderFactory.getMultiKeyBinaryJedisClusterCommands(groupName).mset(keysValues).equals(RESP_OK); }else{ return JedisProviderFactory.getMultiKeyBinaryCommands(groupName).mset(keysValues).equals(RESP_OK); } } finally { JedisProviderFactory.getJedisProvider(groupName).release(); } }
Example #6
Source File: RedisClient.java From apollo with GNU General Public License v2.0 | 6 votes |
public Set<String> hKeys(String key) throws Exception { Jedis jedis = null; try { jedis = this.jedisPool.getResource(); Set<byte[]> hkeys = jedis.hkeys(SafeEncoder.encode(key)); logger.info("hkeys key:" + key); if (CollectionUtils.isEmpty(hkeys)) { return new HashSet<String>(1); } else { Set<String> keys = new HashSet<String>(hkeys.size()); for (byte[] bb : hkeys) { keys.add(SafeEncoder.encode(bb)); } return keys; } } catch (Exception e) { logger.error(e.getMessage(), e); this.jedisPool.returnBrokenResource(jedis); throw e; } finally { if (jedis != null) { this.jedisPool.returnResource(jedis); } } }
Example #7
Source File: RedisHashMap.java From jeesuite-libs with Apache License 2.0 | 6 votes |
/** * 设置hash缓存 * * @param datas * @return */ public <T> boolean set(Map<String, T> datas) { if(datas == null || datas.isEmpty())return false; Map<byte[], byte[]> newDatas = new HashMap<>(); Set<String> keySet = datas.keySet(); for (String key : keySet) { if(datas.get(key) == null)continue; newDatas.put(SafeEncoder.encode(key), valueSerialize(datas.get(key))); } boolean result = false; try { if (isCluster(groupName)) { result = getBinaryJedisClusterCommands(groupName).hmset(keyBytes, newDatas).equals(RESP_OK); } else { result = getBinaryJedisCommands(groupName).hmset(keyBytes, newDatas).equals(RESP_OK); } //设置超时时间 if(result)setExpireIfNot(expireTime); return result; } finally { getJedisProvider(groupName).release(); } }
Example #8
Source File: RedisClient.java From apollo with GNU General Public License v2.0 | 6 votes |
public Object lpop(String key, Class<?> cls) throws Exception { byte[] data = null; Jedis jedis = null; try { jedis = this.jedisPool.getResource(); // long begin = System.currentTimeMillis(); data = jedis.lpop(SafeEncoder.encode(key)); // long end = System.currentTimeMillis(); // LOG.info("getValueFromCache spends: " + (end - begin) + " millionseconds."); } catch (Exception e) { // do jedis.quit() and jedis.disconnect() this.jedisPool.returnBrokenResource(jedis); throw e; } finally { if (jedis != null) { this.jedisPool.returnResource(jedis); } } return this.jsonDeserialize(data, cls); }
Example #9
Source File: RedisClient.java From apollo with GNU General Public License v2.0 | 6 votes |
public boolean hdel(String key, String field) throws Exception { Jedis jedis = null; try { jedis = this.jedisPool.getResource(); long value = jedis.hdel(SafeEncoder.encode(key), SafeEncoder.encode(field)); logger.info("hget key:" + key + ", field:" + field); return value == 1; } catch (Exception e) { logger.error(e.getMessage(), e); this.jedisPool.returnBrokenResource(jedis); throw e; } finally { if (jedis != null) { this.jedisPool.returnResource(jedis); } } }
Example #10
Source File: RedisBatchCommand.java From azeroth with Apache License 2.0 | 6 votes |
/** * 指定组批量写入对象 * @param groupName 缓存组 * @param keyValueMap * @return */ public static boolean setObjectsWithGroup(String groupName, Map<String, Object> keyValueMap) { if (keyValueMap == null || keyValueMap.isEmpty()) { return false; } byte[][] keysValues = new byte[keyValueMap.size() * 2][]; int index = 0; for (String key : keyValueMap.keySet()) { if (keyValueMap.get(key) == null) { continue; } keysValues[index++] = SafeEncoder.encode(key); keysValues[index++] = SerializeUtils.serialize(keyValueMap.get(key)); } try { if (JedisProviderFactory.isCluster(groupName)) { return JedisProviderFactory.getMultiKeyBinaryJedisClusterCommands(groupName) .mset(keysValues).equals(RESP_OK); } else { return JedisProviderFactory.getMultiKeyBinaryCommands(groupName).mset(keysValues) .equals(RESP_OK); } } finally { JedisProviderFactory.getJedisProvider(groupName).release(); } }
Example #11
Source File: JedisClusterCommand.java From cachecloud with Apache License 2.0 | 6 votes |
public T run(int keyCount, String... keys) { if (keys == null || keys.length == 0) { throw new JedisClusterException("No way to dispatch this command to Redis Cluster."); } // For multiple keys, only execute if they all share the // same connection slot. if (keys.length > 1) { int slot = JedisClusterCRC16.getSlot(keys[0]); for (int i = 1; i < keyCount; i++) { int nextSlot = JedisClusterCRC16.getSlot(keys[i]); if (slot != nextSlot) { throw new JedisClusterException("No way to dispatch this command to Redis Cluster " + "because keys have different slots."); } } } return runWithRetries(SafeEncoder.encode(keys[0]), this.redirections, false, false); }
Example #12
Source File: EnhancedJedisCluster.java From super-cloudops with Apache License 2.0 | 6 votes |
@Override public ScanResult<String> scan(final String cursor, final ScanParams params) { String matchPattern = null; if (params == null || (matchPattern = scanMatch(params)) == null || matchPattern.isEmpty()) { throw new IllegalArgumentException( getClass().getSimpleName() + " only supports SCAN commands with non-empty MATCH patterns"); } if (JedisClusterHashTagUtil.isClusterCompliantMatchPattern(matchPattern)) { return new EnhancedJedisClusterCommand<ScanResult<String>>(connectionHandler, maxAttempts) { @Override public ScanResult<String> doExecute(Jedis connection) { return connection.scan(cursor, params); } }.runBinary(SafeEncoder.encode(matchPattern)); } else { throw new IllegalArgumentException(getClass().getSimpleName() + " only supports SCAN commands with MATCH patterns containing hash-tags ( curly-brackets enclosed strings )"); } }
Example #13
Source File: EnhancedJedisCluster.java From super-cloudops with Apache License 2.0 | 6 votes |
@Override public ScanResult<byte[]> scan(final byte[] cursor, final ScanParams params) { String matchPattern = null; if (params == null || (matchPattern = scanMatch(params)) == null || matchPattern.isEmpty()) { throw new IllegalArgumentException( BinaryJedisCluster.class.getSimpleName() + " only supports SCAN commands with non-empty MATCH patterns"); } if (JedisClusterHashTagUtil.isClusterCompliantMatchPattern(matchPattern)) { return new EnhancedJedisClusterCommand<ScanResult<byte[]>>(connectionHandler, maxAttempts) { @Override public ScanResult<byte[]> doExecute(Jedis connection) { return connection.scan(cursor, params); } }.runBinary(SafeEncoder.encode(matchPattern)); } else { throw new IllegalArgumentException(BinaryJedisCluster.class.getSimpleName() + " only supports SCAN commands with MATCH patterns containing hash-tags ( curly-brackets enclosed strings )"); } }
Example #14
Source File: RedisHashMap.java From azeroth with Apache License 2.0 | 6 votes |
/** * 设置hash缓存 * * @param datas * @return */ public <T> boolean set(Map<String, T> datas) { if (datas == null || datas.isEmpty()) { return false; } Map<byte[], byte[]> newDatas = new HashMap<>(); Set<String> keySet = datas.keySet(); for (String key : keySet) { if (datas.get(key) == null) { continue; } newDatas.put(SafeEncoder.encode(key), valueSerialize(datas.get(key))); } boolean result = false; try { if (isCluster(groupName)) { result = getBinaryJedisClusterCommands(groupName).hmset(key, newDatas) .equals(RESP_OK); } else { result = getBinaryJedisCommands(groupName).hmset(key, newDatas).equals(RESP_OK); } //设置超时时间 if (result) { setExpireIfNot(expireTime); } return result; } finally { getJedisProvider(groupName).release(); } }
Example #15
Source File: BuilderFactory.java From cachecloud with Apache License 2.0 | 5 votes |
@Override @SuppressWarnings("unchecked") public Set<Tuple> build(Object data) { if (null == data) { return null; } List<byte[]> l = (List<byte[]>) data; final Set<Tuple> result = new LinkedHashSet<Tuple>(l.size()); Iterator<byte[]> iterator = l.iterator(); while (iterator.hasNext()) { result.add(new Tuple(SafeEncoder.encode(iterator.next()), Double.valueOf(SafeEncoder .encode(iterator.next())))); } return result; }
Example #16
Source File: Client.java From cachecloud with Apache License 2.0 | 5 votes |
@Override public void mset(final String... keysvalues) { final byte[][] bkeysvalues = new byte[keysvalues.length][]; for (int i = 0; i < keysvalues.length; i++) { bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); } mset(bkeysvalues); }
Example #17
Source File: MQTemplatesTest.java From ns4_frame with Apache License 2.0 | 5 votes |
@Test public void testClusterRedisCustom() throws Exception { MQTemplate template = MQTemplates.clusterRedisCustom().setHostAndPorts("127.0.0.1:7000;127.0.0.1:7001;127.0.0.1:7002").build(); String queue = "SingleRedisCustum"; template.send(queue, SafeEncoder.encode("你好")); Message message = template.receive("SingleRedisCustum"); String result = new String(message.getBody(), Charset.forName("UTF-8")); Assert.assertEquals("你好", result); System.out.println(result); }
Example #18
Source File: Client.java From cachecloud with Apache License 2.0 | 5 votes |
public void cluster(final String subcommand, final int... args) { final byte[][] arg = new byte[args.length + 1][]; for (int i = 1; i < arg.length; i++) { arg[i] = toByteArray(args[i - 1]); } arg[0] = SafeEncoder.encode(subcommand); cluster(arg); }
Example #19
Source File: Client.java From cachecloud with Apache License 2.0 | 5 votes |
@Override public void brpop(final String[] args) { final byte[][] bargs = new byte[args.length][]; for (int i = 0; i < bargs.length; i++) { bargs[i] = SafeEncoder.encode(args[i]); } brpop(bargs); }
Example #20
Source File: RedisSortSet.java From azeroth with Apache License 2.0 | 5 votes |
public long removeByScore(long min, long max) { try { long result = 0; byte[] start = SafeEncoder.encode(String.valueOf(min)); byte[] end = SafeEncoder.encode(String.valueOf(max)); if (isCluster(groupName)) { result = getBinaryJedisClusterCommands(groupName).zremrangeByScore(key, start, end); } else { result = getBinaryJedisCommands(groupName).zremrangeByScore(key, start, end); } return result; } finally { getJedisProvider(groupName).release(); } }
Example #21
Source File: SortedSetCommandsTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void zscore() { jedis.zadd("foo", 1d, "a"); jedis.zadd("foo", 10d, "b"); jedis.zadd("foo", 0.1d, "c"); jedis.zadd("foo", 2d, "a"); Double score = jedis.zscore("foo", "b"); assertEquals((Double) 10d, score); score = jedis.zscore("foo", "c"); assertEquals((Double) 0.1d, score); score = jedis.zscore("foo", "s"); assertNull(score); // Binary jedis.zadd(bfoo, 1d, ba); jedis.zadd(bfoo, 10d, bb); jedis.zadd(bfoo, 0.1d, bc); jedis.zadd(bfoo, 2d, ba); Double bscore = jedis.zscore(bfoo, bb); assertEquals((Double) 10d, bscore); bscore = jedis.zscore(bfoo, bc); assertEquals((Double) 0.1d, bscore); bscore = jedis.zscore(bfoo, SafeEncoder.encode("s")); assertNull(bscore); }
Example #22
Source File: AllKindOfValuesCommandsTest.java From cachecloud with Apache License 2.0 | 5 votes |
@Test public void echo() { String result = jedis.echo("hello world"); assertEquals("hello world", result); // Binary byte[] bresult = jedis.echo(SafeEncoder.encode("hello world")); assertArrayEquals(SafeEncoder.encode("hello world"), bresult); }
Example #23
Source File: ClusterRedis.java From ns4_frame with Apache License 2.0 | 5 votes |
@Override public void lpush(String queueName, byte[] data) throws MQRedisException { try { this.jc.lpush(SafeEncoder.encode(queueName), data); } catch (Exception e) { commandLog.error("mq lpush error", e); throw new MQRedisException(e); } }
Example #24
Source File: Client.java From cachecloud with Apache License 2.0 | 5 votes |
@Override public void msetnx(final String... keysvalues) { final byte[][] bkeysvalues = new byte[keysvalues.length][]; for (int i = 0; i < keysvalues.length; i++) { bkeysvalues[i] = SafeEncoder.encode(keysvalues[i]); } msetnx(bkeysvalues); }
Example #25
Source File: PipelineCluster.java From cachecloud with Apache License 2.0 | 5 votes |
public List<byte[]> blpopBytes(final String arg) { final byte[] keyByte = SafeEncoder.encode(arg); return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxRedirections) { public List<byte[]> execute(Jedis connection) { return connection.blpop(keyByte); } }.runBinary(keyByte); }
Example #26
Source File: ProtocolTest.java From cachecloud with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") @Test public void multiBulkReply() { InputStream is = new ByteArrayInputStream( "*4\r\n$3\r\nfoo\r\n$3\r\nbar\r\n$5\r\nHello\r\n$5\r\nWorld\r\n".getBytes()); List<byte[]> response = (List<byte[]>) Protocol.read(new RedisInputStream(is)); List<byte[]> expected = new ArrayList<byte[]>(); expected.add(SafeEncoder.encode("foo")); expected.add(SafeEncoder.encode("bar")); expected.add(SafeEncoder.encode("Hello")); expected.add(SafeEncoder.encode("World")); assertEquals(expected, response); }
Example #27
Source File: PipelineCluster.java From cachecloud with Apache License 2.0 | 5 votes |
public Set<byte[]> zrevrangeByScoreBytes(final String key, final double max, final double min) { final byte[] keyByte = SafeEncoder.encode(key); return new JedisClusterCommand<Set<byte[]>>(connectionHandler, maxRedirections) { public Set<byte[]> execute(Jedis connection) { return connection.zrevrangeByScore(SafeEncoder.encode(key), max, min); } }.runBinary(keyByte); }
Example #28
Source File: PipelineCluster.java From cachecloud with Apache License 2.0 | 5 votes |
public List<byte[]> sortBytes(final String key, final SortingParams sortingParameters) { final byte[] keyByte = SafeEncoder.encode(key); return new JedisClusterCommand<List<byte[]>>(connectionHandler, maxRedirections) { public List<byte[]> execute(Jedis connection) { return connection.sort(keyByte, sortingParameters); } }.runBinary(keyByte); }
Example #29
Source File: Jedis.java From cachecloud with Apache License 2.0 | 5 votes |
@Override public ScanResult<String> scan(final String cursor, final ScanParams params) { checkIsInMultiOrPipeline(); client.scan(cursor, params); List<Object> result = client.getObjectMultiBulkReply(); String newcursor = new String((byte[]) result.get(0)); List<String> results = new ArrayList<String>(); List<byte[]> rawResults = (List<byte[]>) result.get(1); for (byte[] bs : rawResults) { results.add(SafeEncoder.encode(bs)); } return new ScanResult<String>(newcursor, results); }
Example #30
Source File: RedisClient.java From apollo with GNU General Public License v2.0 | 5 votes |
/** * value set<br> * The string can't be longer than 1073741824 bytes (1 GB). * * @param key * @param value * @param expiration * * @return false if redis did not execute the option */ public boolean set(String key, Object value, Integer expiration) throws Exception { String result = ""; Jedis jedis = null; try { jedis = this.jedisPool.getResource(); long begin = System.currentTimeMillis(); if (expiration > 0) { result = jedis.setex(SafeEncoder.encode(key), expiration, serialize(value)); } else { result = jedis.set(SafeEncoder.encode(key), serialize(value)); } long end = System.currentTimeMillis(); logger.info("set key:" + key + ", spends: " + (end - begin) + "ms"); return "OK".equalsIgnoreCase(result); } catch (Exception e) { logger.error(e.getMessage(), e); this.jedisPool.returnBrokenResource(jedis); throw e; } finally { if (jedis != null) { this.jedisPool.returnResource(jedis); } } }