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