什么是memcached:
* memcached是一个基于内存的缓存中间件,是基于key -value进行存储;
* memcached是一个多核缓存;
* memcached对比redis的优缺点
* memcached基于key -value,而redis基于hash,所以在内存的利用率更高效,redis的内存使用率会高于memcached
* 性能方面的话,redis使用的是单核,而memcached是多核,在100k以上的数据memcached会优于redis
* 数据结构,memcached仅支持key -value形式,而redis除了支持key -value外还支持hash set list zset等数据格式,在存储方式方面要优于memcached
* redis的数据是可以通过aof rdb的方法进行持久化到本地硬盘,而memcached只存储在内存中,数据容易丢失;
使用方法:
* 安装方式:基于windows
* 下载64位 http://static.runoob.com/download/memcached-win64-1.4.4-14.zip
* 安装 c:\memcached\memcached.exe -d install
* 配置缓存池的大小最大为512M "c:\memcached\memcached.exe" -d runservice -m 512
* 启动 c:\memcached\memcached.exe -d start
* 停止 c:\memcached\memcached.exe -d stop
* 安装为服务自启动: schtasks /create /sc onstart /tn memcached /tr "'c:\memcached\memcached.exe' -m 512"
* 基于linux https://www.runoob.com/memcached/memcached-install.html
* 配置文件,memcached的默认端口号为11211
server.port=7000
#Mysql数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
logging.level.root=info
memcache.servers=127.0.0.1:11211
#初始连接数
memcached.initconn=100
#最大连接数
memcacaed.maxconn=80
#最小连接数
memcached.minconn=10
#连接超时时间
memcached.socketTo=3000
memcached.alivecheck=true
memcached.failover=true
package com.sunyw.xyz.config;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class MemcachedConfig {
@Value("${memcache.servers}")
private String[] server;
@Value("${memcached.initconn}")
private int initconn;
@Value("${memcacaed.maxconn}")
private int maxconn;
@Value("${memcached.minconn}")
private int minconn;
@Value("${memcached.failover}")
private boolean failover;
@Value("${memcached.alivecheck}")
private boolean alivecheck;
@Value("${memcached.socketTo}")
private int socketTo;
@Bean
public SockIOPool init() {
SockIOPool sockIOPool = SockIOPool.getInstance();
log.info("<开始初始化连接池>");
sockIOPool.setAliveCheck(alivecheck);
sockIOPool.setFailover(failover);
sockIOPool.setInitConn(initconn);
sockIOPool.setMaxConn(maxconn);
sockIOPool.setMinConn(minconn);
sockIOPool.setServers(server);
sockIOPool.setSocketTO(socketTo);
sockIOPool.initialize();
log.info("<连接池初始化完毕>");
return sockIOPool;
}
@Bean
@ConditionalOnBean(SockIOPool.class)
public MemCachedClient initclient() {
log.info("<获取MemCachedClient连接完毕>");
return new MemCachedClient();
}
}
demo
package com.sunyw.xyz.mem;
import com.whalin.MemCached.MemCachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
@RequestMapping("/v1/api")
public class MemcachedTest {
@Autowired
private MemCachedClient memCachedClient;
@PostMapping("/test1")
public String test1() {
memCachedClient.set("name", "aaa");
return memCachedClient.get("name").toString();
}
@PostMapping("/test2")
public String test2() throws InterruptedException {
//设置过期时间为2s,设置一个key的值为true,过期时间设置为2s
memCachedClient.set("boolean", "true", new Date(2000));
Thread.sleep(2000);
return memCachedClient.get("boolean").toString();
}
@PostMapping("/test3")
public String test3() throws InterruptedException {
//添加一个key的值过期时间为2s
memCachedClient.add("boolean", "true", new Date(2000));
Thread.sleep(2000);
return memCachedClient.get("boolean").toString();
}
@PostMapping("/test4")
public boolean test4() throws InterruptedException {
//删除一个key为name的值
return memCachedClient.delete("name");
}
}