• liu13430 Java应用程序如何使用memcached


    package com.memcached.demo;
     
    import java.util.Date;
    import java.util.Map;
     
    import com.danga.MemCached.MemCachedClient;
    import com.danga.MemCached.SockIOPool;
     
     
    public class MemcachedHelper {
     
        private static MemCachedClient mcc = new MemCachedClient();
         
        static {
            //创建Socket连接池实例
            SockIOPool spool = SockIOPool.getInstance();       
            String[] servers = {"127.0.0.1:11211"};
            Integer[] weights = {3};       
            //设置服务器信息
            spool.setServers(servers);
            spool.setWeights(weights);     
            spool.setFailover(true);       
            //设置初始连接数、最小和最大连接数以及最大处理时间
            spool.setInitConn(5);
            spool.setMinConn(5);
            spool.setMaxConn(250);
            spool.setMaxIdle(1000 * 60 * 60 * 6);         
            //设置主线程睡眠时间
            spool.setMaintSleep(30);          
            //设置TCP参数、连接超时等
            spool.setNagle(false);
            spool.setSocketTO(3000);
            spool.setSocketConnectTO(0);
            spool.setAliveCheck(true);        
            //初始化连接池
            spool.initialize();
        }  
         
        protected MemcachedHelper(){
        }
         
        protected static MemcachedHelper instance = new MemcachedHelper();
         
        /**
         * 为受保护的对象提供一个公共的访问方法
         */
        public static MemcachedHelper getInstance () {
           return instance;
        }
         
        /**
         * 插入一条记录到缓存服务器中
         * @param key
         * @param value
         * @return
         */
        public boolean add(String key,Object value) {
           return mcc.add(key, value);
        }
        /**
         * 插入一条记录到缓存服务器,并设置过期时间,单位为秒
         * @param key
         * @param value
         * @param expiry
         * @return
         */
        public boolean add (String key,Object value,Date expiry) {
           return mcc.add(key, value,expiry);
        }
        /**
         * 根据KEY替换缓存服务器中的数据
         * @param key
         * @param value
         * @return
         */
        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);
        }
        /**
         * 当KEY不存在时将数据添加到缓存服务器中
         * 当KEY存在时则替换原来的数据
         * @param key
         * @param value
         * @return
         */
        public boolean set(String key,Object value){
            return mcc.set(key, value);
        }
         
        public boolean set(String key,Object value,Date expiry){
            return mcc.set(key, value,expiry);
        }   
       
        /**
         * 根据KEY删除缓存服务器中的数据
         * @param key
         * @return
         */
        public boolean delete(String key){
            return mcc.delete(key);
        }  
        /**
         * 根据指定的KEY获取数据
         */
        public Object get(String key) {
           return mcc.get(key);
        }
         
        /**
         * 批量获取多个KEY的数据
         * @param keys
         * @return
         */
        public Map<String,Object> getMulti(String[] keys){
            return mcc.getMulti(keys);
        }
         
        /**
         * 判断指定的KEY是否已经存在
         * @param key
         * @return
         */
        public boolean keyExists(String key){
            return mcc.keyExists(key);
        }
    }
    package com.memcached.demo;
     
    import java.util.Date;
     
    import org.junit.Test;
     
    public class MemcachedTimeTest {
         
        private MemcachedHelper install = MemcachedHelper.getInstance();
         
        private String key = "YE1000";
         
        @Test
        public void testAdd(){
            String value = "这是一个Demo测试案例!";
            install.add(key, value, new Date(1000 * 10));
        }
     
        @Test
        public void testSet(){
            String value = "这是一个Demo测试案例!";
            install.set(key, value, new Date(1000 * 20));
        }
         
        @Test
        public void testGet(){     
            String value = (String)install.get(key);
            System.out.println(value);
        }
         
        @Test
        public void testDelete(){
            install.delete(key);
        }
         
        @Test
        public void testKeyExists(){
            System.out.println(install.keyExists(key));
        }
    }
  • 相关阅读:
    python 发包爬取中国移动充值页面---可判断手机号是否异常
    python利用selenium和safari浏览器驱动实现新浪微博自动点赞 Demo
    Django学习报错记录
    nginx和tomcat的区别
    Mac主机映射到域名
    mac下eclipse安装svn插件-subclipse
    移动端——等分,居中
    移动端——重置样式
    M端页面-绝对定位布局
    jquery-练习-折叠效果
  • 原文地址:https://www.cnblogs.com/xsht/p/5407496.html
Copyright © 2020-2023  润新知