Simple Application of memcached in Java in the Widnows Environment

Source: Internet
Author: User

1. Preparations

1) memcached for Win32 server: http://code.jellycan.com/memcached/

The version used in this article: http://download.csdn.net/detail/supercrsky/5166196

2) memcached Java client: https://github.com/gwhalin/Memcached-Java-Client/downloads

The version used in this article: http://download.csdn.net/detail/supercrsky/5166537

2. Install the server

Put the downloaded memcached.exe file in the specified directory, for example, D: \ memcached. Open CMD and switch to D: \ memcached.

1) Installation

memcached.exe -d install

2) start the service

memcached.exe -d start

3) check whether the installation and startup are successful

Open the windows server and find the memcached Server service.

4) The detailed startup parameters are as follows:

-P listening port-l connection IP address, the default value is local-d start to start memcached service-D restart to restart memcached service-d stop | Shutdown to disable the running memcached service-D install to install memcached service-D uninstall to uninstall memcached service-u run as (only valid when running as root) -MB maximum memory usage, in MB. By default, an error is returned when 64mb-m memory is used up, instead of deleting the maximum number of concurrent connections in item-C. The default value is 1024-f, and the default value is 1.25-n, key + value + flags are 48-H display help by default

3. compile Java Test Cases

1. Place all the downloaded jar files in java_memcached-release_2.6.6.zip into the project lib and add the junit4 support.

2) Java code

Create the JavaBean to save <user>

Package com; import Java. io. serializable; import Java. util. date;/*** the serialization interface must be implemented <memcached requirements> * @ author zdw **/public class user implements serializable {Private Static final long serialversionuid = 1l; private long usid; private string username; private string password; private date createtime; public long getusid () {return usid;} public void setusid (long usid) {This. usid = usid;} Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} Public String GetPassword () {return password;} public void setpassword (string password) {This. password = password;} public date getcreatetime () {return createtime;} public void setcreatetime (date createtime) {This. createtime = createtime ;}}

Test class:

Package com; import Java. util. date; import Org. JUnit. test; import COM. danga. memcached. memcachedclient; import COM. danga. memcached. sockiopool;/*** cache test * @ author zdw **/public class memcachedtest {private memcachedclient MC = new memcachedclient (); static {// specifies the server. The default value is local, the default port is 11211 string [] serverlist = {"127.0.0.1: 11211"}; sockiopool pool = sockiopool. getinstance (); pool. setservers (serverlist); pool. initialize () ;}@ testpublic void testadd () {user = new user (); User. setusername ("likaifu"); User. setpassword ("it"); User. setcreatetime (new date (); MC. set ("user1", user) ;}@ testpublic void testget () {user = (User) MC. get ("user1"); system. out. println (user. getUserName () ;}@ testpublic void testreplace () {user = new user (); User. setusername ("Li Kaifu"); User. setpassword ("kick"); User. setcreatetime (new date (); MC. replace ("user1", user );}}

First test the add method, then execute get; then execute replace, and then execute get method.

We found that the user instance has been saved to the memcached server.

Download demo

Related Article

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.