一、yum安装
# yum install memcached
启动memcached
# service memcached start
二、tar安装
1、下载安装包
libevent-2.0.21-stable.tar.gz
memcached-1.4.15.tar.gz
2、安装libevent
# tar zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# ./configure --prefix=/usr/local/lib
# make && make install
3、安装memcached
# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
#./configure --prefix=/usr/local/bin/memcached --with-libevent=/usr/local/lib && make && make install
4、配置启动
# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/sbin/ntpdate ntpupdate.tencentyun.com >/dev/null 2>&1 &
/usr/local/agenttools/agent/startagent.sh /usr/local/agenttools/agent /dev/null
#secu-tcs-agent bootstart, install at Tue Apr 29 17:21:38 CST 2014.
/usr/local/sa/agent/secu-tcs-agent-mon-safe.sh > /dev/null 2>&1
/usr/local/bin/memcached -p 12677 -U 0 -d -r -u root -m 2040 -c 1024 -t 4
保存后退出,手动启动服务
# /usr/local/bin/memcached -p 12677 -U 0 -d -r -u root -m 2040 -c 1024 -t 4
java客户端操作
import java.io.IOException;
import java.net.InetSocketAddress;
import net.spy.memcached.MemcachedClient;
public class TestMemcached {
public static void main(String[] args) throws IOException {
MemcachedClient cache = new MemcachedClient(new InetSocketAddress("192.168.100.102", 12677));
for (int i = 1; i < 10; i++) {
cache.set("T0001" + i, 10, "test");
}
Object myObject = cache.get("T00011");
System.out.println("Get object from mem :" + myObject);
}
}