Install memcache in Windows
1. Download the stable version of memcache for Windows and decompress it to a disk, for example, in c: \ memcached
2. Enter c: \ memcached \ memcached.exe-D install -- install memcached as a service on the terminal (that is, the CMD command interface) to run normally. Otherwise, the operation fails!
3. Enter c: \ memcached \ memcached.exe-d start -- to start memcached.
In the future, memcached will be used as a Windows service to automatically start every time it is started. In this way, the server has been installed.
Linux installation:
1. Download memcached and libevent and put them in the/tmp directory.
# Cd/tmp
# Wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
# Wget http://www.monkey.org /~ Provos/libevent-1.2.tar.gz
2. Install libevent first:
# Tar zxvf libevent-1.2.tar.gz
# Cd libevent-1.2
#./Configure-Prefix =/usr
# Make
# Make install
3. test whether the libevent is successfully installed:
# Ls-Al/usr/lib | grep libevent
Lrwxrwxrwx 1 Root 21 11 ?? Libevent-1.2.so.1> libevent-1.2.so.1.0.3
-Rwxr-XR-x 1 Root 263546 11 ?? Libevent-1.2.so.1.0.3
-RW-r-1 Root 454156 11 ?? 12 libevent.
-Rwxr-XR-x 1 Root 811 11 ?? Libevent. La
Lrwxrwxrwx 1 Root 21 11 ?? 12 libevent. So-> libevent-1.2.so.1.0.3
4. Install memcached and specify the libevent installation location during installation:
# Cd/tmp
# Tar zxvf memcached-1.2.0.tar.gz
# Cd memcached-1.2.0
#./Configure-with-libevent =/usr
# Make
# Make install
If an error is reported in the middle, check the error information carefully and configure or add the corresponding library or path according to the error information.
After the installation is complete, the memcached will be placed in/usr/local/bin/memcached,
5. Test whether memcached is successfully installed:
# Ls-Al/usr/local/bin/MEM *
-Rwxr-XR-x 1 Root 137986 11 ?? 12 :39/usr/local/bin/memcached
-Rwxr-XR-x 1 Root 140179 11 ?? 12 :39/usr/local/bin/memcached-Debug
Basic settings of memcached:
1. Start the memcache Server:
#/Usr/local/bin/memcached-D-M 10-u root-l 192.168.0.200-P 12000-C 256-P/tmp/memcached. PID
-D option is to start a daemon,
-M indicates the amount of memory allocated to memcache. The unit is mb. The value is 10 MB,
-U is the user who runs memcache. Here it is root,
-L is the IP address of the listener server. If there are multiple IP addresses, the IP address 192.168.0.200 of the server is specified here,
-P is the port used to set memcache listening. Here, 12000 is set, preferably over 1024,
-The C option is the maximum number of concurrent connections running. The default value is 1024. Here, 256 is set based on the load of your server,
-P is the PID file for saving memcache, Which is saved in/tmp/memcached. PID,
2. to end the memcache process, run:
# Kill 'cat/tmp/memcached. Pi'
You can also start multiple daemon processes, but the ports cannot be repeated.
3. Restart Apache and service httpd restart
Java Client Connection Program:
Copy the java_memcached-release_2.0.jar file in the decompressed java_memcached-release_1.6.zip directory to the lib directory of the Java project.
Package utils. cache;
Import java. util. date;
Import com. danga. memcached. memcachedclient;
Import com. danga. memcached. sockiopool;
/**
* Use memcached's cache handler class.
*/
Public class memcached
{
// Create a globally unique instance
Protected static memcachedclient MCC = new memcachedclient ();
Protected static memcached = new memcached ();
// Set the connection pool with the Cache Server
Static {
// Server list and weight
String [] servers = {"127.0.0.1: 11211 "};
Integer [] weights = {3 };
// Obtain the Instance Object of the socke connection pool
Sockiopool = sockiopool. getinstance ();
// Set Server Information
Sockiopool. setservers (servers );
Sockiopool. setweights (weights );
// Set the initial connection count, minimum and maximum connections, and maximum processing time.
Sockiopool. setinitconn (5 );
Sockiopool. setminconn (5 );
Sockiopool. setmaxconn (250 );
Sockiopool. setmaxidle (1000*60*60*6 );
// Set the sleep time of the main thread
Sockiopool. setmaintsleep (30 );
// Set TCP Parameters and connection timeout
Sockiopool. setnagle (false );
Sockiopool. setsocketto (3000 );
Sockiopool. setsocketconnectto (0 );
// Sockiopool. setfailover (bfailover );
// Sockiopool. setalivecheck (balivecheck );
// Initialize the connection pool
Sockiopool. initialize ();
// Compression settings. Data exceeding the specified size (unit: K) will be compressed.
If (memcachedclient = NULL)
{
MCC = new memcachedclient (spoolname );
MCC. setcompressenable (true );
MCC. setcompressthreshold (4096 );
MCC. setprimitiveasstring (true );
}
}
/*
<H3> spring-based configuration: <PRE>
& Lt; bean id = "memcachedservice" class = "com. Ms. memcached. memcachedserviceimpl" & gt;
& Lt; constructor-Arg Index = "0" value = "$ {memcached. Pool. name}"/& gt;
& Lt; constructor-Arg Index = "1" value = "$ {memcached. Pool. servers}"/& gt;
& Lt; constructor-Arg Index = "2" value = "$ {memcached. Pool. initconn}"/& gt;
& Lt; constructor-Arg Index = "3" value = "$ {memcached. Pool. maxconn}"/& gt;
& Lt; constructor-Arg Index = "4" value = "$ {memcached. Pool. minconn}"/& gt;
& Lt; constructor-Arg Index = "5" value = "$ {memcached. Pool. socketto}"/& gt;
& Lt; constructor-Arg Index = "6" value = "$ {memcached. Pool. maintsleep}"/& gt;
& Lt; constructor-Arg Index = "7" value = "$ {memcached. Pool. Nagle}"/& gt;
& Lt; constructor-Arg Index = "8" value = "$ {memcached. Pool. Failover}"/& gt;
& Lt; constructor-Arg Index = "9" value = "$ {memcached. Pool. alivecheck}"/& gt;
& Lt;/bean & gt;
</PRE>
<H3> use Com. Ms. cache. properties to set parameters: <PRE>
Memcached. Pool. Name = MS
Memcached. Pool. servers = 192.168.9.132: 12000,192.168 .9.20.: 12000
Memcached. Pool. initconn = 128
Memcached. Pool. maxconn = 1024
Memcached. Pool. minconn = 20
Memcached. Pool. socketto = 3000
Memcached. Pool. maintsleep = 30
Memcached. Pool. Nagle = false
Memcached. Pool. Failover = true
Memcached. Pool. alivecheck = true
</PRE>
*/
/**
* Protection constructor methods cannot be instantiated!
*/
Protected memcached ()
{
}
/**
* Obtain a unique instance.
*/
Public static memcached getinstance ()
{
Return memcached;
}
/**
* Add a specified value to the cache.
* @ Param key
* @ Param Value
*/
// Add the cache content of the specified key, but do not overwrite the existing content.
Public Boolean add (string key, object value)
{
Return MCC. Add (Key, value );
}
// Expiry expiration time
Public Boolean add (string key, object value, date expiry)
{
Return MCC. Add (Key, value, expiry );
}
// Add or overwrite the cache content of the specified key
Public Boolean set (string key, object value)
{
Return MCC. Set (Key, value );
}
// Lexpiry expiration time
Public Boolean set (string key, object value, long lexpiry)
{
Return MCC. Set (Key, value, new date (lexpiry ));
}
// Obtain the cache content based on the specified key
Public Boolean get (string key)
{
Return MCC. Get (key );
}
// Update the cache content based on the specified key
Public Boolean Replace (string key, object value)
{
Return MCC. Replace (Key, value );
}
// Time specified by lexpiry
Public Boolean Replace (string key, object value, long lexpiry)
{
Return MCC. Replace (Key, value, new date (lexpiry ));
}
// Delete the cache content based on the specified key
Public Boolean Delete (string key, object value)
{
Return MCC. Delete (Key, value );
}
// Delete the cache content after the specified time based on the specified key
Public Boolean Delete (string key, object value, long lexpiry)
{
Return MCC. Delete (Key, value, new date (lexpiry ));
}
// Check whether the current key in the cache exists
Public Boolean exists (string key)
{
Return MCC. exists (key );
}
// Obtain cache content in batches based on a specified batch of keys.
/*
* @ Param skeys specifies a batch of keys.
* @ Return object [ovalue]
*/
Public object [] getmultiarray (string [] skeys) throws serviceexception
{
Return memcachedclient. getmultiarray (skeys );
}
/**
* Obtain cache content in batches based on a specified batch of keys.
*
* @ Param skeys specifies a batch of keys.
* @ Return Map <skey, ovalue>
*/
Public Map <string, Object> getmulti (string [] skeys) throws serviceexception
{
Return memcachedclient. getmulti (skeys );
}
Public static void main (string [] ARGs)
{
Memcached = memcached. getinstance ();
Memcached. Add ("hello", 234 );
System. Out. Print ("Get value:" + memcached. Get ("hello "));
}
}
Then, we can save a variable as operated in the main method, and then retrieve it for viewing. We can see that the add method is called first, and then get is performed, after we run it once, the value 234 has been saved to the memcached cache. After we comment out the red line in the main method, we can see that the value obtained by the get operation is also 234, that is, the data already exists in the cache.
We can operate on basic data. For common pojo, if we want to store the data, for example, let it implement Java. io. serializable interface. Because memcached is a distributed cache server, objects must be serialized for data sharing between multiple servers. Therefore, this interface must be implemented; otherwise, an error will be reported.
Entity
/**
* Obtain the cache ID of the current object
*
* @ Return
*/
Public String getcacheid ()
{
Return getcacheid (this. getclass (), sbreedid );
}
Get
Public breed getbreedbyid (string sbreedid) throws serviceexception
{
Breed breed = (breed) memcachedservice. Get (getcacheid (breed. Class, sbreedid ));
If (breed = NULL)
{
Breed = service. Get ("breed. getbreedbyid", sbreedid );
If (breed! = NULL)
{
Memcachedservice. Set (breed. getbreedid (), breed );
}
}
Return breed;
}
Save
Memcachedservice. Set (Spider. getcacheid (), breed );
Update
Memcachedservice. Replace (Spider. getcacheid (), breed );
Remove
Memcachedservice. Delete (getcacheid (Spider. Class, ibreedid ));
Or
Memcachedservice. Delete (breed. getcacheid ());
Listall
Public list listall () throws serviceexception
{
List breeds = new arraylist ();
List breedids = (list) memcachedservice. Get (getkeybymap ("breed", null ));
If (objectutils. isempty (breedids ))
{
Breeds = service. List ("breed. getallbreed", null );
If (! Objectutils. isempty (breeds ))
{
Breedids = new arraylist ();
For (breed: breeds)
{
Breedids. Add (breed. getbreedid ());
}
Memcachedservice. Set (getkeybymap ("breed", null), breedids );
}
}
Else
{
For (string sbreedid: breedids)
{
Breed breed = getbreedbyid (sbreedid );
If (breed! = NULL)
{
Breeds. Add (breed );
}
}
}
Return breeds;
}