温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何利用memcached java client一个简单的应用

发布时间:2021-11-30 17:47:43 来源:亿速云 阅读:131 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关如何利用memcached java client一个简单的应用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

关键字:   利用memcached java client一个简单的应用

1.memcached java client一个实现的下载地址

http://www.whalin.com/memcached/#download
2.  利用memcached java client 一个简单的应用

java 代码

package memcache;

import java.util.Date;

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

public class Test {

/**
 * @param args
 */
 protected static MemCachedClient mcc = new MemCachedClient();      
   
    static {      
        String[] servers ={"124.42.60.19:12000"};      
       
        Integer[] weights = { 3 };      
       
        //创建一个实例对象SockIOPool    
        SockIOPool pool = SockIOPool.getInstance();      
       
        // set the servers and the weights    
        //设置Memcached Server    
        pool.setServers( servers );      
        pool.setWeights( weights );      
       
        // set some basic pool settings      
        // 5 initial, 5 min, and 250 max conns      
        // and set the max idle time for a conn      
        // to 6 hours      
        pool.setInitConn( 5 );      
        pool.setMinConn( 5 );      
        pool.setMaxConn( 250 );      
        pool.setMaxIdle( 1000 * 60 * 60 * 6 );      
       
        // set the sleep for the maint thread      
        // it will wake up every x seconds and      
        // maintain the pool size      
        pool.setMaintSleep( 30 );      
       
//         Tcp的规则就是在发送一个包之前,本地机器会等待远程主机    
//         对上一次发送的包的确认信息到来;这个方法就可以关闭套接字的缓存,    
//         以至这个包准备好了就发;    
        pool.setNagle( false );      
        //连接建立后对超时的控制    
        pool.setSocketTO( 3000 );    
        //连接建立时对超时的控制    
        pool.setSocketConnectTO( 0 );      
       
        // initialize the connection pool      
        //初始化一些值并与MemcachedServer段建立连接    
        pool.initialize();    
               
       
        // lets set some compression on for the client      
        // compress anything larger than 64k      
        mcc.setCompressEnable( true );      
        mcc.setCompressThreshold( 64 * 1024 );      
    }      
           
    public static void bulidCache(){      
        //set(key,value,Date) ,Date是一个过期时间,如果想让这个过期时间生效的话,这里传递的new Date(long date) 中参数date,需要是个大于或等于1000的值。    
        //因为java client的实现源码里是这样实现的 expiry.getTime() / 1000 ,也就是说,如果 小于1000的值,除以1000以后都是0,即永不过期    
        mcc.set( "test", "This is a test String" ,new Date(10000));   //十秒后过期    
             
    }      
     
    public static void output() {      
        //从cache里取值    
        String value = (String) mcc.get( "test" );      
        System.out.println(value);        
    }      
           
    public static void main(String[] args){    
   
        bulidCache();  
     
        output();          
       
    }
}


运行输出值为:


This is a test String


3.注释掉buildCache();

十秒后运行,输出值为 null

感谢各位的阅读!关于“如何利用memcached java client一个简单的应用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI