简述:
配置memcache服务
步骤:
1. 下载memcached
c源文件(需要编译)
http://code.google.com/p/memcached/downloads/list
http://code.jellycan.com/memcached/
2. 解压到文件夹memcached
3. 用管理员打开cmd, cd进入该文件夹,
首先安装该服务
- memcached.exe -d install
开启memcache服务
- memcached.exe -d start
3.使用netstat -a 可以看到memcached服务端口11211已经打开
4. Maven导入需要的memcached客户端包
包括(slf4j-log4j), 用来日志输出的
- <dependency>
- <groupId>com.whalin</groupId>
- <artifactId>Memcached-Java-Client</artifactId>
- <version>3.0.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.2</version>
- </dependency>
5.进入eclipse进行测试(转自: http://jason61719.iteye.com/blog/1002141)
- package com.anialy.project.springmvc.helper;
- import com.whalin.MemCached.MemCachedClient;
- import com.whalin.MemCached.SockIOPool;
- public class CacheHelper {
- /* 单例模式 */
- protected static MemCachedClient mcc = new MemCachedClient();
- private CacheHelper() {
- }
- /* 配置服务器组 */
- static {
- /* 定义IP地址和端口 */
- String[] servers = { "127.0.0.1:11211" };
- /* 设置缓存大小 */
- Integer[] weights = { 2 };
- /* 拿到一个连接池的实例 */
- 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);// 禁用nagle算法
- pool.setSocketConnectTO(0);
- pool.setSocketTO(3000);// 3秒超时
- /* 初始化 */
- pool.initialize();
- }
- public static boolean set(String arg0, Object arg1) {
- return mcc.set(arg0, arg1);
- }
- public static Object get(String arg0) {
- return mcc.get(arg0);
- }
- public static void main(String[] args) {
- CacheHelper.set("1", "cache_1");
- System.out.println(CacheHelper.get("1"));// cache_1
- System.out.println(CacheHelper.get("2"));// null
- }
- }
输出: