Simple Application of memcache (for Java) in Windows

Source: Internet
Author: User

The procedure is as follows:

1. Download memcached for Win32:

Http://jehiah.cz/projects/memcached-win32/

Installation and running method:

Http://www.ccvita.com/258.html

 

2. Download The memcache Java support package

Http://www.whalin.com/memcached/#download

 

3. Code

  1. Package demo;
  2. Import java. util. date;
  3. Import com. danga. memcached. memcachedclient;
  4. Import com. danga. memcached. sockiopool;
  5. Public class test {
  6. Protected static memcachedclient MCC = new memcachedclient ();
  7. Static {
  8. String [] servers = {"127.0.0.1: 11211 "};
  9. Integer [] weights = {3 };
  10. // Create an instance object sockiopool
  11. Sockiopool pool = sockiopool. getinstance ();
  12. // Set the servers and the weights
  13. // Set memcached Server
  14. Pool. setservers (servers );
  15. Pool. setweights (weights );
  16. // Set some basic pool settings
  17. // 5 initial, 5 min, and 250 Max Conns
  18. // And set the max idle time for a Conn
  19. // To 6 hours
  20. Pool. setinitconn (5 );
  21. Pool. setminconn (5 );
  22. Pool. setmaxconn (250 );
  23. Pool. setmaxidle (1000*60*60*6 );
  24. // Set the sleep for the Maint thread
  25. // It will wake up every X seconds and
  26. // Maintain the pool size
  27. Pool. setmaintsleep (30 );
  28. // The TCP rule is that the local machine will wait for the remote host before sending a packet
  29. // The confirmation message of the last sent packet is sent. This method can disable the socket cache,
  30. // Send the package as soon as it is ready;
  31. Pool. setnagle (false );
  32. // Timeout control after connection Establishment
  33. Pool. setsocketto (3000 );
  34. // Timeout Control During connection Establishment
  35. Pool. setsocketconnectto (0 );
  36. // Initialize the connection pool
  37. // Initialize some values and establish a connection with the memcachedserver segment
  38. Pool. initialize ();
  39. // Lets set some compression on for the client
  40. // Compress anything larger than 64 K
  41. MCC. setcompressenable (true );
  42. MCC. setcompressthreshold (64*1024 );
  43. }
  44. Public static void bucket cache (){
  45. // Set (Key, value, date), date is an expiration time. If you want this expiration time to take effect, the parameter date in new date (long date) is passed here, it must be a value greater than or equal to 1000.
  46. // Because expiry. gettime ()/1000 is implemented in the source code of Java client, that is to say, if the value is smaller than 1000, it will be 0 after dividing by 1000, that is, it will never expire.
  47. MCC. Set ("test", "this is a test string", new date (10000); // expire in 10 seconds
  48. }
  49. Public static void output (){
  50. // Value from the cache
  51. String value = (string) MCC. Get ("test ");
  52. System. Out. println (value );
  53. }
  54. Public static void main (string [] ARGs ){
  55. Bucket cache ();
  56. Output ();
  57. }
  58. }

Run:

Running output value: this is a test string

Comment out buildcache ();

Run in 10 seconds and the output value is null

 

Note:

1. The program requires log4j. Jar

2. Default memcache port: 11211

 

Other resources:

Http://www.javayou.com/diary/7141

Http://www.javaeye.com/topic/61898

Reference: http://ttitfly.javaeye.com/blog/110495

Related Article

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.