您的位置:首页 > 数据库 > Memcache

memcache 大内存分布式

2015-12-24 11:59 429 查看
一说到缓存,我们肯定第一想法就是cache,session什么的,但是,这些都是基于系统进程的,简单的说,就是A、B服务器的缓存非共享。所以,有了memcache这类技术的存在。和mongodb类似,需要使用mencache,需要安装客户端

1.memcache的下载安装

1.1
mencache1.4.4下载地址请戳我

1.2 命令安装



1.3 启动服务



1.4 停止和卸载是命令 stop、uninstall,查看当前memcache使用 -h,memcache默认端口号是11211

2.memcache的使用

server端

public class TM2 {
public static void main(String[] args) {
/* 初始化SockIOPool,管理memcached的连接池 */
String[] servers = { "127.0.0.1:11211" };

// 获取连接池实例
SockIOPool pool = SockIOPool.getInstance();
// 设置缓存服务器地址,可以设置多个实现分布式缓存
pool.setServers(servers);
// 当一个服务器请求超时是否去访问另一台
pool.setFailover(true);

// 初始化链接数/最小连接数/最大连接数
pool.setInitConn(10);
pool.setMinConn(5);
pool.setMaxConn(250);

// 线程休眠时间,防止太频繁访问
pool.setMaintSleep(30);
// 是否禁止nagle算法,nagle算法定义是:最多允许一个未被确认的小段。比如,队列里有ABCDE,A发送,且返回,才发送B,以此类推。具体请百度
pool.setNagle(false);
// 请求缓存超时时间
pool.setSocketTO(3000);
// 检查服务器是否失效
pool.setAliveCheck(true);

pool.initialize();
/* 建立MemcachedClient实例 */
MemCachedClient memCachedClient = new MemCachedClient();
for (int i = 0; i < 10; i++) {
/* 将对象加入到memcached缓存 */
boolean success = memCachedClient.set("" + i, "Hello!"+Math.random());
/* 从memcached缓存中按key值取对象 */
String result = (String) memCachedClient.get("" + i);
System.out.println(String.format("set( %d ): %s", i, success));
System.out.println(String.format("get( %d ): %s", i, result));
}
}
}


clien端



项目需要的jar下载,请戳我

memcache的详细配置和使用1

memcache的详细配置和使用2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: