MemCached测试
Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力。

它可以应对任意多个连接,使用非阻塞的网络IO。由于它的工作机制是在内存中开辟一块空间,然后建立一个HashTable,Memcached自管理这些HashTable。

package utils.cache;

public class TBean implements java.io.Serializable {
private static final long serialVersionUID = 1945562032261336919L;

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
package utils.cache;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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


/**
* 使用memcached的缓存实用类.
*
*/

public class MemCached
{
    
// 创建全局的唯一实例
    protected static MemCachedClient mcc = new MemCachedClient();
    
    protected static MemCached memCached = new MemCached();
    
    
// 设置与缓存服务器的连接池
    static {
        
// 服务器列表和其权重
        String[] servers = {
"127.0.0.1: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 MemCached()
    {
        
    }
    
    
/**
     * 获取唯一实例.
     * @return
     */

    public static MemCached 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)
    {
        MemCached cache = MemCached.getInstance();
        TBean tb = new TBean();
        tb.setName(
"铁木箱子");
        List list = new ArrayList();
        list.add(tb);
        TBean tb2 = new TBean();
        tb2.setName(
"铁木箱子2");
        list.add(tb2);
        TBean tb3 = new TBean();
        tb3.setName(
"test");
        list.add(tb3);
        cache.add(
"list", list);
        List list2 = (List)cache.get(
"list");
        for(int i=0;i<list2.size();i++){
         TBean tb11 = (TBean)list2.get(i);
         System.out.println(i+
":"+tb11.getName());
        }
    }
}

测试的index.jsp文件
<%@ page language=
"java" import="java.util.*,utils.cache.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href=
"<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv=
"pragma" content="no-cache">
<meta http-equiv=
"cache-control" content="no-cache">
<meta http-equiv=
"expires" content="0">    
<meta http-equiv=
"keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv=
"description" content="This is my page">
<!--
<link rel=
"stylesheet" type="text/css" href="styles.css">
-->
  </head>
  
  <body>
    This is my JSP page. <br>
    <%
     MemCached cache = MemCached.getInstance();
    
     List list = (List)cache.get(
"list");
    
     for(int i=0;i<list.size();i++){
         TBean tb11 = (TBean)list.get(i);
         out.println(i+
":"+tb11.getName()+"<br/>");
        }
     %>
  </body>
</html>

需要装服务器和客户端
java的客户端实现加入java_memcached-release_2.0.1.jar包和log4j即可。
参考一
参考二
参考三
lunzi   2009-02-10 23:34:47 评论:0   阅读:2718   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog