Test the Memcached service on Ubuntu and Windows

Source: Internet
Author: User
For how to install the memcached service, see install the memcached service idea on Ubuntu and Windows.

For the installation of memcached service, see install memcached service http://www.linuxidc.com/Linux/2012-08/69313.htm on Ubuntu and Windows.

Reference: http://www.linuxidc.com/Linux/2010-11/29573.htm

Put the Code directly:

Create a memcached connection class first. Make sure to enter the correct IP address and port of the memcached server.

  1. ImportJava. io. IOException;
  2. ImportJava.net. InetSocketAddress;
  3. ImportJava. util. concurrent. Future;
  4. ImportNet. spy. memcached. MemcachedClient;
  5. Public ClassMemClient {
  6. Private StaticMemcachedClient client =Null;
  7. Static{
  8. Try{
  9. Client =NewMemcachedClient (NewInetSocketAddress ("127.0.0.1",
  10. 11211));
  11. System. out. println ("Connected to server");
  12. }Catch(IOException e ){
  13. E. printStackTrace ();
  14. System. exit (-1);
  15. }
  16. }
  17. PublicFuture AddToMemCache (String key, IntTimeOut, Object val ){
  18. Future Future = client. set (key, timeOut, val );
  19. ReturnFuture;
  20. }
  21. PublicObject getMemcachedValue (String key ){
  22. ReturnClient. get (key );
  23. }
  24. }

Create another usage class. The basic principle is to randomly generate 100 pairs of key-value pairs and check whether all of them are saved. After the specified expire period, read the data to check whether the stored data is invalid.

  1. ImportJava. util. ArrayList;
  2. ImportJava. util. Random;
  3. ImportJava. util. concurrent. ExecutionException;
  4. ImportJava. util. concurrent. Future;
  5. ImportJava. util. logging. Level;
  6. ImportJava. util. logging. Logger;
  7. Public ClassMemClientUser {
  8. Private StaticArrayList KeyStore = NewArrayList ();
  9. Private StaticArrayList > Futures = NewArrayList > ();
  10. Private Static IntCounter =0;
  11. Public Static VoidMain (String [] args)ThrowsInterruptedException,
  12. ExecutionException {
  13. Logger. getLogger ("Net. spy. memcached"). SetLevel (Level. SEVERE );
  14. FinalMemClient memClient =NewMemClient ();
  15. For(IntI =0; I <100; I ++ ){
  16. FinalString key = getRandomKey ();
  17. FinalString value = getValueFromASource ();
  18. KeyStore. add (key );
  19. Future Future = memClient. addToMemCache (key, 10, Value );
  20. Try{
  21. Future. get ();
  22. }Catch(InterruptedException e ){
  23. E. printStackTrace ();
  24. }Catch(ExecutionException e ){
  25. E. printStackTrace ();
  26. }
  27. If(Future! =Null){
  28. Futures. add (future );
  29. Counter ++;
  30. }Else
  31. System. out. println ("Future is null ?? ");
  32. }
  33. System. out. println ("Values tried :"+ Counter );
  34. Counter =0;
  35. For(FinalString key: keyStore ){
  36. String val = (String) memClient. getMemcachedValue (key );
  37. If(Val! =Null)
  38. Counter ++;
  39. }
  40. System. out. println ("Values found :"+ Counter );
  41. // This ensures the values are expired
  42. Thread. sleep (10000);
  43. Counter =0;
  44. For(FinalString key: keyStore ){
  45. String val = (String) memClient. getMemcachedValue (key );
  46. If(Val! =Null)
  47. Counter ++;
  48. }
  49. System. out. println ("Values remaining :"+ Counter );
  50. }
  51. Private StaticString getRandomKey (){
  52. Return "RANDOM_KEY"+NewRandom (). nextInt (99999);
  53. }
  54. Private StaticString getValueFromASource (){
  55. // This function ideally wowould return a value from a database, or an API
  56. // Call
  57. Return "RANDOM_VALUE"+NewRandom (). nextInt (99999);
  58. }
  59. }

The test results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.