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
- Package demo;
- Import java. util. date;
- Import com. danga. memcached. memcachedclient;
- Import com. danga. memcached. sockiopool;
- Public class test {
- Protected static memcachedclient MCC = new memcachedclient ();
- Static {
- String [] servers = {"127.0.0.1: 11211 "};
- Integer [] weights = {3 };
- // Create an instance object sockiopool
- Sockiopool pool = sockiopool. getinstance ();
- // Set the servers and the weights
- // Set memcached Server
- Pool. setservers (servers );
- Pool. setweights (weights );
- // Set some basic pool settings
- // 5 initial, 5 min, and 250 Max Conns
- // And set the max idle time for a Conn
- // To 6 hours
- Pool. setinitconn (5 );
- Pool. setminconn (5 );
- Pool. setmaxconn (250 );
- Pool. setmaxidle (1000*60*60*6 );
- // Set the sleep for the Maint thread
- // It will wake up every X seconds and
- // Maintain the pool size
- Pool. setmaintsleep (30 );
- // The TCP rule is that the local machine will wait for the remote host before sending a packet
- // The confirmation message of the last sent packet is sent. This method can disable the socket cache,
- // Send the package as soon as it is ready;
- Pool. setnagle (false );
- // Timeout control after connection Establishment
- Pool. setsocketto (3000 );
- // Timeout Control During connection Establishment
- Pool. setsocketconnectto (0 );
- // Initialize the connection pool
- // Initialize some values and establish a connection with the memcachedserver segment
- Pool. initialize ();
- // Lets set some compression on for the client
- // Compress anything larger than 64 K
- MCC. setcompressenable (true );
- MCC. setcompressthreshold (64*1024 );
- }
- Public static void bucket cache (){
- // 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.
- // 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.
- MCC. Set ("test", "this is a test string", new date (10000); // expire in 10 seconds
- }
- Public static void output (){
- // Value from the cache
- String value = (string) MCC. Get ("test ");
- System. Out. println (value );
- }
- Public static void main (string [] ARGs ){
- Bucket cache ();
- Output ();
- }
- }
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