Java Cache (4, memcached)

Source: Internet
Author: User

Overview

Before learning the. NET cache, have already talked about memcached, for its cluster, concurrency and memory recycling issues, have said, here no longer repeat, interested can read this blog: Distributed cache Memcache and Redis. This article is mainly about using the Java language to manipulate memcached.


Memcached's DemoAdd a third-party jar

Java_memcached-release.jar;commons-pool.jar;slf4j-api.jar;slf4j-simple.jar (Demo)

program

Package Com.tgb.testmemcached;import Java.util.date;import Org.junit.before;import org.junit.test;import Com.danga.memcached.memcachedclient;import Com.danga.memcached.sockiopool;public class TestMemcachedClient { Sockiopool Sockiopool = Sockiopool.getinstance (); Memcachedclient memcacheclient = new Memcachedclient (), @Beforepublic void init () {string[] servers = {" 192.168.24.131:11211 "};integer[" weights ={3};sockiopool.setservers (servers); sockiopool.setweights (weights); Sockiopool.setinitconn (5); Sockiopool.setminconn (5); sockiopool.setmaxconn; Sockiopool.setmaxidle (1000*60*60* 6); Sockiopool.setmaintsleep (+); Sockiopool.setnagle (false); Sockiopool.setsocketto (3000); Sockiopool.setsocketconnectto (0); Sockiopool.initialize ();//memcacheclient.setcompressenable (true);// Memcacheclient.setcompressthreshold (64*1024);} @Testpublic void Testadd () {//puts data into the cache Memcacheclient.set ("Test1", "test1"),//puts data into the cache and sets the expiration time of date = new Date ( 2000000); Memcacheclient.set ("Test2", "test2", date); System.out.println ("AddSuccess ");} @Testpublic void Testget () {String strValue1 = memcacheclient.get ("Test1"). ToString (); String strValue2 = Memcacheclient.get ("Test2"). ToString (); System.out.println (strValue1 + "" + StrValue2);} @Testpublic void Testdelete () {Boolean flag = Memcacheclient.delete ("Test1"); SYSTEM.OUT.PRINTLN (flag); String strValue1 = Memcacheclient.get ("Test1"). ToString (); System.out.println (strValue1);} @Testpublic void TestAdd2 () {//does not encode the key Memcacheclient.setsanitizekeys (false); Memcacheclient.add ("[email  Protected] "," Aoyama "); String strName = Memcacheclient.get ("[email protected]"). ToString (); System.out.println (StrName);}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Cache (4, 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.