Memcached 学习笔记(一)安装 & helloworld

Memcached 学习笔记(一)安装 & helloworld

1. 安装Memcached

首先是下载 memcached 了,目前最新版本是 1.1.12,直接从官方网站即可下载到 memcached-1.1.12.tar.gz。除此之外,memcached 用到了 libevent,我下载的是 libevent-1.1a.tar.gz

接下来是分别将 libevent-1.1a.tar.gz 和 memcached-1.1.12.tar.gz 解开包、编译、安装:

# tar -xzf libevent-1.1a.tar.gz
# cd libevent-1.1a
# ./configure -prefix=/usr
# make
# make install
# cd ..
# tar -xzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure -prefix=/usr
# make 
# make install

安装完成之后,memcached 应该在 /usr/bin/memcached。

然后启动memcached:

 ./memcached  -d -m 128 -u root -p 11211 -c 256

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48

-h 显示帮助

然后使用下列命令来查看是否启动成功:

ps aux | grep memcached

2. 测试

首先下载memcache的jar包,导入工程中,然后运行如下代码:

import java.util.Date;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;


public class Main {

	 //  创建全局的唯一实例
    protected static MemCachedClient mcc = new MemCachedClient();
   
    protected static Main memCached = new Main();
   
    // 设置与缓存服务器的连接池
    static {
        // 服务器列表和其权重
        String[] servers = { "192.168.159.128:11211" };
        Integer[] weights = { 3 };

        // 获取socke连接池的实例对象
        SockIOPool pool = SockIOPool.getInstance();

        // 设置服务器信息
        pool.setServers( servers );
        pool.setWeights( weights );

        // 设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn( 5 );
        pool.setMinConn( 5 );
        pool.setMaxConn( 250 );
        pool.setMaxIdle( 1000 * 60 * 60 * 6 );

        // 设置主线程的睡眠时间
        pool.setMaintSleep( 30 );

        // 设置TCP的参数,连接超时等
        pool.setNagle( false );
        pool.setSocketTO( 3000 );
        pool.setSocketConnectTO( 0 );

        // 初始化连接池
        pool.initialize();

        // 压缩设置,超过指定大小(单位为K)的数据都会被压缩
        mcc.setCompressEnable( true );
        mcc.setCompressThreshold( 64 * 1024 );
    }
   
    /**
     * 保护型构造方法,不允许实例化!
     *
     */
    protected Main()
    {
       
    }
   
    /**
     * 获取唯一实例.
     * @return
     */
    public static Main getInstance()
    {
        return memCached;
    }
   
    /**
     * 添加一个指定的值到缓存中.
     * @param key
     * @param value
     * @return
     */
    public boolean add(String key, Object value)
    {
        return mcc.add(key, value);
    }
   
    public boolean add(String key, Object value, Date expiry)
    {
        return mcc.add(key, value, expiry);
    }
   
    public boolean replace(String key, Object value)
    {
        return mcc.replace(key, value);
    }
   
    public boolean replace(String key, Object value, Date expiry)
    {
        return mcc.replace(key, value, expiry);
    }
   
    /**
     * 根据指定的关键字获取对象.
     * @param key
     * @return
     */
    public Object get(String key)
    {
        return mcc.get(key);
    }
   
    public static void main(String[] args)
    {
    	Main cache = Main.getInstance();
        cache.add( "hello" , 234 );
        System.out.print( "get value : " + cache.get("hello"));
    }

}

console端打印如下:

[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = initialize | Line = 617 ] | [++++ initializing pool with following settings:] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = initialize | Line = 618 ] | [++++ initial size: 5] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = initialize | Line = 619 ] | [++++ min spare   : 5] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = initialize | Line = 620 ] | [++++ max spare   : 250] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 647 ] | [++++ initializing internal hashing structure for consistent hashing] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 657 ] | [++++ added 192.168.159.128:11211 to server bucket] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 657 ] | [++++ added 192.168.159.128:11211 to server bucket] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 657 ] | [++++ added 192.168.159.128:11211 to server bucket] |
[DEBUG] [2012-03-27 15:56:10]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 668 ] | [+++ creating initial connections (5) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = createSocket | Line = 818 ] | [++++ created socket (Socket[addr=/192.168.159.128,port=11211,localport=4160]) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 679 ] | [++++ created and added socket: Socket[addr=/192.168.159.128,port=11211,localport=4160] for host 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = createSocket | Line = 818 ] | [++++ created socket (Socket[addr=/192.168.159.128,port=11211,localport=4161]) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 679 ] | [++++ created and added socket: Socket[addr=/192.168.159.128,port=11211,localport=4161] for host 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = createSocket | Line = 818 ] | [++++ created socket (Socket[addr=/192.168.159.128,port=11211,localport=4162]) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 679 ] | [++++ created and added socket: Socket[addr=/192.168.159.128,port=11211,localport=4162] for host 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = createSocket | Line = 818 ] | [++++ created socket (Socket[addr=/192.168.159.128,port=11211,localport=4163]) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 679 ] | [++++ created and added socket: Socket[addr=/192.168.159.128,port=11211,localport=4163] for host 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = createSocket | Line = 818 ] | [++++ created socket (Socket[addr=/192.168.159.128,port=11211,localport=4164]) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = populateBuckets | Line = 679 ] | [++++ created and added socket: Socket[addr=/192.168.159.128,port=11211,localport=4164] for host 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = getSock | Line = 879 ] | [cache socket pick hello null] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = getConnection | Line = 1027 ] | [++++ moving socket for host (192.168.159.128:11211) to busy pool ... socket: Socket[addr=/192.168.159.128,port=11211,localport=4164]] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = getSock | Line = 935 ] | [cache choose 192.168.159.128:11211 for hello] |
[INFO] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = set | Line = 740 ] | [Storing with native handler...] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1294 ] | [++++ Starting self maintenance....] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1308 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 4] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1327 ] | [++++ Need to create 1 new sockets for pool for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = createSocket | Line = 818 ] | [++++ created socket (Socket[addr=/192.168.159.128,port=11211,localport=4165]) for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1357 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1401 ] | [++++ Size of busy pool for host (192.168.159.128:11211)  = 1] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1443 ] | [+++ ending self maintenance.] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1294 ] | [++++ Starting self maintenance....] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1308 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1357 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1401 ] | [++++ Size of busy pool for host (192.168.159.128:11211)  = 1] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1443 ] | [+++ ending self maintenance.] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1294 ] | [++++ Starting self maintenance....] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1308 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1357 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1401 ] | [++++ Size of busy pool for host (192.168.159.128:11211)  = 1] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1443 ] | [+++ ending self maintenance.] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1294 ] | [++++ Starting self maintenance....] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1308 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1357 ] | [++++ Size of avail pool for host (192.168.159.128:11211) = 5] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1401 ] | [++++ Size of busy pool for host (192.168.159.128:11211)  = 1] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = MaintThread | Class = com.danga.MemCached.SockIOPool | Method = selfMaint | Line = 1443 ] | [+++ ending self maintenance.] |
[INFO] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = set | Line = 824 ] | [++++ memcache cmd (result code): add hello 4 0 4
 (STORED)] |
[INFO] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = set | Line = 827 ] | [++++ data successfully stored for key: hello] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool$SockIO | Method = close | Line = 1714 ] | [++++ marking socket (Socket[addr=/192.168.159.128,port=11211,localport=4164]) as closed and available to return to avail pool] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = checkIn | Line = 1151 ] | [++++ calling check-in on socket: Socket[addr=/192.168.159.128,port=11211,localport=4164] for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = checkIn | Line = 1156 ] | [++++ removing socket (Socket[addr=/192.168.159.128,port=11211,localport=4164]) from busy pool for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = checkIn | Line = 1162 ] | [++++ returning socket (Socket[addr=/192.168.159.128,port=11211,localport=4164] to avail pool for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = getSock | Line = 879 ] | [cache socket pick hello null] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = getConnection | Line = 1027 ] | [++++ moving socket for host (192.168.159.128:11211) to busy pool ... socket: Socket[addr=/192.168.159.128,port=11211,localport=4164]] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = getSock | Line = 935 ] | [cache choose 192.168.159.128:11211 for hello] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1276 ] | [++++ memcache get command: get hello
] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1288 ] | [++++ line: VALUE hello 4 4] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1296 ] | [++++ key: hello] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1297 ] | [++++ flags: 4] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1298 ] | [++++ length: 4] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1288 ] | [++++ line: END] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.MemCachedClient | Method = get | Line = 1380 ] | [++++ finished reading from cache server] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool$SockIO | Method = close | Line = 1714 ] | [++++ marking socket (Socket[addr=/192.168.159.128,port=11211,localport=4164]) as closed and available to return to avail pool] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = checkIn | Line = 1151 ] | [++++ calling check-in on socket: Socket[addr=/192.168.159.128,port=11211,localport=4164] for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = checkIn | Line = 1156 ] | [++++ removing socket (Socket[addr=/192.168.159.128,port=11211,localport=4164]) from busy pool for host: 192.168.159.128:11211] |
[DEBUG] [2012-03-27 15:56:11]  [Thread = main | Class = com.danga.MemCached.SockIOPool | Method = checkIn | Line = 1162 ] | [++++ returning socket (Socket[addr=/192.168.159.128,port=11211,localport=4164] to avail pool for host: 192.168.159.128:11211] |
get value : 234

3.管理memcached

连上memcache,然后stats,详细如下:
mqq@32_167_game:~> telnet server port
Trying 172.16.32.166...
Connected to 172.16.32.166.
Escape character is '^]'.
stats

这样就能显示memcached的信息,

指令格式:  <命令> <键> <标记> <有效期> <数据长度>\r\n  <命令> - command name   

主要是三个储存数据的三个命令, set, add, replace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值