A simple memcached client demo

Source: Internet
Author: User
In the current speech system, frequent I/O operations are required. In order to cope with the possible high-concurrency access in the future, a cache mechanism is planned.
Among the many caches, ehcache has a good reputation. I have also written an article about the use of ehcache:
Http://lcllcl987.javaeye.com/blog/222693

However, ehcache is not a distributed cache solution.
Therefore, memcached is selected for the primary election:
Http://www.danga.com/memcached/
 
The installation of memcached is handed over to Google.
 

The following is the JAVA Implementation of two memcached clients:
Http://www.whalin.com/memcached/
Http://code.google.com/p/spymemcached/

To
Http://www.whalin.com/memcached/
For example, its documentation is good and I will not talk about it much. Test is as follows:

  1. Package Co. cache;
  2. Import com. danga. memcached. memcachedclient;
  3. Import com. danga. memcached. sockiopool;
  4. Public class mymemcachedclient
  5. {
  6. // Create a static client as most installonly need
  7. // A single instance
  8. Private Static memcachedclient = new memcachedclient ();
  9. // Set up connection pool once at class load
  10. Static
  11. {
  12. // Server list and weights
  13. String [] servers = {"192.168.0.55: 11211 "};
  14. Integer [] weights = {3 };
  15. // Grab an instance of our connection pool
  16. Sockiopool pool = sockiopool. getinstance ();
  17. // Set the servers and the weights
  18. Pool. setservers (servers );
  19. Pool. setweights (weights );
  20. // Set some basic pool settings
  21. // 5 initial, 5 min, and 250 Max Conns
  22. // And set the max idle time for a Conn
  23. // To 6 hours
  24. Pool. setinitconn (5 );
  25. Pool. setminconn (5 );
  26. Pool. setmaxconn (250 );
  27. Pool. setmaxidle (1000*60*60*6 );
  28. // Set the sleep for the Maint thread
  29. // It will wake up every X seconds and
  30. // Maintain the pool size
  31. Pool. setmaintsleep (30 );
  32. // Set some TCP settings
  33. // Disable Nagle
  34. // Set the read timeout to 3 secs
  35. // And don't set a connect timeout
  36. Pool. setnagle (false );
  37. Pool. setsocketto (3000 );
  38. Pool. setsocketconnectto (0 );
  39. // Initialize the connection pool
  40. Pool. initialize ();
  41. // Lets set some compression on for the client
  42. // Compress anything larger than 64 K
  43. Memcachedclient. setcompressenable (true );
  44. Memcachedclient. setcompressthreshold (64*1024 );
  45. }
  46. Public static memcachedclient getmemcachedclient ()
  47. {
  48. Return memcachedclient;
  49. }
  50. // From here on down, you can call any of the client CILS
  51. Public static void examples ()
  52. {
  53. Memcachedclient. Set ("foo", "this is a test string ");
  54. String bar = (string) memcachedclient. Get ("foo ");
  55. }
  56. Public static void main (string [] ARGs)
  57. {
  58. Mymemcachedclient. getmemcachedclient (). Set ("foo", "this is a test string .");
  59. String bar = (string) mymemcachedclient. getmemcachedclient (). Get ("foo ");
  60. System. Out. println (bar );
  61. }
  62. }
  63. Unlike ehcache, memcached seems to only use the LRU algorithm to deal with the situation where the cache is full.

ZZ: http://blog.csdn.net/lcllcl987/archive/2008/11/03/3209987.aspx

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.