memcached学习笔记

emcachememcached

Memcache
是项目名,memcached是服务名。让很多初接触的人感觉很是莫名其妙。个人认为正确的应该是用前者用更为正确一点。

 

 

 

Memcached下载地址:http://code.google.com/p/memcached/downloads/list

spymemcached

下载地址:http://code.google.com/p/spymemcached/downloads/list

xmemcached  

下载地址:http://code.google.com/p/xmemcached/downloads/list

 

memcached for Windows下载地址:http://www.splinedancer.com/memcached-win32/

  1.                Unzip the binaries in your desired directory (eg. c:\memcached)

2.       Install the service using the command: 'c:\memcached\memcached.exe -d install' from the command line

3.       Start the server from the Microsoft Management Console or by running the following command: 'c:\memcached\memcached.exe -d start'

4.       Use the server, by default listening to port 11211

 

 

Ubuntu下安装memcache

安装Memcache服务端

sudo apt-get install memcached

安装完Memcache服务端以后,我们需要启动该服务:

memcached -d -m 128 -p 11111 -u root


这里需要说明一下memcached服务的启动参数:

-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
显示帮助

 

连接到memcache

telnet IP  Post

      telnet 127.0.0.1 8888

查看memcache状态:statsstats resetstats slabs stats items  set|getgets

 

 

 

 

示例程序
1)      memcached client for java

从前面介绍的Java环境的Memcached客户端程序项目网址里,下载最新版的客户端程序包:java_memcached-release_2.5.1.zip,解压后,文件夹里找到java_memcached- release_2.5.1.jar,这个就是客户端的JAR包。将此JAR包添加到项目的构建路径里,则项目中,就可以使用Memcached了。

示例代码如下:

package temp;

 

import com.danga.MemCached.*;

import org.apache.log4j.*;

 

public class CacheTest {

    public static void main(String[] args) {

       /**

        * 初始化SockIOPool,管理memcached的连接池

        * */

       String[] servers = { "10.11.15.222:10000" };

       SockIOPool pool = SockIOPool.getInstance();

       pool.setServers(servers);

       pool.setFailover(true);

       pool.setInitConn(10);

       pool.setMinConn(5);

       pool.setMaxConn(250);

       pool.setMaintSleep(30);

       pool.setNagle(false);

       pool.setSocketTO(3000);

       pool.setAliveCheck(true);

       pool.initialize();

 

       /**

        * 建立MemcachedClient实例

        * */

       MemCachedClient memCachedClient = new MemCachedClient();

       for (int i = 0; i < 1000; i++) {

           /**

            * 将对象加入到memcached缓存

            * */

           boolean success = memCachedClient.set("" + i, "Hello!");

           /**

            * memcached缓存中按key值取对象

            * */

           String result = (String) memCachedClient.get("" + i);

           System.out.println(String.format("set( %d ): %s", i, success));

           System.out.println(String.format("get( %d ): %s", i, result));

       }

    }

}


2)      spymemcached

spymemcached当前版本是2.5版本,官方网址是:http://code.google.com/p/spymemcached/。可以从地址:http://spymemcached.googlecode.com/files/memcached-2.5.jar下载最新版本来使用。

示例代码如下:

package temp;

 

import java.net.InetSocketAddress;

import java.util.concurrent.Future;

 

import net.spy.memcached.MemcachedClient;

 

public class TestSpyMemcache {

    public static void main(String[] args) {

       // 保存对象

       try {

           /* 建立MemcachedClient实例,并指定memcached服务的IP地址和端口号 */

           MemcachedClient mc = new MemcachedClient(new InetSocketAddress("10.11.15.222", 10000));

           Future<Boolean> b = null;

           /* key值,过期时间()和要缓存的对象setmemcached */

           b = mc.set("neea:testDaF:ksIdno", 900, "someObject");

           if (b.get().booleanValue() == true) {

              mc.shutdown();

           }

       } catch (Exception ex) {

           ex.printStackTrace();

       }

       // 取得对象

        try {

           /* 建立MemcachedClient实例,并指定memcached服务的IP地址和端口号 */

           MemcachedClient mc = new MemcachedClient(new InetSocketAddress("10.11.15.222", 10000));

           /* 按照key值从memcached中查找缓存,不存在则返回null */

           Object b = mc.get("neea:testDaF:ksIdno");

           System.out.println(b.toString());

           mc.shutdown();

       } catch (Exception ex) {

           ex.printStackTrace();

       }

    }

}


3)      xmemcached

Xmemcached的官方网址是:http://code.google.com/p/xmemcached/,可以从其官网上下载最新版本的1.2.4来使用。地址是:http://xmemcached.googlecode.com/files/xmemcached-1.2.4-src.tar.gz

示例代码如下:

package temp;

 

import java.io.IOException;

import java.util.concurrent.TimeoutException;

 

import net.rubyeye.xmemcached.utils.AddrUtil;

import net.rubyeye.xmemcached.MemcachedClient;

import net.rubyeye.xmemcached.MemcachedClientBuilder;

import net.rubyeye.xmemcached.XMemcachedClientBuilder;

import net.rubyeye.xmemcached.exception.MemcachedException;

 

public class TestXMemcache {

    public static void main(String[] args) {

       MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil

              .getAddresses("10.11.15.222:10000"));

       MemcachedClient memcachedClient;

       try {

           memcachedClient = builder.build();

 

           memcachedClient.set("hello", 0, "Hello,xmemcached");

           String value = memcachedClient.get("hello");

           System.out.println("hello=" + value);

           memcachedClient.delete("hello");

           value = memcachedClient.get("hello");

           System.out.println("hello=" + value);

           // close memcached client

           memcachedClient.shutdown();

       } catch (MemcachedException e) {

           System.err.println("MemcachedClient operation fail");

           e.printStackTrace();

       } catch (TimeoutException e) {

           System.err.println("MemcachedClient operation timeout");

           e.printStackTrace();

       } catch (InterruptedException e) {

           // ignore

       }catch (IOException e) {

           System.err.println("Shutdown MemcachedClient fail");

           e.printStackTrace();

       }

    }

}

memcached中写入的数据必须是可序列化的

 

你可能感兴趣的:(缓存,memcache)