Java client calls memcached

Source: Internet
Author: User
Tags failover

 

Web: http://www.whalin.com/memcached
Latest: java_memcached-release_2.0.1

Operation example:

Java code
  1. Import com. danga. memcached .*;
  2. Import org. Apache. log4j .*;
  3. Public class testmemcached {
  4. Public static void main (string [] ARGs ){
  5. /* Initialize the sockiopool and manage the memcached connection pool */
  6. String [] servers = {"192.168.1.20: 12111 "};
  7. Sockiopool pool = sockiopool. getinstance ();
  8. Pool. setservers (servers );
  9. Pool. setfailover (true );
  10. Pool. setinitconn (10 );
  11. Pool. setminconn (5 );
  12. Pool. setmaxconn (250 );
  13. Pool. setmaintsleep (30 );
  14. Pool. setnagle (false );
  15. Pool. setsocketto (3000 );
  16. Pool. setalivecheck (true );
  17. Pool. initialize ();
  18. /* Create a memcachedclient instance */
  19. Memcachedclient = new memcachedclient ();
  20. For (INT I = 0; I <10; I ++ ){
  21. /* Add the object to the memcached cache */
  22. Boolean success = memcachedclient. Set ("" + I, "Hello! ");
  23. /* Get the object by key value from the memcached cache */
  24. String result = (string) memcachedclient. Get ("" + I );
  25. System. Out. println (string. Format ("set (% d): % s", I, success ));
  26. System. Out. println (string. Format ("Get (% d): % s", I, result ));
  27. }
  28. }
  29. }
Java code
  1. Import com. danga. memcached .*;
  2. Import org. Apache. log4j .*;
  3. Public class testmemcached {
  4. Public static void main (string [] ARGs ){
  5. /* Initialize the sockiopool and manage the memcached connection pool */
  6. String [] servers = {"192.168.1.20: 12111 "};
  7. Sockiopool pool = sockiopool. getinstance ();
  8. Pool. setservers (servers );
  9. Pool. setfailover (true );
  10. Pool. setinitconn (10 );
  11. Pool. setminconn (5 );
  12. Pool. setmaxconn (250 );
  13. Pool. setmaintsleep (30 );
  14. Pool. setnagle (false );
  15. Pool. setsocketto (3000 );
  16. Pool. setalivecheck (true );
  17. Pool. initialize ();
  18. /* Create a memcachedclient instance */
  19. Memcachedclient = new memcachedclient ();
  20. For (INT I = 0; I <10; I ++ ){
  21. /* Add the object to the memcached cache */
  22. Boolean success = memcachedclient. Set ("" + I, "Hello! ");
  23. /* Get the object by key value from the memcached cache */
  24. String result = (string) memcachedclient. Get ("" + I );
  25. System. Out. println (string. Format ("set (% d): % s", I, success ));
  26. System. Out. println (string. Format ("Get (% d): % s", I, result ));
  27. }
  28. }
  29. }

2. spymemcachedClient API: spymemcached Client
Web: http://code.google.com/p/spymemcached/
Latest: memcached-2.1.jar

Operation example:
Use spymemcached to save the object to the cache Java code

  1. Import java.net. inetsocketaddress;
  2. Import java. util. Concurrent. Future;
  3. Import net. Spy. memcached. memcachedclient;
  4. Public class mclient {
  5. Public static void main (string [] ARGs ){
  6. Try {
  7. /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
  8. Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
  9. Future <Boolean> B = NULL;
  10. /* Set the key value, expiration time (in seconds), and object to be cached to memcached */
  11. B = mc. Set ("NEEA: testdaf: ksidno", 900, "someobject ");
  12. If (B. Get (). booleanvalue () = true ){
  13. MC. Shutdown ();
  14. }
  15. }
  16. Catch (exception ex ){
  17. Ex. printstacktrace ();
  18. }
  19. }
  20. }
Java code
  1. Import java.net. inetsocketaddress;
  2. Import java. util. Concurrent. Future;
  3. Import net. Spy. memcached. memcachedclient;
  4. Public class mclient {
  5. Public static void main (string [] ARGs ){
  6. Try {
  7. /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
  8. Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
  9. Future <Boolean> B = NULL;
  10. /* Set the key value, expiration time (in seconds), and object to be cached to memcached */
  11. B = mc. Set ("NEEA: testdaf: ksidno", 900, "someobject ");
  12. If (B. Get (). booleanvalue () = true ){
  13. MC. Shutdown ();
  14. }
  15. }
  16. Catch (exception ex ){
  17. Ex. printstacktrace ();
  18. }
  19. }
  20. }

Use spymemcached to retrieve objects from the cache

Java code
  1. Import java.net. inetsocketaddress;
  2. Import java. util. Concurrent. Future;
  3. Import net. Spy. memcached. memcachedclient;
  4. Public class mclient {
  5. Public static void main (string [] ARGs ){
  6. Try {
  7. /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
  8. Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
  9. /* Query the cache from memcached based on the key value. If no cache exists, null is returned */
  10. Object B = mc. Get ("NEEA: testdaf: ksidno ");
  11. MC. Shutdown ();
  12. }
  13. Catch (exception ex ){
  14. Ex. printstacktrace ();
  15. }
  16. }
  17. }
Java code
  1. Import java.net. inetsocketaddress;
  2. Import java. util. Concurrent. Future;
  3. Import net. Spy. memcached. memcachedclient;
  4. Public class mclient {
  5. Public static void main (string [] ARGs ){
  6. Try {
  7. /* Create a memcachedclient instance and specify the IP address and port number of the memcached Service */
  8. Memcachedclient MC = new memcachedclient (New inetsocketaddress ("192.168.1.20", 12111 ));
  9. /* Query the cache from memcached based on the key value. If no cache exists, null is returned */
  10. Object B = mc. Get ("NEEA: testdaf: ksidno ");
  11. MC. Shutdown ();
  12. }
  13. Catch (exception ex ){
  14. Ex. printstacktrace ();
  15. }
  16. }
  17. }
  3. alisoft-xplatform-asf-cache-2.4This is the locally made memcached client. I have spent some time testing it. It is easy to configure and use, and has detailed Chinese and English documents.
Web: http://code.google.com/p/memcache-client-forjava/
Latest Version: alisoft-xplatform-asf-cache-2.5.2.jar use example memcached. xml ==========================<? XML version = "1.0" encoding = "UTF-8"?>
<Memcached>

<Client name = "mclient0" compressenable = "true" defaultencoding = "UTF-8" socketpool = "pool0">
<Errorhandler> com. moit. xplatform. ASF. cache. memcached. memcachederrorhandler </errorhandler>
</Client>

<Client name = "mclient1" compressenable = "true" defaultencoding = "UTF-8" socketpool = "pool1">
<Errorhandler> com. moit. xplatform. ASF. cache. memcached. memcachederrorhandler </errorhandler>
</Client>

<Client name = "mclient2" compressenable = "true" defaultencoding = "UTF-8" socketpool = "pool2">
<Errorhandler> com. moit. xplatform. ASF. cache. memcached. memcachederrorhandler </errorhandler>
</Client>
<Socketpool name = "pool0" failover = "true" initconn = "5" minconn = "5" maxconn = "250" maintsleep = "0"
Nagle = "false" socketto = "3000" alivecheck = "true">
<Servers> 192.168.3.small: 11211,192.168 .3.small: 11211 </servers>
</Socketpool>

<Socketpool name = "pool1" failover = "true" initconn = "5" minconn = "5" maxconn = "250" maintsleep = "0"
Nagle = "false" socketto = "3000" alivecheck = "true">
<Servers> 192.168.3.small: 11211,192.168 .3.small: 11211 </servers>
</Socketpool>

<Socketpool name = "pool2" failover = "true" initconn = "5" minconn = "5" maxconn = "250" maintsleep = "0"
Nagle = "false" socketto = "3000" alivecheck = "true">
<Servers> 192.168.3.small: 11211,192.168 .3.small: 11211 </servers>
</Socketpool> <cluster name = "cluster1">
<Memcachedclients> mclient1, mclient2 </memcachedclients>
</Cluster> </memcached> // use the containskey method to determine whether the Cache Server exists based on the specified key value.
System. Out. println ("whether the data contains the key =" + cache. containskey ("key "));
If (Cache. containskey ("key "))
{
System. Out. println ("data containing key ");
System. Out. println ("Get key value from Cache Server ");
}
Else
{
System. Out. println ("data without keys ");
System. Out. println ("cache server. If there is no data, retrieve the database data! ");
}

Example:
Static icachemanager <imemcachedcache> manager;
 
/**
* Test memcached.
* @ Return
*/
Public String memcache ()
{
Manager = cacheutil. getcachemanager (imemcachedcache. Class,
Memcachedcachemanager. Class. getname ());
Manager. Start ();
Try
{
Imemcachedcache cache = manager. getcache ("mclient0 ");
// Obtain the cached data based on the key
String A = (string) cache. Get ("key ");
// Use the containskey method to determine whether the Cache Server exists based on the specified key value.
System. Out. println ("whether the data contains the key =" + cache. containskey ("key "));
If (Cache. containskey ("key "))
{
System. Out. println ("data containing key ");
System. Out. println ("Get key value from Cache Server ");
}
Else
{
System. Out. println ("data without keys ");
System. Out. println ("cache server. If there is no data, retrieve the database data! ");
}
// Delete the corresponding cache data on the server based on the key
Cache. Remove ("key ");
// Save data to the server based on the key
Cache. Put ("key", "Hello! ");
// Set an example with an expiration time // 30 minutes later
Calendar calendar = calendar. getinstance (); // current date
Calendar. settime (new date ());
Calendar. Add (calendar. Minute, 30 );//
Cache. Remove ("keytime ");
Cache. Put ("keytime", "expired after 30 minutes", calendar. gettime ());
System. Out. println ("expired after 30 minutes = keytime =" + cache. Get ("keytime "));
System. Out. println ("Cache Server gettime =" + calendar. gettime ());

} Finally {manager. Stop ();} // for JSP usage, see the test. jsp file.
Return "testmempage ";
}

Conclusion: Comparison of Three APIs 
Memcached client for Java: The memcached Java client API launched earlier. It is widely used and runs stably.
Spymemcached: A simple, asynchronous, single-threaded memcached client written in Java. supports asynchronous and single-threaded memcached clients that use concurrent and NiO of Java. The access speed is higher than the former, but the stability is poor. related exceptions such as timeout are often reported during testing. Alisoft-xplatform-asf-cache-2.4 I began to use, and later found that the performance is very poor, especially when the call is frequent, especially prone to problems. I later adopted the first. Now no problem found.
Memcached client for Java has released a new version, which improves performance and runs stably. Therefore, we recommend that you use memcached client for Java.

 

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.