**
memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分显著。这是一套开放源代码软件,以BSD license授权发布。
**
Memcached 内存管理机制
Menceched 通过预分配指定的内存空间来存取数据,所有的数据都保存在 memcached 内置的内存中。利用 Slab
Allocation
机制来分配和管理内存。按照预先规定的大小,将分配的内存分割成特定长度的内存块,再把尺寸相同的内存块分成组,这些内存块不会释放,可以重复利用。当存入的数据占满内存空间时,Memcached
使用 LRU 算法自动删除不是用的缓存数据,即重用过期数据的内存空间。Memcached
是为缓存系统设计的,因此没有考虑数据的容灾问题,和机器的内存一样,重启机器将会丢失,如果希望服务重启数据依然能保留,那么就需要 sina
网开发的 Memcachedb 持久性内存缓冲系统,当然还有常见的 NOSQL 服务如 redis。默认监听端口:11211
Memcached Centos 下安装启动
安装
yum install -y memcached
启动
/usr/bin/memcached -b -p 11211 -m 150 -u root >> /tmp/memcached.log &
查看线程
ps -ef | grep memcached
在springboot项目中的使用
1.引入相关依赖
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.5</version>
</dependency>
2.添加配置文件
# 单个 Memcached 配置
memcached.servers=127.0.0.1:11211
# 连接池
memcached.poolSize=15
#操作超时时间
memcached.opTimeout=6000
3.创建 XMemcachedProperties 类,读配置信息:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author andy
* @create 2019/7/3
* @since 1.0.0
*/
@Component
@ConfigurationProperties(prefix = "memcached")
public class XMemcachedProperties {
private String servers;
private int poolSize;
private long opTimeout;
public String getServers() {
return servers;
}
public void setServers(String servers) {
this.servers = servers;
}
public int getPoolSize() {
return poolSize;
}
public void setPoolSize(int poolSize) {
this.poolSize = poolSize;
}
public long getOpTimeout() {
return opTimeout;
}
public void setOpTimeout(long opTimeout) {
this.opTimeout = opTimeout;
}
4.利用 @Configuration 注解,在启动时对 Memcached 进行初始化
import com.nanshan.cache.entity.XMemcachedProperties;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.io.IOException;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author andy
* @create 2019/7/3
* @since 1.0.0
*/
@Configuration
public class MemcachedBuilder {
protected static Logger logger = LoggerFactory.getLogger(MemcachedBuilder.class);
@Resource
private XMemcachedProperties xMemcachedProperties;
@Bean
public MemcachedClient getMemcachedClient() {
MemcachedClient memcachedClient = null;
try {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(xMemcachedProperties.getServers()));
builder.setConnectionPoolSize(xMemcachedProperties.getPoolSize());
builder.setOpTimeout(xMemcachedProperties.getOpTimeout());
memcachedClient = builder.build();
} catch (IOException e) {
logger.error("inint MemcachedClient failed ",e);
}
return memcachedClient;
}
}
5.测试 Memcached 的 get 、set 方法。
@Autowired
private MemcachedClient memcachedClient;
@Test
public void testGetSet() throws Exception {
memcachedClient.set("hello", 0, "Hello,xmemcached");
String value = memcachedClient.get("hello");
System.out.println("hello=" + value);
memcachedClient.delete("hello");
}
6.这是 Memcached 的 所有方法,大家有兴趣可以自己去尝试
public interface MemcachedClient {
int DEFAULT_READ_THREAD_COUNT = 0;
boolean DEFAULT_TCP_KEEPLIVE = true;
int DEFAULT_CONNECT_TIMEOUT = 60000;
int DEFAULT_TCP_SEND_BUFF_SIZE = 32768;
boolean DEFAULT_TCP_NO_DELAY = true;
int DEFAULT_SESSION_READ_BUFF_SIZE = 131072;
int DEFAULT_TCP_RECV_BUFF_SIZE = 65536;
long DEFAULT_OP_TIMEOUT = 5000L;
int DEFAULT_CONNECTION_POOL_SIZE = 1;
int DEFAULT_SESSION_IDLE_TIMEOUT = 5000;
long DEFAULT_HEAL_SESSION_INTERVAL = 2000L;
int MAX_QUEUED_NOPS = 40000;
int DYNAMIC_MAX_QUEUED_NOPS = (int)(40000.0D * ((double)Runtime.getRuntime().maxMemory() / 1024.0D / 1024.0D / 1024.0D));
int DEFAULT_MAX_QUEUED_NOPS = DYNAMIC_MAX_QUEUED_NOPS > 40000 ? '鱀' : DYNAMIC_MAX_QUEUED_NOPS;
int DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD = 1000;
void setMergeFactor(int var1);
long getConnectTimeout();
void setConnectTimeout(long var1);
Connector getConnector();
void setOptimizeGet(boolean var1);
void setOptimizeMergeBuffer(boolean var1);
boolean isShutdown();
void addServer(String var1, int var2) throws IOException;
void addServer(InetSocketAddress var1) throws IOException;
void addServer(String var1) throws IOException;
List<String> getServersDescription();
void removeServer(String var1);
/** @deprecated */
@Deprecated
void setBufferAllocator(BufferAllocator var1);
<T> T get(String var1, long var2, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> T get(String var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> T get(String var1, Transcoder<T> var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> T get(String var1) throws TimeoutException, InterruptedException, MemcachedException;
<T> GetsResponse<T> gets(String var1, long var2, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> GetsResponse<T> gets(String var1) throws TimeoutException, InterruptedException, MemcachedException;
<T> GetsResponse<T> gets(String var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> GetsResponse<T> gets(String var1, Transcoder var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, T> get(Collection<String> var1, long var2, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, T> get(Collection<String> var1, Transcoder<T> var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, T> get(Collection<String> var1) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, T> get(Collection<String> var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, GetsResponse<T>> gets(Collection<String> var1, long var2, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, GetsResponse<T>> gets(Collection<String> var1) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, GetsResponse<T>> gets(Collection<String> var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> Map<String, GetsResponse<T>> gets(Collection<String> var1, Transcoder<T> var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean set(String var1, int var2, T var3, Transcoder<T> var4, long var5) throws TimeoutException, InterruptedException, MemcachedException;
boolean set(String var1, int var2, Object var3) throws TimeoutException, InterruptedException, MemcachedException;
boolean set(String var1, int var2, Object var3, long var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean set(String var1, int var2, T var3, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
void setWithNoReply(String var1, int var2, Object var3) throws InterruptedException, MemcachedException;
<T> void setWithNoReply(String var1, int var2, T var3, Transcoder<T> var4) throws InterruptedException, MemcachedException;
<T> boolean add(String var1, int var2, T var3, Transcoder<T> var4, long var5) throws TimeoutException, InterruptedException, MemcachedException;
boolean add(String var1, int var2, Object var3) throws TimeoutException, InterruptedException, MemcachedException;
boolean add(String var1, int var2, Object var3, long var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean add(String var1, int var2, T var3, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
void addWithNoReply(String var1, int var2, Object var3) throws InterruptedException, MemcachedException;
<T> void addWithNoReply(String var1, int var2, T var3, Transcoder<T> var4) throws InterruptedException, MemcachedException;
<T> boolean replace(String var1, int var2, T var3, Transcoder<T> var4, long var5) throws TimeoutException, InterruptedException, MemcachedException;
boolean replace(String var1, int var2, Object var3) throws TimeoutException, InterruptedException, MemcachedException;
boolean replace(String var1, int var2, Object var3, long var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean replace(String var1, int var2, T var3, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
void replaceWithNoReply(String var1, int var2, Object var3) throws InterruptedException, MemcachedException;
<T> void replaceWithNoReply(String var1, int var2, T var3, Transcoder<T> var4) throws InterruptedException, MemcachedException;
boolean append(String var1, Object var2) throws TimeoutException, InterruptedException, MemcachedException;
boolean append(String var1, Object var2, long var3) throws TimeoutException, InterruptedException, MemcachedException;
void appendWithNoReply(String var1, Object var2) throws InterruptedException, MemcachedException;
boolean prepend(String var1, Object var2) throws TimeoutException, InterruptedException, MemcachedException;
boolean prepend(String var1, Object var2, long var3) throws TimeoutException, InterruptedException, MemcachedException;
void prependWithNoReply(String var1, Object var2) throws InterruptedException, MemcachedException;
boolean cas(String var1, int var2, Object var3, long var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, int var2, T var3, Transcoder<T> var4, long var5, long var7) throws TimeoutException, InterruptedException, MemcachedException;
boolean cas(String var1, int var2, Object var3, long var4, long var6) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, int var2, T var3, Transcoder<T> var4, long var5) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, int var2, CASOperation<T> var3, Transcoder<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, int var2, GetsResponse<T> var3, CASOperation<T> var4, Transcoder<T> var5) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, int var2, GetsResponse<T> var3, CASOperation<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, GetsResponse<T> var2, CASOperation<T> var3) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, int var2, CASOperation<T> var3) throws TimeoutException, InterruptedException, MemcachedException;
<T> boolean cas(String var1, CASOperation<T> var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> void casWithNoReply(String var1, GetsResponse<T> var2, CASOperation<T> var3) throws TimeoutException, InterruptedException, MemcachedException;
<T> void casWithNoReply(String var1, int var2, GetsResponse<T> var3, CASOperation<T> var4) throws TimeoutException, InterruptedException, MemcachedException;
<T> void casWithNoReply(String var1, int var2, CASOperation<T> var3) throws TimeoutException, InterruptedException, MemcachedException;
<T> void casWithNoReply(String var1, CASOperation<T> var2) throws TimeoutException, InterruptedException, MemcachedException;
/** @deprecated */
@Deprecated
boolean delete(String var1, int var2) throws TimeoutException, InterruptedException, MemcachedException;
boolean delete(String var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
boolean delete(String var1, long var2, long var4) throws TimeoutException, InterruptedException, MemcachedException;
boolean touch(String var1, int var2, long var3) throws TimeoutException, InterruptedException, MemcachedException;
boolean touch(String var1, int var2) throws TimeoutException, InterruptedException, MemcachedException;
<T> T getAndTouch(String var1, int var2, long var3) throws TimeoutException, InterruptedException, MemcachedException;
<T> T getAndTouch(String var1, int var2) throws TimeoutException, InterruptedException, MemcachedException;
Map<InetSocketAddress, String> getVersions() throws TimeoutException, InterruptedException, MemcachedException;
long incr(String var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
long incr(String var1, long var2, long var4) throws TimeoutException, InterruptedException, MemcachedException;
long incr(String var1, long var2, long var4, long var6) throws TimeoutException, InterruptedException, MemcachedException;
long decr(String var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
long decr(String var1, long var2, long var4) throws TimeoutException, InterruptedException, MemcachedException;
long decr(String var1, long var2, long var4, long var6) throws TimeoutException, InterruptedException, MemcachedException;
void flushAll() throws TimeoutException, InterruptedException, MemcachedException;
void flushAllWithNoReply() throws InterruptedException, MemcachedException;
void flushAll(long var1) throws TimeoutException, InterruptedException, MemcachedException;
void flushAll(InetSocketAddress var1) throws MemcachedException, InterruptedException, TimeoutException;
void flushAllWithNoReply(InetSocketAddress var1) throws MemcachedException, InterruptedException;
void flushAll(InetSocketAddress var1, long var2) throws MemcachedException, InterruptedException, TimeoutException;
/** @deprecated */
@Deprecated
void flushAll(String var1) throws TimeoutException, InterruptedException, MemcachedException;
Map<String, String> stats(InetSocketAddress var1) throws MemcachedException, InterruptedException, TimeoutException;
Map<String, String> stats(InetSocketAddress var1, long var2) throws MemcachedException, InterruptedException, TimeoutException;
Map<InetSocketAddress, Map<String, String>> getStats(long var1) throws MemcachedException, InterruptedException, TimeoutException;
Map<InetSocketAddress, Map<String, String>> getStats() throws MemcachedException, InterruptedException, TimeoutException;
Map<InetSocketAddress, Map<String, String>> getStatsByItem(String var1) throws MemcachedException, InterruptedException, TimeoutException;
void shutdown() throws IOException;
boolean delete(String var1) throws TimeoutException, InterruptedException, MemcachedException;
Transcoder getTranscoder();
void setTranscoder(Transcoder var1);
Map<InetSocketAddress, Map<String, String>> getStatsByItem(String var1, long var2) throws MemcachedException, InterruptedException, TimeoutException;
long getOpTimeout();
void setOpTimeout(long var1);
Map<InetSocketAddress, String> getVersions(long var1) throws TimeoutException, InterruptedException, MemcachedException;
/** @deprecated */
@Deprecated
Collection<InetSocketAddress> getAvaliableServers();
Collection<InetSocketAddress> getAvailableServers();
void addServer(String var1, int var2, int var3) throws IOException;
void addServer(InetSocketAddress var1, int var2) throws IOException;
/** @deprecated */
@Deprecated
void deleteWithNoReply(String var1, int var2) throws InterruptedException, MemcachedException;
void deleteWithNoReply(String var1) throws InterruptedException, MemcachedException;
void incrWithNoReply(String var1, long var2) throws InterruptedException, MemcachedException;
void decrWithNoReply(String var1, long var2) throws InterruptedException, MemcachedException;
void setLoggingLevelVerbosity(InetSocketAddress var1, int var2) throws TimeoutException, InterruptedException, MemcachedException;
void setLoggingLevelVerbosityWithNoReply(InetSocketAddress var1, int var2) throws InterruptedException, MemcachedException;
void addStateListener(MemcachedClientStateListener var1);
void removeStateListener(MemcachedClientStateListener var1);
Collection<MemcachedClientStateListener> getStateListeners();
void flushAllWithNoReply(int var1) throws InterruptedException, MemcachedException;
void flushAll(int var1, long var2) throws TimeoutException, InterruptedException, MemcachedException;
void flushAllWithNoReply(InetSocketAddress var1, int var2) throws MemcachedException, InterruptedException;
void flushAll(InetSocketAddress var1, long var2, int var4) throws MemcachedException, InterruptedException, TimeoutException;
void setHealSessionInterval(long var1);
void setEnableHealSession(boolean var1);
long getHealSessionInterval();
Protocol getProtocol();
void setPrimitiveAsString(boolean var1);
void setConnectionPoolSize(int var1);
void setEnableHeartBeat(boolean var1);
void setSanitizeKeys(boolean var1);
boolean isSanitizeKeys();
Counter getCounter(String var1);
Counter getCounter(String var1, long var2);
/** @deprecated */
@Deprecated
KeyIterator getKeyIterator(InetSocketAddress var1) throws MemcachedException, InterruptedException, TimeoutException;
void setAuthInfoMap(Map<InetSocketAddress, AuthInfo> var1);
Map<InetSocketAddress, AuthInfo> getAuthInfoMap();
Map<String, AuthInfo> getAuthInfoStringMap();
long decr(String var1, long var2, long var4, long var6, int var8) throws TimeoutException, InterruptedException, MemcachedException;
long incr(String var1, long var2, long var4, long var6, int var8) throws TimeoutException, InterruptedException, MemcachedException;
String getName();
void setName(String var1);
Queue<ReconnectRequest> getReconnectRequestQueue();
void setFailureMode(boolean var1);
boolean isFailureMode();
void setKeyProvider(KeyProvider var1);
int getTimeoutExceptionThreshold();
void setTimeoutExceptionThreshold(int var1);
void invalidateNamespace(String var1) throws MemcachedException, InterruptedException, TimeoutException;
void invalidateNamespace(String var1, long var2) throws MemcachedException, InterruptedException, TimeoutException;
void endWithNamespace();
void beginWithNamespace(String var1);
<T> T withNamespace(String var1, MemcachedClientCallable<T> var2) throws MemcachedException, InterruptedException, TimeoutException;
}