|
| 1 | +Project: JRediClients File: PipelinedGetSetBenchmark.java View Source Code Vote up 6 votes |
| 2 | +public static void main(String[] args) throws UnknownHostException, IOException { |
| 3 | + Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort()); |
| 4 | + jedis.connect(); |
| 5 | + jedis.auth("foobared"); |
| 6 | + jedis.flushAll(); |
| 7 | + |
| 8 | + long begin = Calendar.getInstance().getTimeInMillis(); |
| 9 | + |
| 10 | + Pipeline p = jedis.pipelined(); |
| 11 | + for (int n = 0; n <= TOTAL_OPERATIONS; n++) { |
| 12 | + String key = "foo" + n; |
| 13 | + p.set(key, "bar" + n); |
| 14 | + p.get(key); |
| 15 | + } |
| 16 | + p.sync(); |
| 17 | + |
| 18 | + long elapsed = Calendar.getInstance().getTimeInMillis() - begin; |
| 19 | + |
| 20 | + jedis.disconnect(); |
| 21 | + |
| 22 | + System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops"); |
| 23 | +} |
| 24 | + |
| 25 | +Example 2 |
| 26 | +Project: JRediClients File: TransactionCommandsTest.java View Source Code Vote up 6 votes |
| 27 | +@Test |
| 28 | +public void testResetStateWithFullyExecutedTransaction() { |
| 29 | + Jedis jedis2 = new Jedis(jedis.getClient().getHost(), jedis.getClient().getPort()); |
| 30 | + jedis2.auth("foobared"); |
| 31 | + |
| 32 | + Transaction t = jedis2.multi(); |
| 33 | + t.set("mykey", "foo"); |
| 34 | + t.get("mykey"); |
| 35 | + |
| 36 | + List<Object> resp = t.exec(); |
| 37 | + assertNotNull(resp); |
| 38 | + assertEquals(2, resp.size()); |
| 39 | + |
| 40 | + jedis2.resetState(); |
| 41 | + jedis2.close(); |
| 42 | +} |
| 43 | + |
| 44 | +Example 3 |
| 45 | +Project: JRediClients File: GetSetBenchmark.java View Source Code Vote up 6 votes |
| 46 | +public static void main(String[] args) throws UnknownHostException, IOException { |
| 47 | + Jedis jedis = new Jedis(hnp.getHost(), hnp.getPort()); |
| 48 | + jedis.connect(); |
| 49 | + jedis.auth("foobared"); |
| 50 | + jedis.flushAll(); |
| 51 | + |
| 52 | + long begin = Calendar.getInstance().getTimeInMillis(); |
| 53 | + |
| 54 | + for (int n = 0; n <= TOTAL_OPERATIONS; n++) { |
| 55 | + String key = "foo" + n; |
| 56 | + jedis.set(key, "bar" + n); |
| 57 | + jedis.get(key); |
| 58 | + } |
| 59 | + |
| 60 | + long elapsed = Calendar.getInstance().getTimeInMillis() - begin; |
| 61 | + |
| 62 | + jedis.disconnect(); |
| 63 | + |
| 64 | + System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops"); |
| 65 | +} |
| 66 | + |
| 67 | +Example 4 |
| 68 | +Project: JRediClients File: JedisPoolTest.java View Source Code Vote up 6 votes |
| 69 | +@Test |
| 70 | +public void checkJedisIsReusedWhenReturned() { |
| 71 | + |
| 72 | + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort()); |
| 73 | + Jedis jedis = pool.getResource(); |
| 74 | + jedis.auth("foobared"); |
| 75 | + jedis.set("foo", "0"); |
| 76 | + pool.returnResource(jedis); |
| 77 | + |
| 78 | + jedis = pool.getResource(); |
| 79 | + jedis.auth("foobared"); |
| 80 | + jedis.incr("foo"); |
| 81 | + pool.returnResource(jedis); |
| 82 | + pool.destroy(); |
| 83 | + assertTrue(pool.isClosed()); |
| 84 | +} |
| 85 | + |
| 86 | +Example 5 |
| 87 | +Project: JRediClients File: JedisPoolTest.java View Source Code Vote up 6 votes |
| 88 | +@Test |
| 89 | +public void getNumActiveReturnsTheCorrectNumber() { |
| 90 | + JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000); |
| 91 | + Jedis jedis = pool.getResource(); |
| 92 | + jedis.auth("foobared"); |
| 93 | + jedis.set("foo", "bar"); |
| 94 | + assertEquals("bar", jedis.get("foo")); |
| 95 | + |
| 96 | + assertEquals(1, pool.getNumActive()); |
| 97 | + |
| 98 | + Jedis jedis2 = pool.getResource(); |
| 99 | + jedis.auth("foobared"); |
| 100 | + jedis.set("foo", "bar"); |
| 101 | + |
| 102 | + assertEquals(2, pool.getNumActive()); |
| 103 | + |
| 104 | + pool.returnResource(jedis); |
| 105 | + assertEquals(1, pool.getNumActive()); |
| 106 | + |
| 107 | + pool.returnResource(jedis2); |
| 108 | + |
| 109 | + assertEquals(0, pool.getNumActive()); |
| 110 | + |
| 111 | + pool.destroy(); |
| 112 | +} |
| 113 | + |
| 114 | +Example 6 |
| 115 | +Project: xbin-store File: JedisClientSingle.java View Source Code Vote up 5 votes |
| 116 | +private Jedis getResource() { |
| 117 | + Jedis resource = jedisPool.getResource(); |
| 118 | + if (StringUtils.isBlank(password)) { |
| 119 | + return resource; |
| 120 | + } else { |
| 121 | + resource.auth(password); |
| 122 | + return resource; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +Example 7 |
| 127 | +Project: nighthawk File: JaRedisFactory.java View Source Code Vote up 5 votes |
| 128 | +@Override |
| 129 | +public PooledObject<Jedis> makeObject() throws Exception { |
| 130 | + final HostAndPort hostAndPort = this.hostAndPort.get(); |
| 131 | + JaRedis.Builder builder = new JaRedis.Builder(); |
| 132 | + builder |
| 133 | + .host(hostAndPort.getHost()) |
| 134 | + .port(hostAndPort.getPort()) |
| 135 | + .connectionTimeout(connectionTimeout) |
| 136 | + .soTimeout(soTimeout); |
| 137 | + Jedis jedis = builder.build(); |
| 138 | + try { |
| 139 | + jedis.connect(); |
| 140 | + if (null != this.password) { |
| 141 | + jedis.auth(this.password); |
| 142 | + } |
| 143 | + if (database != 0) { |
| 144 | + jedis.select(database); |
| 145 | + } |
| 146 | + if (clientName != null) { |
| 147 | + jedis.clientSetname(clientName); |
| 148 | + } |
| 149 | + } catch (JedisException je) { |
| 150 | + jedis.close(); |
| 151 | + throw je; |
| 152 | + } |
| 153 | + return new DefaultPooledObject<>(jedis); |
| 154 | +} |
| 155 | + |
| 156 | +Example 8 |
| 157 | +Project: JRediClients File: JedisTest.java View Source Code Vote up 5 votes |
| 158 | +@Test |
| 159 | +public void startWithUrlString() { |
| 160 | + Jedis j = new Jedis("localhost", 6380); |
| 161 | + j.auth("foobared"); |
| 162 | + j.select(2); |
| 163 | + j.set("foo", "bar"); |
| 164 | + Jedis jedis = new Jedis("redis://:foobared@localhost:6380/2"); |
| 165 | + assertEquals("PONG", jedis.ping()); |
| 166 | + assertEquals("bar", jedis.get("foo")); |
| 167 | +} |
| 168 | + |
| 169 | +Example 9 |
| 170 | +Project: JRediClients File: TransactionCommandsTest.java View Source Code Vote up 5 votes |
| 171 | +@Before |
| 172 | +public void setUp() throws Exception { |
| 173 | + super.setUp(); |
| 174 | + |
| 175 | + nj = new Jedis(hnp.getHost(), hnp.getPort(), 500); |
| 176 | + nj.connect(); |
| 177 | + nj.auth("foobared"); |
| 178 | + nj.flushAll(); |
| 179 | +} |
| 180 | + |
| 181 | +Example 10 |
| 182 | +Project: JRediClients File: JedisTest.java View Source Code Vote up 5 votes |
| 183 | +@Test |
| 184 | +public void testBitfield() { |
| 185 | + Jedis jedis = new Jedis("redis://localhost:6380"); |
| 186 | + jedis.auth("foobared"); |
| 187 | + jedis.del("mykey"); |
| 188 | + try { |
| 189 | + List<Long> responses = jedis.bitfield("mykey", "INCRBY","i5","100","1", "GET", "u4", "0"); |
| 190 | + assertEquals(1l, responses.get(0).longValue()); |
| 191 | + assertEquals(0l, responses.get(1).longValue()); |
| 192 | + } finally { |
| 193 | + jedis.del("mykey"); |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +Example 11 |
| 198 | +Project: JRediClients File: ClusterCommandsTest.java View Source Code Vote up 5 votes |
| 199 | +@Before |
| 200 | +public void setUp() throws Exception { |
| 201 | + |
| 202 | + node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); |
| 203 | + node1.auth("cluster"); |
| 204 | + node1.flushAll(); |
| 205 | + |
| 206 | + node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); |
| 207 | + node2.auth("cluster"); |
| 208 | + node2.flushAll(); |
| 209 | +} |
| 210 | + |
| 211 | +Example 12 |
| 212 | +Project: JRediClients File: JedisSentinelPoolTest.java View Source Code Vote up 5 votes |
| 213 | +@Test |
| 214 | +public void checkCloseableConnections() throws Exception { |
| 215 | + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); |
| 216 | + |
| 217 | + JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, |
| 218 | + "foobared", 2); |
| 219 | + Jedis jedis = pool.getResource(); |
| 220 | + jedis.auth("foobared"); |
| 221 | + jedis.set("foo", "bar"); |
| 222 | + assertEquals("bar", jedis.get("foo")); |
| 223 | + pool.returnResource(jedis); |
| 224 | + pool.close(); |
| 225 | + assertTrue(pool.isClosed()); |
| 226 | +} |
| 227 | + |
| 228 | +Example 13 |
| 229 | +Project: JRediClients File: JedisTest.java View Source Code Vote up 5 votes |
| 230 | +@Test |
| 231 | +public void startWithUrl() throws URISyntaxException { |
| 232 | + Jedis j = new Jedis("localhost", 6380); |
| 233 | + j.auth("foobared"); |
| 234 | + j.select(2); |
| 235 | + j.set("foo", "bar"); |
| 236 | + Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); |
| 237 | + assertEquals("PONG", jedis.ping()); |
| 238 | + assertEquals("bar", jedis.get("foo")); |
| 239 | +} |
| 240 | + |
| 241 | +Example 14 |
| 242 | +Project: JRediClients File: SSLJedisTest.java View Source Code Vote up 5 votes |
| 243 | +/** |
| 244 | + * Tests opening a default SSL/TLS connection to redis. |
| 245 | + */ |
| 246 | +@Test |
| 247 | +public void connectWithoutShardInfo() { |
| 248 | + // The "rediss" scheme instructs jedis to open a SSL/TLS connection. |
| 249 | + Jedis jedis = new Jedis(URI.create("rediss://localhost:6390")); |
| 250 | + jedis.auth("foobared"); |
| 251 | + jedis.get("foo"); |
| 252 | + jedis.close(); |
| 253 | +} |
| 254 | + |
| 255 | +Example 15 |
| 256 | +Project: JRediClients File: JedisTest.java View Source Code Vote up 5 votes |
| 257 | +@Test(expected = JedisConnectionException.class) |
| 258 | +public void timeoutConnection() throws Exception { |
| 259 | + jedis = new Jedis("localhost", 6379, 15000); |
| 260 | + jedis.auth("foobared"); |
| 261 | + jedis.configSet("timeout", "1"); |
| 262 | + Thread.sleep(2000); |
| 263 | + jedis.hmget("foobar", "foo"); |
| 264 | +} |
| 265 | + |
| 266 | +Example 16 |
| 267 | +Project: JRediClients File: JedisPoolTest.java View Source Code Vote up 5 votes |
| 268 | +@Test(expected = JedisException.class) |
| 269 | +public void checkPoolOverflow() { |
| 270 | + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); |
| 271 | + config.setMaxTotal(1); |
| 272 | + config.setBlockWhenExhausted(false); |
| 273 | + JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort()); |
| 274 | + Jedis jedis = pool.getResource(); |
| 275 | + jedis.auth("foobared"); |
| 276 | + jedis.set("foo", "0"); |
| 277 | + |
| 278 | + Jedis newJedis = pool.getResource(); |
| 279 | + newJedis.auth("foobared"); |
| 280 | + newJedis.incr("foo"); |
| 281 | +} |
| 282 | + |
| 283 | +Example 17 |
| 284 | +Project: JRediClients File: JedisPoolTest.java View Source Code Vote up 5 votes |
| 285 | +@Test |
| 286 | +public void startWithUrlString() { |
| 287 | + Jedis j = new Jedis("localhost", 6380); |
| 288 | + j.auth("foobared"); |
| 289 | + j.select(2); |
| 290 | + j.set("foo", "bar"); |
| 291 | + JedisPool pool = new JedisPool("redis://:foobared@localhost:6380/2"); |
| 292 | + Jedis jedis = pool.getResource(); |
| 293 | + assertEquals("PONG", jedis.ping()); |
| 294 | + assertEquals("bar", jedis.get("foo")); |
| 295 | +} |
| 296 | + |
| 297 | +Example 18 |
| 298 | +Project: JRediClients File: PoolBenchmark.java View Source Code Vote up 5 votes |
| 299 | +public static void main(String[] args) throws Exception { |
| 300 | + Jedis j = new Jedis(hnp.getHost(), hnp.getPort()); |
| 301 | + j.connect(); |
| 302 | + j.auth("foobared"); |
| 303 | + j.flushAll(); |
| 304 | + j.quit(); |
| 305 | + j.disconnect(); |
| 306 | + long t = System.currentTimeMillis(); |
| 307 | + // withoutPool(); |
| 308 | + withPool(); |
| 309 | + long elapsed = System.currentTimeMillis() - t; |
| 310 | + System.out.println(((1000 * 2 * TOTAL_OPERATIONS) / elapsed) + " ops"); |
| 311 | +} |
| 312 | + |
| 313 | +Example 19 |
| 314 | +Project: JRediClients File: JedisClusterTest.java View Source Code Vote up 4 votes |
| 315 | +@Before |
| 316 | +public void setUp() throws InterruptedException { |
| 317 | + node1 = new Jedis(nodeInfo1.getHost(), nodeInfo1.getPort()); |
| 318 | + node1.auth("cluster"); |
| 319 | + node1.flushAll(); |
| 320 | + |
| 321 | + node2 = new Jedis(nodeInfo2.getHost(), nodeInfo2.getPort()); |
| 322 | + node2.auth("cluster"); |
| 323 | + node2.flushAll(); |
| 324 | + |
| 325 | + node3 = new Jedis(nodeInfo3.getHost(), nodeInfo3.getPort()); |
| 326 | + node3.auth("cluster"); |
| 327 | + node3.flushAll(); |
| 328 | + |
| 329 | + node4 = new Jedis(nodeInfo4.getHost(), nodeInfo4.getPort()); |
| 330 | + node4.auth("cluster"); |
| 331 | + node4.flushAll(); |
| 332 | + |
| 333 | + nodeSlave2 = new Jedis(nodeInfoSlave2.getHost(), nodeInfoSlave2.getPort()); |
| 334 | + nodeSlave2.auth("cluster"); |
| 335 | + nodeSlave2.flushAll(); |
| 336 | + // ---- configure cluster |
| 337 | + |
| 338 | + // add nodes to cluster |
| 339 | + node1.clusterMeet(localHost, nodeInfo2.getPort()); |
| 340 | + node1.clusterMeet(localHost, nodeInfo3.getPort()); |
| 341 | + |
| 342 | + // split available slots across the three nodes |
| 343 | + int slotsPerNode = JedisCluster.HASHSLOTS / 3; |
| 344 | + int[] node1Slots = new int[slotsPerNode]; |
| 345 | + int[] node2Slots = new int[slotsPerNode + 1]; |
| 346 | + int[] node3Slots = new int[slotsPerNode]; |
| 347 | + for (int i = 0, slot1 = 0, slot2 = 0, slot3 = 0; i < JedisCluster.HASHSLOTS; i++) { |
| 348 | + if (i < slotsPerNode) { |
| 349 | + node1Slots[slot1++] = i; |
| 350 | + } else if (i > slotsPerNode * 2) { |
| 351 | + node3Slots[slot3++] = i; |
| 352 | + } else { |
| 353 | + node2Slots[slot2++] = i; |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + node1.clusterAddSlots(node1Slots); |
| 358 | + node2.clusterAddSlots(node2Slots); |
| 359 | + node3.clusterAddSlots(node3Slots); |
| 360 | + |
| 361 | + JedisClusterTestUtil.waitForClusterReady(node1, node2, node3); |
| 362 | +} |
| 363 | + |
| 364 | +Example 20 |
| 365 | +Project: JRediClients File: JedisTest.java View Source Code Vote up 4 votes |
| 366 | +@Test |
| 367 | +public void useWithoutConnecting() { |
| 368 | + Jedis jedis = new Jedis("localhost"); |
| 369 | + jedis.auth("foobared"); |
| 370 | + jedis.dbSize(); |
| 371 | +} |
| 372 | + |
0 commit comments