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

转载 2012年03月27日 15:54:41

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

相关文章推荐

多线程(三)线程控制之后台线程

有一种线程,它是在后台运行的,它的任务是为其他的线程提供服务,这种线程被称为“后台线程(Daemon Thread)”,又称为“守护线程”或“精灵线程”。JVM的垃圾回收线程就是典型的后台线程。 后台...

spring整合spymemcached实现Memcached的各种操作

1.配置pom.xml、web.xml、applicationContext.xml和servletmvc.xml,仅仅列出与memcached相关配置 pom.xml ...

OpenCL学习笔记(三):OpenCL安装,编程简介与helloworld

从图中可以看出(参考《OpenCL 编程入门》): 1. 异构计算设备,可以是CPU或GPU。现在也有支持OpenCL的FPGA设备和至强融核协处理设备(MIC)。 2. OpenCL的API通过Co...

【nginx学习笔记01】nginx安装与HelloWorld

安装的环境 nginx安装 安装nginx依赖包 安装nginx HelloWorld 安装中遇到的问题 用到的一些命令安装的环境操作系统:ubuntu 16.04 LTS(64位)nginx安装安装...

cocos2d-x学习笔记之cocos2d-x的安装与helloworld测试

最近入手学习cocos2d-x, 整个图书馆居然只有一本书, cocos2d-x手机游戏开发 跨ios android 和沃phone 平台, 按着按着书上的说法,我安装了cygwin,安装了4...
  • mixi57
  • mixi57
  • 2013年05月25日 01:34
  • 645

Memcached学习笔记

  • 2010年10月05日 13:49
  • 103KB
  • 下载

memcached 安装学习笔记(win7 64 环境)

1、首先你有下载下来安装包(Memcached win7 64big下载) 2、解压到本地目录如:D:\memcached ...
  • mchehe
  • mchehe
  • 2017年03月01日 11:55
  • 76

Memcached学习笔记2.doc

  • 2011年06月08日 22:07
  • 45KB
  • 下载

Memcached学习笔记

  • 2015年12月17日 17:23
  • 620KB
  • 下载

nginx 源码学习笔记(一)——初识nginx helloworld模块

最近看完一本书,而且还是跟我一个学校的人写的关于nginx的内容,个人觉得内容可以更充实点,包括整本书的后半本没有太大的价值,但是总体来说还是不错的哈哈!(是不有点自恋),不费话了,今天记录下我刚刚编...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Memcached 学习笔记(一)安装 & helloworld
举报原因:
原因补充:

(最多只允许输入30个字)