ubuntu及windows上測試memcached服務

來源:互聯網
上載者:User

參考:

http://blog.csdn.net/lizhongkan/article/details/6033370

http://brainfry.in/programming/memcached-java-examples/

關於memcached服務的安裝可以參見:

ubuntu及windows上安裝memcached服務

廢話少說直接上代碼:

先建立一個memcached的串連類,注意填寫正確的memcached伺服器的IP及連接埠

import java.io.IOException;import java.net.InetSocketAddress;import java.util.concurrent.Future;import net.spy.memcached.MemcachedClient;public class MemClient {    private static MemcachedClient client = null;    static {        try {            client = new MemcachedClient(new InetSocketAddress("127.0.0.1",                    11211));            System.out.println("CONNECTED TO SERVER");        } catch (IOException e) {            e.printStackTrace();            System.exit(-1);        }    }    public Future<Boolean> addToMemCache(String key, int timeOut, Object val) {        Future<Boolean> future = client.set(key, timeOut, val);        return future;    }    public Object getMemcachedValue(String key) {        return client.get(key);    }}

再建立一個使用類,基本原理就是隨機產生100對索引值對存入,並檢查是否全部存入,在設定的expire期限以後,讀取看是否存入的資料都失效了

import java.util.ArrayList;import java.util.Random;import java.util.concurrent.ExecutionException;import java.util.concurrent.Future;import java.util.logging.Level;import java.util.logging.Logger;public class MemClientUser {    private static ArrayList<String> keyStore = new ArrayList<String>();    private static ArrayList<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();    private static int counter = 0;    public static void main(String[] args) throws InterruptedException,            ExecutionException {        Logger.getLogger("net.spy.memcached").setLevel(Level.SEVERE);        final MemClient memClient = new MemClient();        for (int i = 0; i < 100; i++) {            final String key = getRandomKey();            final String value = getValueFromASource();            keyStore.add(key);            Future<Boolean> future = memClient.addToMemCache(key, 10, value);            try {                future.get();            } catch (InterruptedException e) {                e.printStackTrace();            } catch (ExecutionException e) {                e.printStackTrace();            }            if (future != null) {                futures.add(future);                counter++;            } else                System.out.println("future is null??");        }        System.out.println("VALUES TRIED: " + counter);        counter = 0;        for (final String key : keyStore) {            String val = (String) memClient.getMemcachedValue(key);            if (val != null)                counter++;        }        System.out.println("VALUES FOUND: " + counter);        // This ensures the the values are expired        Thread.sleep(10000);        counter = 0;        for (final String key : keyStore) {            String val = (String) memClient.getMemcachedValue(key);            if (val != null)                counter++;        }        System.out.println("VALUES REMAINING: " + counter);    }    private static String getRandomKey() {        return "RANDOM_KEY" + new Random().nextInt(99999);    }    private static String getValueFromASource() {        // This function ideally would return a value from a database, or an API        // call        return "RANDOM_VALUE" + new Random().nextInt(99999);    }}

測試結果如下:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.