Spring Boot 2.0 (eight): Spring Boot integrated Memcached

Source: Internet
Author: User
Tags time in milliseconds

Memcached Introduction

Memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. 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 officially launched Java client spymemcached is one of the better choices.

spymemcached Introduction

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

Spymemcached is a Java-developed asynchronous, single-threaded Memcached client that is implemented using NIO. Spymemcached is a popular Java client library for Memcached, which performs well and is widely used in Java + Memcached projects.

Dependency configuration

Add dependency

Add a reference to the 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.161memcache.port=11211

Configure the Ip address and port of the memcache, respectively.

Setting Up 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")The memcache.* corresponding configuration file will be loaded into the attribute for opening.

Initiate initialization of Memcachedclient

We use the contents of the previous section Spring Boot 2.0 (vii): How spring boot solves the initialization of resources at project startup, and uses Commandlinerunner to configure memcachedclient when the project is started.

@Componentpublic 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 using
@RunWith(SpringRunner.class)@SpringBootTestpublic 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());  }}

Using the first Test insert a key is TestKey, 1000 is the expiration time in milliseconds, and the last "666666" is the value corresponding to the key.

To execute the test case testsetget, the console outputs the contents:

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

Indicates that the test was successful.

Sample Code-github

Sample code-Cloud Code

Spring Boot 2.0 (eight): Spring Boot integrated Memcached

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.