memcached安裝使用(mac)

1、安裝 memcachedjava

  1. 安裝前,能夠先查找一下,看看有沒有: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中使用

  1. 由於我使用的是父子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);
    		}
    
    	}
相關文章
相關標籤/搜索