public void testOperation() throws InterruptedException, TimeoutException, MemcachedException, IOException { assertEquals(0, getSize()); client.set(keyName, 0, "12345"); assertEquals(1, getSize()); assertEquals("12345", client.get(keyName)); assertEquals(0, getSize()); client.set(keyName + "_" + System.currentTimeMillis(), 0, "12345"); assertEquals(1, getSize()); assertEquals("12345", client.get(keyName + "_" + System.currentTimeMillis())); log.info("push 10000 items"); long start = System.currentTimeMillis(); // 测试顺序写入10000个数据,再按顺序取出来,是否正确 for (int i = 0; i < 10000; i++) { client.set(keyName, 0, "value_" + i); } log.info("push 10000 items :" + (System.currentTimeMillis() - start) + " ms"); assertEquals(10000, getSize()); log.info("poll 10000 items"); start = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { assertEquals("value_" + i, client.get(keyName)); } log.info("poll 10000 items :" + (System.currentTimeMillis() - start) + " ms"); assertEquals(0, getSize()); log.info("开始测试权限状态"); authorization(); log.info("开始测试多线程操作"); mutiThreadWrite(); mutiThreadGet(); assertEquals(0, getSize()); }
public void testIssue150() throws Exception { memcachedClient.set("a", 0, 1); try { memcachedClient.incr("a", 1); fail(); } catch (MemcachedException e) { // assertEquals("cannot increment or decrement non-numeric value",e.getMessage()); } memcachedClient.set("a", 0, "1"); assertEquals(3, memcachedClient.incr("a", 2)); }
public void testSanitizeKey() throws Exception { memcachedClient.setSanitizeKeys(true); String key = "The string 眉@foo-bar"; assertTrue(memcachedClient.add(key, 0, 0)); assertEquals(0, memcachedClient.get(key)); assertTrue(memcachedClient.replace(key, 0, 1)); assertEquals(1, memcachedClient.get(key, 2000)); assertTrue(memcachedClient.set(key, 0, 2)); assertEquals((Integer) 2, memcachedClient.get(key, 2000, new IntegerTranscoder())); assertTrue(memcachedClient.set(key, 0, "xmemcached", new StringTranscoder())); assertTrue(memcachedClient.append(key, " great")); assertTrue(memcachedClient.prepend(key, "hello ")); assertEquals("hello xmemcached great", memcachedClient.get(key)); // test bulk get List<String> keys = new ArrayList<String>(); for (int i = 0; i < 100; i++) { memcachedClient.add(key + i, 0, i); keys.add(key + i); } Map<String, Integer> result = memcachedClient.get(keys, 5000); for (int i = 0; i < 100; i++) { assertEquals((Integer) i, result.get(key + i)); } for (int i = 0; i < 100; i++) { assertTrue(memcachedClient.delete(key + i)); assertNull(memcachedClient.get(key + i)); } // test cas memcachedClient.set(key, 0, 1); memcachedClient.cas( key, new CASOperation<Integer>() { public int getMaxTries() { return 1; } public Integer getNewValue(long currentCAS, Integer currentValue) { return currentValue + 1; } }); assertEquals((Integer) 2, memcachedClient.get(key, 2000, new IntegerTranscoder())); }
public void _testOperationEncodeTimeout() throws Exception { memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); long writeMessageCount = memcachedClient.getConnector().getStatistics().getWriteMessageCount(); CountDownLatch latch = new CountDownLatch(1); Command errorCommand = null; if (memcachedClient.getProtocol() == Protocol.Text) { errorCommand = new MockEncodeTimeoutTextGetOneCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, 1000); } else { errorCommand = new MockEncodeTimeoutBinaryGetCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false, 1000); } memcachedClient.getConnector().send(errorCommand); // Force write thread to encode command errorCommand.setIoBuffer(null); // wait 100 milliseconds,the operation will be timeout if (!latch.await(100, TimeUnit.MILLISECONDS)) { errorCommand.cancel(); } Thread.sleep(1000); // It is not written to channel,because it is canceled. assertEquals( writeMessageCount, memcachedClient.getConnector().getStatistics().getWriteMessageCount()); // It works assertEquals("dennis", memcachedClient.get("name")); }
public static void main(String[] args) throws IOException { MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211")); MemcachedClient memcachedClient = builder.build(); try { memcachedClient.set("hello", 0, "Hello,xmemcached"); String value = memcachedClient.get("hello"); System.out.println("hello=" + value); memcachedClient.delete("hello"); value = memcachedClient.get("hello"); System.out.println("hello=" + value); } catch (MemcachedException e) { System.err.println("MemcachedClient operation fail"); e.printStackTrace(); } catch (TimeoutException e) { System.err.println("MemcachedClient operation timeout"); e.printStackTrace(); } catch (InterruptedException e) { // ignore } try { // close memcached client memcachedClient.shutdown(); } catch (IOException e) { System.err.println("Shutdown MemcachedClient fail"); e.printStackTrace(); } }
public void testRemoveAndAddServer() throws Exception { String servers = properties.getProperty("test.memcached.servers"); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); memcachedClient.removeServer(servers); synchronized (this) { while (memcachedClient.getAvaliableServers().size() > 0) { wait(1000); } } assertEquals(0, memcachedClient.getAvaliableServers().size()); try { memcachedClient.get("name"); fail(); } catch (MemcachedException e) { assertEquals("There is no available connection at this moment", e.getMessage()); } memcachedClient.addServer(properties.getProperty("test.memcached.servers")); synchronized (this) { while (memcachedClient.getAvaliableServers().size() < AddrUtil.getAddresses(servers).size()) { wait(1000); } } Thread.sleep(5000); assertEquals("dennis", memcachedClient.get("name")); }
public boolean set(String key, Object value, int exp) { try { return memcachedClient.set(key, exp, value); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public boolean set(String key, Object value) { try { return memcachedClient.set(key, Integer.MAX_VALUE, value); // TODO } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
public void testStats() throws Exception { assertTrue(memcachedClient.getStats().size() > 0); System.out.println(memcachedClient.getStats()); memcachedClient.set("a", 0, 1); assertTrue(memcachedClient.getStatsByItem("items").size() > 0); System.out.println(memcachedClient.getStatsByItem("items")); }
public void _testErrorCommand() throws Exception { Command nonexisCmd = new Command() { @Override public boolean decode(MemcachedTCPSession session, ByteBuffer buffer) { return decodeError(ByteUtils.nextLine(buffer)); } @Override public void encode() { ioBuffer = IoBuffer.wrap(ByteBuffer.wrap("test\r\n".getBytes())); } }; nonexisCmd.setKey("test"); nonexisCmd.setLatch(new CountDownLatch(1)); memcachedClient.getConnector().send(nonexisCmd); // this.memcachedClient.flushAll(); nonexisCmd.getLatch().await(); assertNotNull(nonexisCmd.getException()); assertEquals( "Nonexist command,check your memcached version please.", nonexisCmd.getException().getMessage()); assertTrue(nonexisCmd.getException() instanceof UnknownCommandException); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); }
public void testWeightedServers() throws Exception { // shutdown current client memcachedClient.shutdown(); MemcachedClientBuilder builder = createWeightedBuilder(); builder.getConfiguration().setStatisticsServer(true); memcachedClient = builder.build(); memcachedClient.flushAll(5000); Map<InetSocketAddress, Map<String, String>> oldStats = memcachedClient.getStats(); for (int i = 0; i < 100; i++) { assertTrue(memcachedClient.set(String.valueOf(i), 0, i)); } for (int i = 0; i < 100; i++) { assertEquals(i, memcachedClient.get(String.valueOf(i))); } List<InetSocketAddress> addressList = AddrUtil.getAddresses(properties.getProperty("test.memcached.servers")); Map<InetSocketAddress, Map<String, String>> newStats = memcachedClient.getStats(); for (InetSocketAddress address : addressList) { int oldSets = Integer.parseInt(oldStats.get(address).get("cmd_set")); int newSets = Integer.parseInt(newStats.get(address).get("cmd_set")); System.out.println("sets:" + (newSets - oldSets)); int oldGets = Integer.parseInt(oldStats.get(address).get("cmd_get")); int newGets = Integer.parseInt(newStats.get(address).get("cmd_get")); System.out.println("gets:" + (newGets - oldGets)); } }
public static boolean setXCache( net.rubyeye.xmemcached.MemcachedClient mcc, String key, int exp, Object value) { try { return mcc.set(key, exp, value); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void updateOnlineState(long uid) { try { memcachedClient.set(MemcachedKeyGenerator.genUserOnlineKey(uid), userOnlineExpireTime, true); } catch (Exception e) { log.error(e.getMessage(), e); } }
@Override public void put(Object key, Object value) { try { memcachedClient.set(keyToString(key), expiry, value); } catch (Exception e) { log.warn("Unable to put object to cache", throwableOrMessage(e)); } }
@Override public void setPage(String key, String pageContent, int expires) throws PageCacheException { try { memcachedClient.set(key, expires, pageContent); } catch (Exception e) { throw new PageCacheException(e.getMessage()); } }
public void testDelete() throws Exception { assertTrue(memcachedClient.set("name", 0, "dennis")); assertEquals("dennis", memcachedClient.get("name")); assertTrue(memcachedClient.delete("name")); assertNull(memcachedClient.get("name")); assertFalse(memcachedClient.delete("not_exists")); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); assertTrue(memcachedClient.delete("name")); assertNull(memcachedClient.get("name")); memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); assertTrue(memcachedClient.delete("name", 2000L)); assertNull(memcachedClient.get("name")); // add,replace success assertTrue(memcachedClient.add("name", 0, "zhuang")); assertTrue(memcachedClient.replace("name", 0, "zhuang")); }
@Test public void simpleTest() throws TimeoutException, InterruptedException, MemcachedException { long start = System.currentTimeMillis(); String value = "simpleValue"; Assert.assertTrue(memcachedClient.set(key, 120, value)); Assert.assertEquals(value, memcachedClient.get(key)); // Assert.assertTrue(memcachedClient.touch(key, 120)); Assert.assertTrue(memcachedClient.delete(key)); Assert.assertNull(memcachedClient.get(key)); System.out.println("use time: " + (System.currentTimeMillis() - start)); }
/** * 测试类 * * @param client */ public void test(MemcachedClient client) { try { System.out.println(client.get("1")); if (client.get("1") == null) { client.set("1", 0, "11111111111"); } } catch (TimeoutException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } catch (MemcachedException e) { e.printStackTrace(); } }
public void testOperationDecodeTimeOut() throws Exception { memcachedClient.set("name", 0, "dennis"); assertEquals("dennis", memcachedClient.get("name")); CountDownLatch latch = new CountDownLatch(1); Command errorCommand = null; if (memcachedClient.getProtocol() == Protocol.Text) { errorCommand = new MockDecodeTimeoutTextGetOneCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, 1000); } else { errorCommand = new MockDecodeTimeoutBinaryGetOneCommand( "name", "name".getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false, 1000); } memcachedClient.getConnector().send(errorCommand); // wait 100 milliseconds,the operation will be timeout latch.await(100, TimeUnit.MILLISECONDS); assertNull(errorCommand.getResult()); Thread.sleep(1000); // It works. assertNotNull(errorCommand.getResult()); assertEquals("dennis", memcachedClient.get("name")); }
public void testAutoReconnect() throws Exception { final String key = "name"; memcachedClient.set(key, 0, "dennis"); assertEquals("dennis", memcachedClient.get(key)); CountDownLatch latch = new CountDownLatch(1); int currentServerCount = memcachedClient.getAvaliableServers().size(); MockErrorCommand errorCommand = null; if (memcachedClient.getProtocol() == Protocol.Text) { errorCommand = new MockErrorTextGetOneCommand(key, key.getBytes(), CommandType.GET_ONE, latch); } else { errorCommand = new MockErrorBinaryGetOneCommand( key, key.getBytes(), CommandType.GET_ONE, latch, OpCode.GET, false); } memcachedClient.getConnector().send((Command) errorCommand); latch.await(MemcachedClient.DEFAULT_OP_TIMEOUT, TimeUnit.MILLISECONDS); assertTrue(errorCommand.isDecoded()); // wait for reconnecting Thread.sleep(2000 * 3); assertEquals(currentServerCount, memcachedClient.getAvaliableServers().size()); // It works assertEquals("dennis", memcachedClient.get(key)); }
public void testSet() throws Exception { assertTrue(memcachedClient.set("name", 0, "dennis")); assertEquals("dennis", memcachedClient.get("name", 2000)); assertTrue(memcachedClient.set("name", 1, "zhuang", new StringTranscoder())); assertEquals("zhuang", memcachedClient.get("name", 2000)); Thread.sleep(2000); // expired assertNull(memcachedClient.get("zhuang")); // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.set("", 0, 1); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.set((String) null, 0, 1); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.set("test\r\n", 0, 1); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.set("test test2", 0, 1); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.set(sb.toString(), 0, 1); } }.check(); // Transcoder new TranscoderChecker(mockTranscoder, 2) { @Override public void call() throws Exception { memcachedClient.set("name", 0, "xmemcached", mockTranscoder); assertEquals("xmemcached", memcachedClient.get("name", mockTranscoder)); } }.check(); }
public void testGets() throws Exception { memcachedClient.add("name", 0, "dennis"); GetsResponse<String> getsResponse = memcachedClient.gets("name"); GetsResponse<String> oldGetsResponse = getsResponse; assertEquals("dennis", getsResponse.getValue()); long oldCas = getsResponse.getCas(); getsResponse = memcachedClient.gets("name", 2000, new StringTranscoder()); assertEquals("dennis", getsResponse.getValue()); // check the same assertEquals(oldCas, getsResponse.getCas()); assertEquals(oldGetsResponse, getsResponse); memcachedClient.set("name", 0, "zhuang"); getsResponse = memcachedClient.gets("name", 2000); assertEquals("zhuang", getsResponse.getValue()); assertFalse(oldCas == getsResponse.getCas()); // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.gets(""); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.gets((String) null); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.gets("test\r\n"); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.gets("test test2"); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.gets(sb.toString()); } }.check(); // client is shutdown try { memcachedClient.shutdown(); memcachedClient.gets("name"); fail(); } catch (MemcachedException e) { assertEquals("Xmemcached is stopped", e.getMessage()); } }
public void testAppendPrepend() throws Exception { // append,prepend assertTrue(memcachedClient.set("name", 0, "dennis", new StringTranscoder(), 1000)); assertTrue(memcachedClient.prepend("name", "hello ")); assertEquals("hello dennis", memcachedClient.get("name")); assertTrue(memcachedClient.append("name", " zhuang")); assertEquals("hello dennis zhuang", memcachedClient.get("name")); memcachedClient.delete("name"); assertFalse(memcachedClient.prepend("name", "hello ", 2000)); assertFalse(memcachedClient.append("name", " zhuang", 2000)); // append test // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.append("", 0, 1); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.append((String) null, 0, 1); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.append("test\r\n", 0, 1); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.append("test test2", 0, 1); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.append(sb.toString(), 0, 1); } }.check(); // prepend test // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend("", 0, 1); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend((String) null, 0, 1); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend("test\r\n", 0, 1); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.prepend("test test2", 0, 1); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.prepend(sb.toString(), 0, 1); } }.check(); }
public void testDecr() throws Exception { assertEquals(0, memcachedClient.decr("a", 5)); assertTrue(memcachedClient.set("a", 0, "100")); assertEquals(50, memcachedClient.decr("a", 50)); assertEquals(46, memcachedClient.decr("a", 4)); // test decr with initValue memcachedClient.delete("a"); assertEquals(100, memcachedClient.decr("a", 5, 100)); assertEquals(50, memcachedClient.decr("a", 50)); assertEquals(46, memcachedClient.decr("a", 4)); // test decr with initValue and expire time memcachedClient.delete("a"); assertEquals(1, memcachedClient.decr("a", 5, 1, 1000, 1)); Thread.sleep(2000); assertNull(memcachedClient.get("a")); // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.decr("", 0, 1); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.decr((String) null, 0, 1); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.decr("test\r\n", 0, 1); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.decr("test test2", 0, 1); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.decr(sb.toString(), 0, 1); } }.check(); }
public void testCAS() throws Exception { memcachedClient.add("name", 0, "dennis"); GetsResponse<String> getsResponse = memcachedClient.gets("name"); assertEquals("dennis", getsResponse.getValue()); final CASOperation<String> operation = new CASOperation<String>() { public int getMaxTries() { return 1; } public String getNewValue(long currentCAS, String currentValue) { return "zhuang"; } }; assertTrue(memcachedClient.cas("name", getsResponse, operation)); assertEquals("zhuang", memcachedClient.get("name")); getsResponse = memcachedClient.gets("name"); memcachedClient.set("name", 0, "dennis"); // cas fail assertFalse(memcachedClient.cas("name", 0, "zhuang", getsResponse.getCas())); assertEquals("dennis", memcachedClient.get("name")); // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.cas("", operation); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.cas((String) null, operation); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.cas("test\r\n", operation); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.cas("test test2", operation); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.cas(sb.toString(), operation); } }.check(); }
public void testGet() throws Exception { assertNull(memcachedClient.get("name")); memcachedClient.set("name", 1, "dennis", new StringTranscoder(), 1000); assertEquals("dennis", memcachedClient.get("name", new StringTranscoder())); new TranscoderChecker(mockTranscoder, 1) { @Override public void call() throws Exception { assertEquals("dennis", memcachedClient.get("name", mockTranscoder)); } }.check(); Thread.sleep(2000); // expire assertNull(memcachedClient.get("name")); // blank key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.get(""); } }.check(); // null key new BlankKeyChecker() { @Override public void call() throws Exception { memcachedClient.get((String) null); } }.check(); // invalid key new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.get("test\r\n"); } }.check(); new InValidKeyChecker() { @Override public void call() throws Exception { memcachedClient.get("test test2"); } }.check(); // key is too long new TooLongKeyChecker(memcachedClient) { @Override public void call() throws Exception { int keyLength = memcachedClient.getProtocol() == Protocol.Text ? 256 : 65536; StringBuilder sb = new StringBuilder(); for (int i = 0; i < keyLength; i++) { sb.append(i); } memcachedClient.get(sb.toString()); } }.check(); // client is shutdown try { memcachedClient.shutdown(); memcachedClient.get("name"); fail(); } catch (MemcachedException e) { assertEquals("Xmemcached is stopped", e.getMessage()); } }