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.
- ImportJava. io. IOException;
- ImportJava.net. InetSocketAddress;
- ImportJava. util. concurrent. Future;
- ImportNet. spy. memcached. MemcachedClient;
- Public ClassMemClient {
- Private StaticMemcachedClient client =Null;
- Static{
- Try{
- Client =NewMemcachedClient (NewInetSocketAddress ("127.0.0.1",
- 11211));
- System. out. println ("Connected to server");
- }Catch(IOException e ){
- E. printStackTrace ();
- System. exit (-1);
- }
- }
- PublicFuture AddToMemCache (String key, IntTimeOut, Object val ){
- Future Future = client. set (key, timeOut, val );
- ReturnFuture;
- }
- PublicObject getMemcachedValue (String key ){
- ReturnClient. get (key );
- }
- }
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.
- ImportJava. util. ArrayList;
- ImportJava. util. Random;
- ImportJava. util. concurrent. ExecutionException;
- ImportJava. util. concurrent. Future;
- ImportJava. util. logging. Level;
- ImportJava. util. logging. Logger;
- Public ClassMemClientUser {
- Private StaticArrayList KeyStore = NewArrayList ();
- Private StaticArrayList > Futures = NewArrayList > ();
- Private Static IntCounter =0;
- Public Static VoidMain (String [] args)ThrowsInterruptedException,
- ExecutionException {
- Logger. getLogger ("Net. spy. memcached"). SetLevel (Level. SEVERE );
- FinalMemClient memClient =NewMemClient ();
- For(IntI =0; I <100; I ++ ){
- FinalString key = getRandomKey ();
- FinalString value = getValueFromASource ();
- KeyStore. add (key );
- Future Future = memClient. addToMemCache (key, 10, Value );
- Try{
- Future. get ();
- }Catch(InterruptedException e ){
- E. printStackTrace ();
- }Catch(ExecutionException e ){
- E. printStackTrace ();
- }
- If(Future! =Null){
- Futures. add (future );
- Counter ++;
- }Else
- System. out. println ("Future is null ?? ");
- }
- System. out. println ("Values tried :"+ Counter );
- Counter =0;
- For(FinalString key: keyStore ){
- String val = (String) memClient. getMemcachedValue (key );
- If(Val! =Null)
- Counter ++;
- }
- System. out. println ("Values found :"+ Counter );
- // This ensures the values are expired
- Thread. sleep (10000);
- Counter =0;
- For(FinalString key: keyStore ){
- String val = (String) memClient. getMemcachedValue (key );
- If(Val! =Null)
- Counter ++;
- }
- System. out. println ("Values remaining :"+ Counter );
- }
- Private StaticString getRandomKey (){
- Return "RANDOM_KEY"+NewRandom (). nextInt (99999);
- }
- Private StaticString getValueFromASource (){
- // This function ideally wowould return a value from a database, or an API
- // Call
- Return "RANDOM_VALUE"+NewRandom (). nextInt (99999);
- }
- }
The test results are as follows: