Java development: Share the use of memcached

Source: Internet
Author: User

In project development, some infrequently modified data, we usually choose to use the cache. One way is to memcached.

Windows system, we need to download and install memcached.

Address such as: D:\memcached\memcached.exe

Then, run Cmd.exe as administrator and enter the start command:

In this way, we can create a cache instance in the project:

 Public classMemcachedmanager {//creating Memcachedclient Global Objects    Private StaticMemcachedclient MCC =Newmemcachedclient (); Static {        //Create a server list and its weightsString[] Servers = {"127.0.0.1:11211" }; Integer[] Weights= {3 }; //Create a socket connection pool objectSockiopool pool =sockiopool.getinstance (); //setting up Server informationpool.setservers (servers);        Pool.setweights (weights); Pool.setfailover (true); //set the number of initial connections, minimum and maximum connections, and maximum processing timePool.setinitconn (5); Pool.setminconn (5); Pool.setmaxconn (250); Pool.setmaxidle (1000 * 60 * 60 * 6); //set the main thread sleep timePool.setmaintsleep (30); //setting TCP parameters, connection timeouts, and so onPool.setnagle (false); Pool.setsocketto (3000); Pool.setsocketconnectto (0); Pool.setalivecheck (true); //Initializing the connection poolpool.initialize (); }    /*** Non-parametric construction*/    protectedMemcachedmanager () {}//protected Objects    protected StaticMemcachedmanager instance =NewMemcachedmanager (); /*** Provides a public access method for protected objects*/     Public StaticMemcachedmanager getinstance () {returninstance; }    /*** Add object to cache, form method overload * *@paramKey *@paramValue *@return     */     Public BooleanAdd (String key, Object value) {returnMcc.add (key, value); }     Public BooleanAdd (String key, Object value, Date expiry) {returnMcc.add (key, value, expiry); }     Public BooleanReplace (String key, Object value) {returnmcc.replace (key, value); }     Public BooleanReplace (String key, Object value, Date expiry) {returnmcc.replace (key, value, expiry); }    /*** Gets the object according to the specified keyword*/     PublicObject get (String key) {returnMcc.get (key); }         Public Static voidMain (string[] args) {//Get Memcachedmanager InstancesMemcachedmanager cache =memcachedmanager.getinstance (); //Create test CacheCache.Add ("Test", "Hello"); //Remove Test CacheSystem.out.println (Cache.get ("Test")); //Modifying the test cacheCache.replace ("Test", "hello123"); //Remove Test CacheSystem.out.println (Cache.get ("Test")); }   }

After running this example, the printed results are as follows:

Hello
Hello123

Java development: Share the use of memcached

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.