@Test public void canRetrieveUnsetKey() { Pipeline p = jedis.pipelined(); Response<String> shouldNotExist = p.get(UUID.randomUUID().toString()); p.sync(); assertNull(shouldNotExist.get()); }
private void showEstimatedVoltages(String caseid) { try (Jedis jedis = jedisPool.getResource()) { auth(jedis); Pipeline p = jedis.pipelined(); String lastEstTimeKey = mkKey(caseid, StormUtils.REDIS.KEYS.LAST_EST_TIME); long esttime = System.currentTimeMillis() - Long.parseLong(jedis.get(lastEstTimeKey)); jedis.set(lastEstTimeKey, System.currentTimeMillis() + ""); String estStartTimeKey = mkKey(caseid, StormUtils.REDIS.KEYS.EST_START_TIME); long estdur = System.currentTimeMillis() - Long.parseLong(jedis.get(estStartTimeKey)); String estVaKey = mkKey(caseid, StormUtils.REDIS.KEYS.VA_EST_HASH); String estVmKey = mkKey(caseid, StormUtils.REDIS.KEYS.VM_EST_HASH); String valfKey = mkKey(caseid, StormUtils.REDIS.KEYS.MEASURE, StormUtils.MEASURE.TYPE.VA); String vmlfKey = mkKey(caseid, StormUtils.REDIS.KEYS.MEASURE, StormUtils.MEASURE.TYPE.VM); Response<Map<String, String>> vaMapResp = p.hgetAll(estVaKey); Response<Map<String, String>> vmMapResp = p.hgetAll(estVmKey); Response<Map<String, String>> valfMapResp = p.hgetAll(valfKey); Response<Map<String, String>> vmlfMapResp = p.hgetAll(vmlfKey); p.sync(); System.out.printf( "\n\nTotal number of buses: %8d with estimation interval %10.4fs and estimation duration %10.4fs", vaMapResp.get().size(), esttime / 1000.0, estdur / 1000); System.out.print("\nMaxdiff with power flow:\nVaMaxDiff(degree)\t\tVmMaxDiff(pu)\t"); List<Double> maxdiff = findMaxDiff(vaMapResp.get(), vmMapResp.get(), valfMapResp.get(), vmlfMapResp.get()); System.out.printf("\n%10.5f\t%10.5f\n", maxdiff.get(0), maxdiff.get(1)); } }
/** * 获取上传地址,每次调用缓存延长24小时 * * @param uuid * @return * @throws IOException */ public static String getUploadFilePath(String uuid) { Jedis jedis = getJedis(); Response<String> pathResponse; try { String key = CacheConstants.Redis_String_PhotoPath + uuid; // if(true)return jedis.get(key); Pipeline p = jedis.pipelined(); pathResponse = p.get(key); p.expire(key, CacheConstants.Redis_String_PhotoPath_Expire); p.sync(); p.close(); // List<Object> results = p.syncAndReturnAll(); String path = pathResponse.get(); if (path != null) return path; List list = nSimpleHibernateDao .createSqlQuery("select path from fp_photo_item where uuid='" + uuid + "'") .list(); if (list != null && list.size() > 0) { path = (String) list.get(0); setUploadFilePath(uuid, path); } else { // 防止暴力查数据库 setUploadFilePath(uuid, ""); } return path; } catch (Exception e) { //// e.printStackTrace(); throw e; } finally { if (jedis != null) jedis.close(); } }
@Test(expected = JedisDataException.class) public void pipelineMultiShoudThrowJedisDataExceptionWhenAlreadyInMulti() { Pipeline pipeline = jedis.pipelined(); pipeline.multi(); pipeline.set("foo", "3"); pipeline.multi(); }
@Test public void multi() { Pipeline p = jedis.pipelined(); p.multi(); Response<Long> r1 = p.hincrBy("a", "f1", -1); Response<Long> r2 = p.hincrBy("a", "f1", -2); Response<List<Object>> r3 = p.exec(); List<Object> result = p.syncAndReturnAll(); assertEquals(new Long(-1), r1.get()); assertEquals(new Long(-3), r2.get()); assertEquals(4, result.size()); assertEquals("OK", result.get(0)); assertEquals("QUEUED", result.get(1)); assertEquals("QUEUED", result.get(2)); // 4th result is a list with the results from the multi @SuppressWarnings("unchecked") List<Object> multiResult = (List<Object>) result.get(3); assertEquals(new Long(-1), multiResult.get(0)); assertEquals(new Long(-3), multiResult.get(1)); assertEquals(new Long(-1), r3.get().get(0)); assertEquals(new Long(-3), r3.get().get(1)); }
@Test public void pipelineWithPubSub() { Pipeline pipelined = jedis.pipelined(); Response<Long> p1 = pipelined.publish("foo", "bar"); Response<Long> p2 = pipelined.publish("foo".getBytes(), "bar".getBytes()); pipelined.sync(); assertEquals(0, p1.get().longValue()); assertEquals(0, p2.get().longValue()); }
@Test(expected = JedisDataException.class) public void pipelineResponseWithinPipeline() { jedis.set("string", "foo"); Pipeline p = jedis.pipelined(); Response<String> string = p.get("string"); string.get(); p.sync(); }
@Test public void testEval() { String script = "return 'success!'"; Pipeline p = jedis.pipelined(); Response<String> result = p.eval(script); p.sync(); assertEquals("success!", result.get()); }
@Test public void pipelineResponseWithData() { jedis.zadd("zset", 1, "foo"); Pipeline p = jedis.pipelined(); Response<Double> score = p.zscore("zset", "foo"); p.sync(); assertNotNull(score.get()); }
@Test public void pipeline() throws UnsupportedEncodingException { Pipeline p = jedis.pipelined(); p.set("foo", "bar"); p.get("foo"); List<Object> results = p.syncAndReturnAll(); assertEquals(2, results.size()); assertEquals("OK", results.get(0)); assertEquals("bar", results.get(1)); }
@Test public void testEvalsha() { String script = "return 'success!'"; String sha1 = jedis.scriptLoad(script); assertTrue(jedis.scriptExists(sha1)); Pipeline p = jedis.pipelined(); Response<String> result = p.evalsha(sha1); p.sync(); assertEquals("success!", result.get()); }
public void pipelineWithoutTransaction() { Jedis jedis = pool.getResource(); try { Pipeline p = jedis.pipelined(); for (int i = 0; i < rowCount; i++) { String key = RandomStringUtils.randomAlphabetic(8); p.set(key, RandomStringUtils.randomNumeric(5)); p.expire(key, 5 * 60); } p.sync(); } catch (Exception e) { pool.returnResource(jedis); } }
/** Execute with a call back action without result in pipeline. */ public void execute(PipelineActionNoResult pipelineAction) throws JedisException { Jedis jedis = null; boolean broken = false; try { jedis = jedisPool.getResource(); Pipeline pipeline = jedis.pipelined(); pipelineAction.action(pipeline); pipeline.sync(); } catch (JedisException e) { broken = handleJedisException(e); throw e; } finally { closeResource(jedis, broken); } }
/** Execute with a call back action with result in pipeline. */ public List<Object> execute(PipelineAction pipelineAction) throws JedisException { Jedis jedis = null; boolean broken = false; try { jedis = jedisPool.getResource(); Pipeline pipeline = jedis.pipelined(); pipelineAction.action(pipeline); return pipeline.syncAndReturnAll(); } catch (JedisException e) { broken = handleJedisException(e); throw e; } finally { closeResource(jedis, broken); } }
@Test public void piplineWithError() { Pipeline p = jedis.pipelined(); p.set("foo", "bar"); Response<Set<String>> error = p.smembers("foo"); Response<String> r = p.get("foo"); p.sync(); try { error.get(); fail(); } catch (JedisDataException e) { // that is fine we should be here } assertEquals(r.get(), "bar"); }
@Aop("redis") public CResult addReply(final String topicId, final TopicReply reply, final int userId) { if (userId < 1) return _fail("请先登录"); if (reply == null || reply.getContent() == null || reply.getContent().trim().isEmpty()) { return _fail("内容不能为空"); } final String cnt = reply.getContent().trim(); final Topic topic = dao.fetch(Topic.class, topicId); // TODO 改成只fetch出type属性 if (topic == null) { return _fail("主题不存在"); } if (topic.isLock()) { return _fail("该帖子已经锁定,不能回复"); } reply.setTopicId(topicId); reply.setUserId(userId); reply.setContent(Toolkit.filteContent(reply.getContent())); reply.setContentId(bigContentService.put(reply.getContent())); reply.setContent(null); dao.insert(reply); // 更新索引 topicSearchService.add(topic); // 更新topic的时间戳 Pipeline pipe = jedis().pipelined(); if (topic.isTop()) { pipe.zadd(RKEY_TOPIC_TOP, reply.getCreateTime().getTime(), topicId); } else { pipe.zadd(RKEY_TOPIC_UPDATE + topic.getType(), reply.getCreateTime().getTime(), topicId); pipe.zadd(RKEY_TOPIC_UPDATE_ALL, reply.getCreateTime().getTime(), topicId); } pipe.zrem(RKEY_TOPIC_NOREPLY, topicId); if (topic.getTags() != null) { for (String tag : topic.getTags()) { pipe.zadd( RKEY_TOPIC_TAG + tag.toLowerCase().trim(), reply.getCreateTime().getTime(), topicId); } } pipe.hset(RKEY_REPLY_LAST, topicId, reply.getId()); pipe.zincrby(RKEY_REPLY_COUNT, 1, topicId); pipe.zincrby(RKEY_USER_SCORE, 10, "" + userId); pipe.sync(); notifyUsers(topic, reply, cnt, userId); return _ok(reply.getId()); }
@Aop("redis") public List<TopicTag> fetchTopTags() { Set<String> names = jedis().zrevrangeByScore(RKEY_TOPIC_TAG_COUNT, Long.MAX_VALUE, 0, 0, 20); List<TopicTag> tags = new ArrayList<>(); List<Response<Double>> tmps = new ArrayList<>(); Pipeline pipe = jedis().pipelined(); for (String name : names) { tmps.add(pipe.zscore(RKEY_TOPIC_TAG_COUNT, name)); tags.add(new TopicTag(name, 0)); } pipe.sync(); Iterator<TopicTag> it = tags.iterator(); for (Response<Double> response : tmps) { it.next().setCount(response.get().intValue()); } return tags; }
/** * sum_daily report. * * <p><br> * url : * report?report_name=sum_daily&appkey=b5558eab134cf306fdcdc3a6746afa25&startDate=20140528&endDate=20140531 * * @param _appkey * @return */ private JSONArray sumDailyReport(String _appkey, Map<String, List<String>> params) { String _startDate = getParameter(params, "startDate"); String _endDate = getParameter(params, "endDate"); int _istartDate = Integer.parseInt(_startDate); int _iendDate = Integer.parseInt(_endDate); String _create_date = DateUtils.getDateRegx(_startDate, _endDate); String key = _appkey + Constants.SPLIT + _create_date; System.out.println(key); JSONArray jsonArray = new JSONArray(); Jedis jedis = null; try { jedis = RedisUtil.getJedis(); Set<String> hkeys = jedis.keys(key); Iterator<String> it = hkeys.iterator(); Pipeline pipeline = jedis.pipelined(); while (it.hasNext()) { JSON map = new JSON(); String hkey = it.next(); String[] cols = hkey.split(Constants.SPLIT); int createDate = Integer.parseInt(cols[1]); if (!(createDate >= _istartDate && createDate <= _iendDate)) { continue; } map.put("appkey", cols[0]); map.put("create_date", cols[1]); Response<List<String>> fieldValueResponse = pipeline.hmget(hkey, "run_num"); pipeline.sync(); List<String> fieldValues = fieldValueResponse.get(); String run_num = fieldValues.get(0); map.put("run_num", run_num); jsonArray.add(map); } } finally { if (jedis != null) { RedisUtil.returnJedis(jedis); } } return jsonArray; }
private void compareClassificationResults( OWLOntology ontology, OWLReasoner reasoner, Jedis resultStore, Jedis idReader) throws Exception { Set<OWLClass> classes = ontology.getClassesInSignature(); Pipeline resultPipeline = resultStore.pipelined(); double classCount = 0; int multiplier = 1; double totalCount = 0; for (OWLClass cl : classes) { classCount++; double classProgress = (classCount / classes.size()) * 100; Set<OWLClass> reasonerSuperclasses = reasoner.getSuperClasses(cl, false).getFlattened(); // add cl itself to S(X) computed by reasoner. That is missing // in its result. reasonerSuperclasses.add(cl); // adding equivalent classes -- they are not considered if asked for superclasses Iterator<OWLClass> iterator = reasoner.getEquivalentClasses(cl).iterator(); while (iterator.hasNext()) reasonerSuperclasses.add(iterator.next()); String classToCheckID = conceptToID(cl.toString(), idReader); List<Response<Double>> responseList = new ArrayList<Response<Double>>(); for (OWLClass scl : reasonerSuperclasses) { String key = conceptToID(scl.toString(), idReader); responseList.add(resultPipeline.zscore(key, classToCheckID)); } resultPipeline.sync(); double hitCount = 0; for (Response<Double> response : responseList) { if (response.get() != null) hitCount++; } totalCount += (hitCount / reasonerSuperclasses.size()); if (classProgress >= (5 * multiplier)) { System.out.println( "% of no. of classes looked at: " + classProgress + "\tProgress %: " + (totalCount / classCount) * 100); multiplier++; } } double progress = totalCount / classes.size(); System.out.println("\nProgress %: " + (progress * 100)); }
public void process(ActionContext ac) throws Throwable { try { if (pool == null) pool = Mvcs.getIoc().get(JedisPool.class); int uid = Toolkit.uid(); if (uid > 0) { try (Jedis jedis = pool.getResource()) { Pipeline pipe = jedis.pipelined(); pipe.setbit(RKEY_ONLINE_DAY + Toolkit.today_yyyyMMdd(), uid, true); pipe.setbit(RKEY_ONLINE_HOUR + Toolkit.today_yyyyMMddHH(), uid, true); pipe.sync(); } } } catch (Exception e) { if (e instanceof JedisConnectionException) { log.debug("jedis is down? ignore error"); } else { log.debug("something wrong? ignore error", e); } } doNext(ac); }
private void rearrangeIndividuals( Set<OWLNamedIndividual> individuals, Jedis resultStore, Jedis resultStore2, Jedis idReader) throws Exception { resultStore2.flushDB(); Pipeline p = resultStore2.pipelined(); double cnt = 0; int multiplier = 1; for (OWLNamedIndividual individual : individuals) { String indID = conceptToID(individual.toString(), idReader); Set<String> classInstances = resultStore.smembers(indID); for (String cl : classInstances) if (!cl.equals(indID)) p.sadd(cl, indID); cnt++; double keyProgress = (cnt / individuals.size()) * 100; if (keyProgress >= (5 * multiplier)) { System.out.println("% of no. of keys rearranged: " + keyProgress); multiplier++; p.sync(); } } p.sync(); }
@Aop("redis") public boolean updateTags(String topicId, @Param("tags") Set<String> tags) { if (Strings.isBlank(topicId) || tags == null) { return false; } Topic topic = dao.fetch(Topic.class, topicId); if (topic == null) return false; Set<String> oldTags = topic.getTags(); if (oldTags == null) oldTags = new HashSet<>(); log.debugf("update from '%s' to '%s'", oldTags, tags); topic.setTags(tags); dao.update(topic, "tags"); Set<String> newTags = new HashSet<>(tags); newTags.removeAll(oldTags); Set<String> removeTags = new HashSet<>(oldTags); ; removeTags.remove(tags); fillTopic(topic, null); Date lastReplyTime = topic.getCreateTime(); if (topic.getLastComment() != null) lastReplyTime = topic.getLastComment().getCreateTime(); Pipeline pipe = jedis().pipelined(); for (String tag : removeTags) { pipe.zrem(RKEY_TOPIC_TAG + tag.toLowerCase().trim(), topic.getId()); pipe.zincrby(RKEY_TOPIC_TAG_COUNT, -1, tag.toLowerCase().trim()); } for (String tag : newTags) { pipe.zadd(RKEY_TOPIC_TAG + tag.toLowerCase().trim(), lastReplyTime.getTime(), topic.getId()); pipe.zincrby(RKEY_TOPIC_TAG_COUNT, 1, tag.toLowerCase().trim()); } pipe.sync(); return true; }
public static void main(String[] args) throws UnknownHostException, IOException { Jedis jedis = new Jedis(hnp.host, hnp.port); jedis.connect(); // jedits.auth("foobared"); jedis.flushAll(); long begin = Calendar.getInstance().getTimeInMillis(); Pipeline p = jedis.pipelined(); for (int n = 0; n <= TOTAL_OPERATIONS; n++) { String key = "foo" + n; p.set(key, "bar" + n); p.get(key); } p.sync(); long elapsed = Calendar.getInstance().getTimeInMillis() - begin; jedis.disconnect(); System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops"); }
@Aop("redis") public CResult add(Topic topic, int userId) { if (userId < 1) { return _fail("请先登录"); } if (Strings.isBlank(topic.getTitle()) || topic.getTitle().length() > 1024 || topic.getTitle().length() < 5) { return _fail("标题长度不合法"); } if (Strings.isBlank(topic.getContent())) { return _fail("内容不合法"); } if (topic.getTags() != null && topic.getTags().size() > 10) { return _fail("最多只能有10个tag"); } if (0 != dao.count(Topic.class, Cnd.where("title", "=", topic.getTitle().trim()))) { return _fail("相同标题已经发过了"); } topic.setTitle(Strings.escapeHtml(topic.getTitle().trim())); topic.setUserId(userId); topic.setTop(false); topic.setTags(new HashSet<String>()); if (topic.getType() == null) topic.setType(TopicType.ask); topic.setContent(Toolkit.filteContent(topic.getContent())); String oldContent = topic.getContent(); topic.setContentId(bigContentService.put(topic.getContent())); topic.setContent(null); dao.insert(topic); try { topic.setContent(oldContent); topicSearchService.add(topic); } catch (Exception e) { } // 如果是ask类型,把帖子加入到 "未回复"列表 Pipeline pipe = jedis().pipelined(); if (TopicType.ask.equals(topic.getType())) { pipe.zadd(RKEY_TOPIC_NOREPLY, System.currentTimeMillis(), topic.getId()); } pipe.zadd(RKEY_TOPIC_UPDATE + topic.getType(), System.currentTimeMillis(), topic.getId()); if (topic.getType() != TopicType.shortit) pipe.zadd(RKEY_TOPIC_UPDATE_ALL, System.currentTimeMillis(), topic.getId()); pipe.zincrby(RKEY_USER_SCORE, 100, "" + userId); pipe.sync(); String replyAuthorName = dao.fetch(User.class, userId).getName(); for (Integer watcherId : globalWatcherIds) { if (watcherId != userId) pushUser( watcherId, "新帖:" + topic.getTitle(), topic.getId(), replyAuthorName, topic.getTitle(), PushService.PUSH_TYPE_REPLY); } updateTopicTypeCount(); return _ok(topic.getId()); }
@Test public void testDiscardInPipeline() { Pipeline pipeline = jedis.pipelined(); pipeline.multi(); pipeline.set("foo", "bar"); Response<String> discard = pipeline.discard(); Response<String> get = pipeline.get("foo"); pipeline.sync(); discard.get(); get.get(); }
@Test public void testEvalKeyAndArg() { String key = "test"; String arg = "3"; String script = "redis.call('INCRBY', KEYS[1], ARGV[1]) redis.call('INCRBY', KEYS[1], ARGV[1])"; Pipeline p = jedis.pipelined(); p.set(key, "0"); Response<String> result0 = p.eval(script, Arrays.asList(key), Arrays.asList(arg)); p.incr(key); Response<String> result1 = p.eval(script, Arrays.asList(key), Arrays.asList(arg)); Response<String> result2 = p.get(key); p.sync(); assertNull(result0.get()); assertNull(result1.get()); assertEquals("13", result2.get()); }
@Test public void pipelineBinarySafeHashCommands() { jedis.hset("key".getBytes(), "f1".getBytes(), "v111".getBytes()); jedis.hset("key".getBytes(), "f22".getBytes(), "v2222".getBytes()); Pipeline p = jedis.pipelined(); Response<Map<byte[], byte[]>> fmap = p.hgetAll("key".getBytes()); Response<Set<byte[]>> fkeys = p.hkeys("key".getBytes()); Response<List<byte[]>> fordered = p.hmget("key".getBytes(), "f22".getBytes(), "f1".getBytes()); Response<List<byte[]>> fvals = p.hvals("key".getBytes()); p.sync(); assertNotNull(fmap.get()); // we have to do these strange contortions because byte[] is not a very // good key // for a java Map. It only works with equality (you need the exact key // object to retrieve // the value) I recommend we switch to using ByteBuffer or something // similar: // http://stackoverflow.com/questions/1058149/using-a-byte-array-as-hashmap-key-java Map<byte[], byte[]> map = fmap.get(); Set<byte[]> mapKeys = map.keySet(); Iterator<byte[]> iterMap = mapKeys.iterator(); byte[] firstMapKey = iterMap.next(); byte[] secondMapKey = iterMap.next(); assertFalse(iterMap.hasNext()); verifyHasBothValues(firstMapKey, secondMapKey, "f1".getBytes(), "f22".getBytes()); byte[] firstMapValue = map.get(firstMapKey); byte[] secondMapValue = map.get(secondMapKey); verifyHasBothValues(firstMapValue, secondMapValue, "v111".getBytes(), "v2222".getBytes()); assertNotNull(fkeys.get()); Iterator<byte[]> iter = fkeys.get().iterator(); byte[] firstKey = iter.next(); byte[] secondKey = iter.next(); assertFalse(iter.hasNext()); verifyHasBothValues(firstKey, secondKey, "f1".getBytes(), "f22".getBytes()); assertNotNull(fordered.get()); assertArrayEquals("v2222".getBytes(), fordered.get().get(0)); assertArrayEquals("v111".getBytes(), fordered.get().get(1)); assertNotNull(fvals.get()); assertEquals(2, fvals.get().size()); byte[] firstValue = fvals.get().get(0); byte[] secondValue = fvals.get().get(1); verifyHasBothValues(firstValue, secondValue, "v111".getBytes(), "v2222".getBytes()); }
@Test public void multiWithSync() { jedis.set("foo", "314"); jedis.set("bar", "foo"); jedis.set("hello", "world"); Pipeline p = jedis.pipelined(); Response<String> r1 = p.get("bar"); p.multi(); Response<String> r2 = p.get("foo"); p.exec(); Response<String> r3 = p.get("hello"); p.sync(); // before multi assertEquals("foo", r1.get()); // It should be readable whether exec's response was built or not assertEquals("314", r2.get()); // after multi assertEquals("world", r3.get()); }
@Override protected void updateStatesToRedis(RedisState redisState, Map<String, String> keyToValue) { Jedis jedis = null; try { jedis = redisState.getJedis(); Pipeline pipeline = jedis.pipelined(); for (Map.Entry<String, String> kvEntry : keyToValue.entrySet()) { String key = kvEntry.getKey(); String value = kvEntry.getValue(); switch (dataType) { case STRING: if (this.expireIntervalSec > 0) { pipeline.setex(key, expireIntervalSec, value); } else { pipeline.set(key, value); } break; case HASH: pipeline.hset(additionalKey, key, value); break; default: throw new IllegalArgumentException("Cannot process such data type: " + dataType); } } // send expire command for hash only once // it expires key itself entirely, so use it with caution if (dataType == RedisDataTypeDescription.RedisDataType.HASH && this.expireIntervalSec > 0) { pipeline.expire(additionalKey, expireIntervalSec); } pipeline.sync(); } finally { if (jedis != null) { redisState.returnJedis(jedis); } } }
public final void persistInfo(String name, UUID uuid, Pipeline jedis) { addToMaps(name, uuid); String json = RedisBungee.getGson().toJson(uuidToNameMap.get(uuid)); jedis.hmset("uuid-cache", ImmutableMap.of(name, json, uuid.toString(), json)); }