Package net.spy.memcached

Examples of net.spy.memcached.MemcachedClient


 
  public SpyMemcachedCacheProvider(Properties p) {
    super(p);
   
    try {
      client = new MemcachedClient(AddrUtil.getAddresses(super.getUrlsProperty()));
      timeout = (super.getRequestTimeoutInSecondsProperty() > 0)?super.getRequestTimeoutInSecondsProperty():60;
      spyMemcachedCache = new SpyMemcachedCache(this, client);
    } catch (Exception ex) {
      log.error("Error initiating memcached client: " + ex.getMessage());
    }
View Full Code Here


      new ConnectionFactoryBuilder();
  private String servers;

  @Override
  public Object getObject() throws Exception {
    return new MemcachedClient(connectionFactoryBuilder.build(),
        AddrUtil.getAddresses(servers));
  }
View Full Code Here

      CompatibilityCacheFactory.killCacheFactories(cacheFactory);
   }

   public void testLoadingAndStoringEventsMemcached() throws InterruptedException, ExecutionException, TimeoutException {
      Cache<String, String> embedded = cacheFactory.getEmbeddedCache();
      MemcachedClient remote = cacheFactory.getMemcachedClient();

      TestCacheListener l = new TestCacheListener();
      embedded.addListener(l);

      assertTrue(l.created.isEmpty());
      assertTrue(l.removed.isEmpty());
      assertTrue(l.modified.isEmpty());
      assertTrue(l.visited.isEmpty());

      Future<Boolean> future1 = remote.add("k", 0, "v");
      assertTrue(future1.get(60, TimeUnit.SECONDS));

      assertEquals(1, l.createdCounter);
      assertEquals("v", l.created.get("k"));
      assertTrue(l.removed.isEmpty());
      assertTrue(l.modified.isEmpty());
      assertTrue(l.visited.isEmpty());

      Future<Boolean> future2 = remote.set("key", 0, "value");
      assertTrue(future2.get(60, TimeUnit.SECONDS));

      assertEquals(2, l.createdCounter);
      assertTrue(l.removed.isEmpty());
      assertTrue(l.modified.isEmpty());
      assertTrue(l.visited.isEmpty());

      Future<Boolean> future3 = remote.set("key", 0, "modifiedValue");
      assertTrue(future3.get(60, TimeUnit.SECONDS));

      assertEquals(2, l.createdCounter);
      assertTrue(l.removed.isEmpty());
      assertEquals(1, l.modifiedCounter);
      assertEquals("modifiedValue", l.modified.get("key"));
      assertTrue(l.visited.isEmpty());

      Future<Boolean> future4 = remote.replace("k", 0, "replacedValue");
      assertTrue(future4.get(60, TimeUnit.SECONDS));

      assertEquals(2, l.createdCounter);
      assertTrue(l.removed.isEmpty());
      assertEquals(2, l.modifiedCounter);
      assertEquals("replacedValue", l.modified.get("k"));
      assertTrue(l.visited.isEmpty());

      //resetting so don't have to type "== 2" etc. all over again
      l.reset();

      Future<Boolean> future5 = remote.delete("key");
      assertTrue(future5.get(60, TimeUnit.SECONDS));

      assertTrue(l.created.isEmpty());
      assertEquals(1, l.removedCounter);
      assertEquals("modifiedValue", l.removed.get("key"));
      assertTrue(l.modified.isEmpty());

      l.reset();

      String value = (String) remote.get("k");
      assertTrue(l.created.isEmpty());
      assertTrue(l.removed.isEmpty());
      assertTrue(l.modified.isEmpty());
      assertEquals(1, l.visitedCounter);
      assertEquals("replacedValue", l.visited.get("k"));
View Full Code Here

    @Override
    protected MemcachedClient createMemcachedClient() throws IOException {
        int port = getPort(randomInt(cluster().size()-1));
        logger.info("  --> Testing Thrift on port [{}]", port);
        return new MemcachedClient(AddrUtil.getAddresses("localhost:" + port));
    }
View Full Code Here

    @Override
    protected MemcachedClient createMemcachedClient() throws IOException {
        int port = getPort(randomInt(cluster().size()-1));
        logger.info("  --> Testing Thrift on port [{}]", port);
        return new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses("localhost:" + port));
    }
View Full Code Here

    public MemcachedClient getMemcacheClient(HazelcastInstance instance) throws IOException {
        final LinkedList<InetSocketAddress> addresses = new LinkedList<InetSocketAddress>();
        addresses.add(instance.getCluster().getLocalMember().getInetSocketAddress());
        final ConnectionFactory factory = new ConnectionFactoryBuilder().setOpTimeout(60 * 60 * 60).setDaemon(true).setFailureMode(FailureMode.Retry).build();
        return new MemcachedClient(factory, addresses);
    }
View Full Code Here

    }

    @Test
    public void testMemcacheSimple() throws IOException, ExecutionException, InterruptedException {
        final HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
        MemcachedClient client = getMemcacheClient(instance);
        try {
            for (int i = 0; i < 100; i++) {
                final OperationFuture<Boolean> future = client.set(String.valueOf(i), 0, i);
                assertEquals(Boolean.TRUE, future.get());
            }
            for (int i = 0; i < 100; i++) {
                assertEquals(i, client.get(String.valueOf(i)));
            }
            for (int i = 0; i < 100; i++) {
                final OperationFuture<Boolean> future = client.add(String.valueOf(i), 0, i * 100);
                assertEquals(Boolean.FALSE, future.get());
            }
            for (int i = 0; i < 100; i++) {
                assertEquals(i, client.get(String.valueOf(i)));
            }
            for (int i = 100; i < 200; i++) {
                final OperationFuture<Boolean> future = client.add(String.valueOf(i), 0, i);
                assertEquals(Boolean.TRUE, future.get());
            }
            for (int i = 0; i < 200; i++) {
                assertEquals(i, client.get(String.valueOf(i)));
            }
            for (int i = 0; i < 200; i++) {
                final OperationFuture<Boolean> future = client.replace(String.valueOf(i), 0, i * 10);
                assertEquals(Boolean.TRUE, future.get());
            }
            for (int i = 0; i < 200; i++) {
                assertEquals(i * 10, client.get(String.valueOf(i)));
            }
            for (int i = 200; i < 400; i++) {
                final OperationFuture<Boolean> future = client.replace(String.valueOf(i), 0, i);
                assertEquals(Boolean.FALSE, future.get());
            }
            for (int i = 200; i < 400; i++) {
                assertEquals(null, client.get(String.valueOf(i)));
            }
            for (int i = 100; i < 200; i++) {
                final OperationFuture<Boolean> future = client.delete(String.valueOf(i));
                assertEquals(Boolean.TRUE, future.get());
            }
            for (int i = 100; i < 200; i++) {
                assertEquals(null, client.get(String.valueOf(100)));
            }
            for (int i = 100; i < 200; i++) {
                final OperationFuture<Boolean> future = client.delete(String.valueOf(i));
                assertEquals(Boolean.FALSE, future.get());
            }

            final LinkedList<String> keys = new LinkedList<String>();
            for (int i = 0; i < 100; i++) {
                keys.add(String.valueOf(i));
            }
            final Map<String, Object> bulk = client.getBulk(keys);
            for (int i = 0; i < 100; i++) {
                assertEquals(i * 10, bulk.get(String.valueOf(i)));
            }
            // STATS
            final Map<String, String> stats = client.getStats().get(instance.getCluster().getLocalMember().getInetSocketAddress());
            assertEquals("700", stats.get("cmd_set"));
            assertEquals("1000", stats.get("cmd_get"));
            assertEquals("700", stats.get("get_hits"));
            assertEquals("300", stats.get("get_misses"));
            assertEquals("100", stats.get("delete_hits"));
            assertEquals("100", stats.get("delete_misses"));
        } finally {
            client.shutdown();
        }
    }
View Full Code Here

    }

    @Test
    public void testMemcacheWithIMap() throws IOException, InterruptedException, ExecutionException {
        final HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
        MemcachedClient client = getMemcacheClient(instance);
        final String prefix = "testMemcacheWithIMap:";
        try {
            final IMap<String, Object> map = instance.getMap("hz_memcache_testMemcacheWithIMap");
            for (int i = 0; i < 100; i++) {
                map.put(String.valueOf(i), String.valueOf(i));
            }
            for (int i = 0; i < 100; i++) {
                assertEquals(String.valueOf(i), client.get(prefix + String.valueOf(i)));
                final OperationFuture<Boolean> future = client.set(prefix + String.valueOf(i), 0, String.valueOf(i * 10));
                future.get();
            }
            for (int i = 0; i < 100; i++) {
                final MemcacheEntry memcacheEntry = (MemcacheEntry) map.get(String.valueOf(i));
                final MemcacheEntry expected = new MemcacheEntry(prefix + String.valueOf(i), String.valueOf(i * 10).getBytes(), 0);
                assertEquals(expected, memcacheEntry);
            }
            final OperationFuture<Boolean> future = client.delete(prefix);
            future.get();
            for (int i = 0; i < 100; i++) {
                assertEquals(null, client.get(prefix + String.valueOf(i)));
            }
        } finally {
            client.shutdown();
        }
    }
View Full Code Here

    }

    @Test
    public void testIncrementAndDecrement() throws IOException, ExecutionException, InterruptedException {
        final HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
        MemcachedClient client = getMemcacheClient(instance);
        try {
            for (int i = 0; i < 100; i++) {
                final OperationFuture<Boolean> future = client.set(String.valueOf(i), 0, i);
                future.get();
            }
            for (int i = 0; i < 100; i++) {
                assertEquals(i * 2, client.incr(String.valueOf(i), i));
            }
            for (int i = 100; i < 120; i++) {
                assertEquals(-1, client.incr(String.valueOf(i), i));
            }
            for (int i = 0; i < 100; i++) {
                assertEquals(i, client.decr(String.valueOf(i), i));
            }
            for (int i = 100; i < 130; i++) {
                assertEquals(-1, client.decr(String.valueOf(i), i));
            }
            for (int i = 0; i < 100; i++) {
                assertEquals(i, client.get(String.valueOf(i)));
            }
            final Map<String, String> stats = client.getStats().get(instance.getCluster().getLocalMember().getInetSocketAddress());
            assertEquals("100", stats.get("cmd_set"));
            assertEquals("100", stats.get("cmd_get"));
            assertEquals("100", stats.get("incr_hits"));
            assertEquals("20", stats.get("incr_misses"));
            assertEquals("100", stats.get("decr_hits"));
            assertEquals("30", stats.get("decr_misses"));
        } finally {
            client.shutdown();
        }
    }
View Full Code Here

    }

    @Test
    public void testMemcacheAppendPrepend() throws IOException, ExecutionException, InterruptedException {
        final HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
        MemcachedClient client = getMemcacheClient(instance);
        try {
            for (int i = 0; i < 100; i++) {
                final OperationFuture<Boolean> future = client.set(String.valueOf(i), 0, String.valueOf(i));
                future.get();
            }
            for (int i = 0; i < 100; i++) {
                final OperationFuture<Boolean> future = client.append(0, String.valueOf(i), "append");
                assertEquals(Boolean.TRUE, future.get());
            }
            for (int i = 0; i < 100; i++) {
                final OperationFuture<Boolean> future = client.prepend(0, String.valueOf(i), "prepend");
                assertEquals(Boolean.TRUE, future.get());
            }
            for (int i = 1; i < 100; i++) {
                assertEquals("prepend" + String.valueOf(i) + "append", client.get(String.valueOf(i)));
            }
        } finally {
            client.shutdown();
        }
    }
View Full Code Here

TOP

Related Classes of net.spy.memcached.MemcachedClient

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.