What is the cache mechanism of memcached?

 To use the Memcached cache mechanism in Java, you need to use the Java Memcached client library. Memcached is a high-performance distributed memory caching system that can be used to cache commonly used data, reduce the load on back-end storage such as databases, and improve system response speed.

  Before starting, make sure the Memcached server is installed and started. Then, we need to add the dependency of the Memcached Java client library. Commonly used Java client libraries include Spymemcached and XMemcached. In this example we use Spymemcached.

  1. Add dependencies:

  For Maven projects, add the following dependencies to the pom.xml file:

<dependency>
    <groupId>net.spy</groupId>
    <artifactId>spymemcached</artifactId>
    <version>2.12.3</version> <!-- 请根据最新版本进行替换 -->
</dependency>

  2. Write code demo:

  Next, the author uses a simple Java code to demonstrate how to use Memcached for caching.

import net.spy.memcached.MemcachedClient;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.MemcachedClientIF;
import java.net.InetSocketAddress;
import java.util.concurrent.Future;

public class MemcachedCacheExample {

    public static void main(String[] args) {
        try {
            // 连接到本地Memcached服务器,端口默认为11211
            MemcachedClientIF memcachedClient = new MemcachedClient(new InetSocketAddress("localhost", 11211));

            // 缓存的键值对
            String key = "example_key";
            String value = "Hello, Memcached!";

            // 将数据存入缓存,缓存过期时间为60秒
            Future<Boolean> setResult = memcachedClient.set(key, 60, value);
            System.out.println("Set Result: " + setResult.get());

            // 从缓存中获取数据
            Object cachedValue = memcachedClient.get(key);
            System.out.println("Cached Value: " + cachedValue);

            // 等待一段时间,使缓存过期
            Thread.sleep(60000);

            // 再次尝试获取数据
            Object expiredValue = memcachedClient.get(key);
            System.out.println("Expired Value: " + expiredValue);

            // 关闭连接
            memcachedClient.shutdown();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  In the above code, we created a MemcachedClient to connect to the local Memcached server (specify the address and port through InetSocketAddress). Then, we use the set method to store the key-value pair in the cache, and specify the cache expiration time (in seconds). Next, we use the get method to get data from the cache.

  After the code runs, we can observe that "Hello, Memcached!" will be returned when the data is fetched for the first time, but null will be returned after waiting for 60 seconds to fetch the data again, because the cache has expired.

  Please note that in practical applications, we usually store commonly used, infrequently changed data in Memcached instead of hardcoding keys and values ​​like in the above example. In addition, Memcached also supports the storage of various data structures, such as hash tables, lists, etc., and more usages can be learned and explored according to specific needs.

Guess you like

Origin blog.csdn.net/Blue92120/article/details/131846156