1、安装 memcachedjava
安装前,能够先查找一下,看看有没有:mysql
brew search memcache
返回结果:git
libmemcached memcache-top memcached memcacheq
说明和关键字memcache相关的有上面这四个,这样就确认了,有咱们须要的东西,第一个是客户端,第三个是服务器。github
先装服务器:web
brew install memcached
安装日志:spring
==> Installing memcached dependency: libevent ==> Downloading https://github.com/downloads/libevent/libevent/libevent-2.0.21-s ######################################################################## 100.0% ==> ./configure --disable-debug-mode --prefix=/usr/local/Cellar/libevent/2.0.21 ==> make ==> make install � /usr/local/Cellar/libevent/2.0.21: 48 files, 1.8M, built in 84 seconds ==> Installing memcached ==> Downloading http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz ######################################################################## 100.0% ==> ./configure --prefix=/usr/local/Cellar/memcached/1.4.15 --disable-coverage ==> make install ==> Caveats To have launchd start memcached at login: ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAgents Then to load memcached now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist Or, if you don't want/need launchctl, you can just run: /usr/local/opt/memcached/bin/memcached ==> Summary � /usr/local/Cellar/memcached/1.4.15: 10 files, 176K, built in 8 seconds
从上面安装日志,能够看出:sql
安装 memcached 前,先安装了其所依赖的 libevent 库 下载的libevent和memcached,被安装到/usr/local/Cellar下面,可是又自动在/usr/local/bin下面创建了软链接,方便使用。 安装后能够查看安装的结果:mongodb
$which memcached /usr/local/bin/memcached $ memcached -h memcached 1.4.15
... 步骤二:安装 libmemcached 继续安装客户端库:apache
$ brew install libmemcached ==> Downloading https://launchpad.net/libmemcached/1.0/1.0.16/+download/libmemca ######################################################################## 100.0% ==> ./configure --prefix=/usr/local/Cellar/libmemcached/1.0.16 ==> make install � /usr/local/Cellar/libmemcached/1.0.16: 110 files, 1.4M, built in 108 seconds
步骤三:启动服务器缓存
先默认参数启动吧:
$ /usr/local/bin/memcached -d
步骤四:测试 清单 3.
启动 memcached
./memcached -d -m 2048 -l 10.0.5.103 -p 11211 这会以守护程序的形式启动 memcached(-d),为其分配 2GB 内存(-m 2048),并指定监听 localhost,即端口 11211。您能够根据须要修改这些值,但以上设置足以完成本文中的练习。接下来,您须要链接到 memcached。您将使用一个简单的 telnet 客户机链接到 memcached 服务器。
2、在springboot中使用
由于我使用的是父子maven结构
父maven配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tom.heliming.cloud</groupId> <artifactId>cloud-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <!--<module>oracle</module>--> <!--<module>mongodb</module>--> <!--<module>okhttp</module>--> <!--<module>netty-demo</module>--> <!--<module>mysqldemo</module>--> <!--<module>rabbitmq</module>--> <module>memcache</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <java.version>1.8</java.version> </properties> <dependencies> <!-- Spring Boot相关依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <!-- <scope>test</scope>--> </dependency> </dependencies> </project>
子maven配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-demo</artifactId> <groupId>tom.heliming.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>tom.heliming.memcache</groupId> <artifactId>memcache</artifactId> <dependencies> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.whalin/Memcached-Java-Client --> <dependency> <groupId>com.whalin</groupId> <artifactId>Memcached-Java-Client</artifactId> <version>3.0.0</version> </dependency> </dependencies> <!-- 打包设置启动springboot的main函数 --> <build> <finalName>memcached</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
#memcached启动 #memcached -d -m 2048 -l 10.0.5.103 -p 11211 ## Memcache 配置 ## memcache: servers: 10.0.5.103:11211 failover: true initConn: 100 minConn: 20 maxConn: 1000 maintSleep: 50 nagel: false socketTO: 3000 aliveCheck: true
app.java
package top; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/8/22 10:32 AM */ @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class,args); } }
MemcacheConfiguration.java
package top.conf; /** * description: 注入MemCachedClient * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/8/22 10:32 AM */ import com.whalin.MemCached.MemCachedClient; import com.whalin.MemCached.SockIOPool; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MemcacheConfiguration { @Value("${memcache.servers}") private String[] servers; @Value("${memcache.failover}") private boolean failover; @Value("${memcache.initConn}") private int initConn; @Value("${memcache.minConn}") private int minConn; @Value("${memcache.maxConn}") private int maxConn; @Value("${memcache.maintSleep}") private int maintSleep; @Value("${memcache.nagel}") private boolean nagel; @Value("${memcache.socketTO}") private int socketTO; @Value("${memcache.aliveCheck}") private boolean aliveCheck; @Bean public SockIOPool sockIOPool () { SockIOPool pool = SockIOPool.getInstance(); pool.setServers(servers); pool.setFailover(failover); pool.setInitConn(initConn); pool.setMinConn(minConn); pool.setMaxConn(maxConn); pool.setMaintSleep(maintSleep); pool.setNagle(nagel); pool.setSocketTO(socketTO); pool.setAliveCheck(aliveCheck); pool.initialize(); return pool; } @Bean public MemCachedClient memCachedClient(){ return new MemCachedClient(); } }
HelloController.java
package top.controller; import com.whalin.MemCached.MemCachedClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/8/22 10:38 AM */ @RestController @RequestMapping public class HelloController { @Autowired private MemCachedClient memCachedClient; @RequestMapping("/memcahed") public String hello() throws InterruptedException { // 放入缓存 boolean flag = memCachedClient.set("a", 2); // 取出缓存 Object a = memCachedClient.get("a"); System.out.println(a); // 3s后过时 memCachedClient.set("b", "2", new Date(30)); Object b = memCachedClient.get("b"); System.out.println(b); Thread.sleep(30); b = memCachedClient.get("b"); System.out.println(a); System.out.println(b); return "memcachedok"; } }
ApplicationTests.java
/** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/8/22 10:34 AM */ import com.whalin.MemCached.MemCachedClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; import top.App; /** * set与add在key不存在时效果一致,add在key存在时不会成功。 * set与replace在key存在时效果一致,replace在key不存在不会成功。 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class ApplicationTests { @Autowired private MemCachedClient memCachedClient; @Test public void contextLoads() throws InterruptedException{ // 放入缓存 boolean flag = memCachedClient.set("a", 1); // 取出缓存 Object a = memCachedClient.get("a"); System.out.println(a); // 3s后过时 memCachedClient.set("b", "2", new Date(3000)); Object b = memCachedClient.get("b"); System.out.println(b); Thread.sleep(3000); b = memCachedClient.get("b"); System.out.println(a); System.out.println(b); } }