Example Method for SpringBoot to Integrate Memcached

  • 2021-07-26 07:35:22
  • OfStack

Introduction to Memcached

Memcached is a high performance distributed memory object caching system for dynamic Web applications to reduce database load. It reduces the number of database reads by caching data and objects in memory, thus improving the speed of dynamic, database-driven websites. Memcached hashmap based on 1 store key/value pair. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.

Because Spring Boot does not provide a corresponding build package for Memcached, we need to integrate it ourselves. The official Java client Spymemcached is a better choice.

Introduction to Spymemcached

Spymemcached was first developed by Dustin Sallings, and Dustin later founded Couchbase (formerly NorthScale) with others as the chief architect. Joined Google in 2014.

Spymemcached is an asynchronous, single-threaded Memcached client developed by Java and implemented by NIO. Spymemcached is a popular Java client library of Memcached, which has excellent performance and is widely used in Java + Memcached projects.

Dependent configuration

Add dependencies

Add a reference to spymemcached in the pomx package


<dependency>
 <groupId>net.spy</groupId>
 <artifactId>spymemcached</artifactId>
 <version>2.12.2</version>
</dependency>

Add Configuration


memcache.ip=192.168.0.161
memcache.port=11211

Configure the Ip address and port of memcache, respectively.

Setting Configuration Objects

Create MemcacheSource Receive Configuration Information


@Component
@ConfigurationProperties(prefix = "memcache")
public class MemcacheSource {

 private String ip;

 private int port;

 public String getIp() {
  return ip;
 }

 public void setIp(String ip) {
  this.ip = ip;
 }

 public int getPort() {
  return port;
 }

 public void setPort(int port) {
  this.port = port;
 }
}

@ ConfigurationProperties (prefix = "memcache") means that the corresponding configuration file is loaded into the property with memcache. * enabled.

Initialize MemcachedClient

We use the content of the previous section Spring Boot 2 (7): Spring Boot how to initialize resources at project start-up, and use CommandLineRunner to configure MemcachedClient at project start-up.


@Component
public class MemcachedRunner implements CommandLineRunner {
 protected Logger logger = LoggerFactory.getLogger(this.getClass());

 @Resource
 private MemcacheSource memcacheSource;

 private MemcachedClient client = null;

 @Override
 public void run(String... args) throws Exception {
  try {
   client = new MemcachedClient(new InetSocketAddress(memcacheSource.getIp(),memcacheSource.getPort()));
  } catch (IOException e) {
   logger.error("inint MemcachedClient failed ",e);
  }
 }

 public MemcachedClient getClient() {
  return client;
 }

}

Test use


@RunWith(SpringRunner.class)
@SpringBootTest
public class RepositoryTests {

 @Resource
 private MemcachedRunner memcachedRunner;

 @Test
 public void testSetGet() {
 MemcachedClient memcachedClient = memcachedRunner.getClient();
 memcachedClient.set("testkey",1000,"666666");
 System.out.println("*********** "+memcachedClient.get("testkey").toString());
 }

}

In use, first test and insert an key as testkey, 1000 as the expiration time unit is milliseconds, and the last "666666" is the corresponding value of key.

Execute the test case testSetGet, and the console output:

*********** 666666

Indicates that the test was successful.


Related articles: