用户登录
用户注册

分享至

java memcached集群

  • 作者: 用户57127498
  • 来源: 51数据库
  • 2020-10-17

一、 概念

Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,用于在动态系统中减少数据库负载,提升性能。

二、 适用场合

1. 分布式应用。由于memcached本身基于分布式的系统,所以尤其适合大型的分布式系统。

2. 数据库前段缓存。数据库常常是网站系统的瓶颈。数据库的大并发量访问,常常造成网站内存溢出。当然我们也可以使用hibernate的缓存机制。但memcached是基于分布式的,并可独立于网站应用本身,所以更适合大型网站进行应用的拆分。

3. 服务器间数据共享。举例来讲,我们将网站的登录系统、查询系统拆分为两个应用,放在不同的服务器上,并进行集群,那这个时候用户登录后,登录信息如何从登录系统服务器同步到查询系统服务器呢?这时候,我们便可以使用memcached,登录系统将登录信息缓存起来,查询系统便可以获得登录信息,就像获取本地信息一样。

4.代码如下:

package com.demo.memcached;

import Java.util.Date;

import com.danga.MemCached.MemCachedClient;

import com.danga.MemCached.SockIOPool;

public class MemCached {

private static MemCachedClient cachedClient = new MemCachedClient(); // memcached客户端单例

/**

* 初始化连接池

*/

static {

System.out.println("初始化连接池");

// 获取连接池的实例

SockIOPool pool = SockIOPool.getInstance();

// 服务器列表及其权重

String[] servers = { "172.20.0.196:11211"};

Integer[] weights = { 3 };

// 设置服务器信息

pool.setServers(servers);

pool.setWeights(weights);

// 设置初始连接数、最小连接数、最大连接数、最大处理时间

pool.setInitConn(10);

pool.setMinConn(10);

pool.setMaxConn(1000);

// pool.setMaxIdle(1000 * 60 * 60);

pool.setMaxIdle(1000 * 60 * 60);

//设置主线程睡眠时间,每3秒苏醒一次,维持连接池大小  

//maintSleep 千万不要设置成30,访问量一大就出问题,单位是毫秒,推荐30000毫秒。

pool.setMaintSleep(30000);

//关闭套接字缓存

pool.setNagle(false);

//连接建立后的超时时间

pool.setSocketTO(3000);

//连接建立时的超时时间

pool.setSocketConnectTO(0); 

// 初始化并启动连接池

pool.initialize();

// 压缩设置,超过指定大小的都压缩

// cachedClient.setCompressEnable(true);

// cachedClient.setCompressThreshold(1024*1024);

}

public static boolean add(String key, Object value) {

return cachedClient.add(key, value);

}

/**

* 新增缓存数据,该KEY值如果没有则插入

* @param key

*        键(key)

* @param value

* @param expire

*        过期时间(单位是秒)

*        

* @return

*/

public static boolean add(String key, Object value, Integer expire) {

return cachedClient.add(key, value, expire);

}

public static boolean add(String key, Object value,  Date expireDate) {

return cachedClient.add(key, value, expireDate);

}

public static boolean set(String key, Object value) {

return cachedClient.set(key, value);

}

/**

* 设置缓存中的对象(value),如果没有则插入,如果有则修改。

* @param key

* @param value

* @param expire

* @return

*/

public static boolean set(String key, Object value, Integer expire) {

return cachedClient.set(key, value, expire);

/**

* @param key

* @param value

* @param expireDate

*        失效日期

* @return

*/

public static boolean set(String key, Object value, Date expireDate) {

return cachedClient.set(key, value, expireDate);

}

public static boolean replace(String key, Object value) {

return cachedClient.replace(key, value);

}

/**

* 该键的新值(new value),如果有则修改。

* @param key

* @param value

* @param expire

* @return

*/

public static boolean replace(String key, Object value, Integer expire) {

return cachedClient.replace(key, value, expire);

}

public static boolean replace(String key, Object value, Date expireDate) {

return cachedClient.replace(key, value, expireDate);

}

public static Object get(String key) {

return cachedClient.get(key);

}

/**

* 清空所有对象

*/

public static void flushAll(){

cachedClient.flushAll();

}

}






public class MemcachedTest {



public static void main(String[] agr){


// MemCached.set("mem", "12e3232", 1);

// MemCached.set("mem1", "mem1mem1");

// Date date=new Date(4000);

// MemCached.set("mem", "12e3232", date);

try{

// Thread.sleep(3000);

String mem=(String)MemCached.get("mem");

System.out.println("mem="+mem);

}catch(Exception ex){

ex.printStackTrace();

}

}


软件
前端设计
程序设计
Java相关