Memcache的使用

一、Win 64下安装Memcache

1、下载地址:http://download.csdn.net/detail/zhaotengfei36520/8182503
下载后解压,如下图所示:
这里写图片描述
2、打开cmd命令行,进入memcached目录,执行memcached -d install命令,安装。
安装完成后,你会看到windows服务里面多了一个memcache,如下:
这里写图片描述

3、启动memcache服务
启动memcache有两种方式,一种是在上图所示的Windows服务窗口启动,另一种是执行memcached -d start命令。

详情见:http://blog.csdn.net/zhaotengfei36520/article/details/41315329

二、Memcache使用实例

在使用Memcache时,需要引入下面的几个jar包。
这里写图片描述

package com.ghs.cache.memcache;

import java.util.Date;
import java.util.Properties;

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

public class MemcacheService {

    private String[] servers;
    /**初始连接数*/
    private int initConn = 0;
    /**最小连接数*/
    private int minConn = 0;
    /**最大连接数*/
    private int maxConn = 0;
    /**最大空闲连接数*/
    private long maxIdle = 0;
    /**初始连接数*/
    private long mainSleep = 0;
    /**初始连接数*/
    private boolean nagle = false;

    private SockIOPool pool = SockIOPool.getInstance();

    private MemCachedClient client=null;


    public MemcacheService() {
        loadParams();
        pool.setServers(servers);
        pool.setInitConn(initConn);
        pool.setMinConn(minConn);
        pool.setMaxIdle(maxIdle);
        pool.setMaintSleep(mainSleep);
        pool.setNagle(nagle);
        pool.initialize();

        client= new MemCachedClient();
    }

    /**
     * 加载配置文件中的参数
     */
    private void loadParams(){
        Properties properties = new Properties();
        try {
            properties.load(MemcacheService.class.getResourceAsStream("/memcache.properties"));
            servers = properties.get("servers").toString().split(",");
            initConn = Integer.parseInt(properties.get("initConn").toString());
            maxConn = Integer.parseInt(properties.get("maxConn").toString());
            maxConn = Integer.parseInt(properties.get("maxConn").toString());
            maxIdle = Integer.parseInt(properties.get("maxIdle").toString());
            mainSleep = Integer.parseInt(properties.get("mainSleep").toString());
            nagle = Boolean.parseBoolean(properties.get("nagle").toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //==============下面是存取操作===================
    public void set(String key,Object value){
        client.set(key, value);
    }

    public void set(String key,Object value,Date expiry){
        client.set(key, value, expiry);
    }

    public Object get(String key){
        return client.get(key);
    }

    public static void main(String[] args) {
        MemcacheService service = new MemcacheService();
        service.set("name", "zhangsan");
        System.out.println(service.get("name"));
    }
}

配置文件:memcache.properties

servers=127.0.0.1:11211
initConn=5
minConn=5
maxConn=250
maxIdle=1000
mainSleep=30
nagle=false
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值