This chapter is a tutorial on the use of Mmecached in Spring Boot. Memcached and Redis have their own advantages. This paper mainly studies how to apply integrated Mmecached in Spring Boot.
- spring boot 1.5.x/2.x
- memcached
- jdk 1.8+
Source code download of this project
1 Install memcached
It is convenient to install under window s. Just double-click the exe installation file directly.
mac Installation Using Command Line Installation
brew install libmemcached brew install memcached rew services start memcached
Note that if you encounter update... and need to wait for a long time, press the combination key command+c to cancel the update of brew and execute the command directly.
2 New Spring Boot Maven Demonstration Project
Note: This is an IDEA development tool
- File > New > Project, select Spring Initializr as shown below and click Next for the next step
- Fill in GroupId (package name) and Artifact (project name). Click Next
groupId=com.fishpro
artifactId=memcached - Check before choosing to rely on Spring Web Starter.
- The project name is set to spring-boot-study-memcached.
3 Introducing Dependent Pom
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/net.spy/spymemcached add by fishpro at 2019-08-07--> <dependency> <groupId>net.spy</groupId> <artifactId>spymemcached</artifactId> <version>2.12.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
4 Configure Memcached to initialize MemcachedClient
MemcacheConfig path src/main/java/com/fishpro/memcached/config/RedisController (path.java)
package com.fishpro.memcached.config; import net.spy.memcached.MemcachedClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; import java.net.InetSocketAddress; @EnableCaching @Configuration public class MemcacheConfig extends CachingConfigurerSupport { @Value("${memcached.port}") private Integer port; @Value("${memcached.ip}") private String ip; @Bean public MemcachedClient memcachedClient(){ try { return new MemcachedClient(new InetSocketAddress(ip,port)); }catch (IOException e){ e.printStackTrace(); return null; } } }
5 Write RestController test code
Not used to unit testing. Here's the test code for RestController
Method | Effect |
---|---|
set(key,value) | Add a value to the key and replace it if stored |
add(key,value) | Add a value to the key without replacing it if it exists |
replace(key,value) | Replace the value of the cache key with value |
delete(key) | Delete the value of the cache key |
Note the effective time unit second
@RestController public class UserController { @Autowired private MemcachedClient memcachedClient; @RequestMapping("/test") public String test(){ System.out.println("======set/get Mode demonstration==============================="); memcachedClient.set("FPCACHE",3,"THIS IS TEST This is the test."); System.out.println("Setting and Reading FPCACHE value:"+memcachedClient.get("FPCACHE")); memcachedClient.set("FPCACHE",3,"Use SET Cache added to an existing value"); System.out.println("Read again FPCACHE value:"+memcachedClient.get("FPCACHE")); System.out.println("======add Mode demonstration==============================="); memcachedClient.add("FPCACHE",3,"Use ADD Cache added to an existing value"); System.out.println("Read again FPCACHE value:"+memcachedClient.get("FPCACHE")); memcachedClient.add("FPCACHE2",3,"Use ADD Add to new cache key FPCACHE2 in"); System.out.println("Read again FPCACHE2 value:"+memcachedClient.get("FPCACHE2")); System.out.println("======replace Mode demonstration==============================="); memcachedClient.replace("FPCACHE",3,"Use Replace replace FPCACHE Key corresponding cache value"); System.out.println("replace Mode reading FPCACHE value:"+memcachedClient.get("FPCACHE")); try { Thread.sleep(3001); }catch (Exception ex){} System.out.println("3 Get the cache again after seconds FPCACHE: "+memcachedClient.get("FPCACHE")); System.out.println("======delete Mode demonstration==============================="); memcachedClient.delete("FPCACHE"); System.out.println("replace Mode reading FPCACHE value:"+memcachedClient.get("FPCACHE")); return ""; } }
6 Running examples
Right-click Memcached Application and select Run Memcached Application to enter http://localhost:8080/test in the browser
Source code download of this project
Reference resources
Official Memcached github