Interviewer: How did you implement your own caching (support high concurrent) use JDK?

Interviewer: How did you implement your own caching (support high concurrent) use JDK?

demand analysis

Projects often encounter this scenario: a shared data needs in many places, some of the data as well as the timeliness of expired automatically expire. For example, phone code, after sending the required cached, then for security considerations, the general also set the validity period expires automatically lapse. How do we achieve this function?

solution

Using prior art caching framework, such redis, ehcache. Advantages: mature, stable, powerful; drawback, the project needs to introduce corresponding framework, not a lightweight.

If you do not consider distributed only in the single-threaded or multithreaded intercropping data cache, in fact, you can own handwriting a caching tool. Here's a simple implementation of such a tool.

First on the code:

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @Author: lixk
 * @Date: 2018/5/9 15:03
 * @Description: 简单的内存缓存工具类
 */
public class Cache {
    //键值对集合
    private final static Map<String, Entity> map = new HashMap<>();
    //定时器线程池,用于清除过期缓存
    private final static ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

    /**
     * 添加缓存
     *
     * @param key  键
     * @param data 值
     */
    public synchronized static void put(String key, Object data) {
        Cache.put(key, data, 0);
    }

    /**
     * 添加缓存
     *
     * @param key    键
     * @param data   值
     * @param expire 过期时间,单位:毫秒, 0表示无限长
     */
    public synchronized static void put(String key, Object data, long expire) {
        //清除原键值对
        Cache.remove(key);
        //设置过期时间
        if (expire > 0) {
            Future future = executor.schedule(new Runnable() {
                @Override
                public void run() {
                    //过期后清除该键值对
                    synchronized (Cache.class) {
                        map.remove(key);
                    }
                }
            }, expire, TimeUnit.MILLISECONDS);
            map.put(key, new Entity(data, future));
        } else {
            //不设置过期时间
            map.put(key, new Entity(data, null));
        }
    }

    /**
     * 读取缓存
     *
     * @param key 键
     * @return
     */
    public synchronized static Object get(String key) {
        Entity entity = map.get(key);
        return entity == null ? null : entity.getValue();
    }

    /**
     * 读取缓存
     *
     * @param key 键
     *            * @param clazz 值类型
     * @return
     */
    public synchronized static <T> T get(String key, Class<T> clazz) {
        return clazz.cast(Cache.get(key));
    }

    /**
     * 清除缓存
     *
     * @param key
     * @return
     */
    public synchronized static Object remove(String key) {
        //清除原缓存数据
        Entity entity = map.remove(key);
        if (entity == null) return null;
        //清除原键值对定时器
        Future future = entity.getFuture();
        if (future != null) future.cancel(true);
        return entity.getValue();
    }

    /**
     * 查询当前缓存的键值对数量
     *
     * @return
     */
    public synchronized static int size() {
        return map.size();
    }

    /**
     * 缓存实体类
     */
    private static class Entity {
        //键值对的value
        private Object value;
        //定时器Future 
        private Future future;

        public Entity(Object value, Future future) {
            this.value = value;
            this.future = future;
        }

        /**
         * 获取值
         *
         * @return
         */
        public Object getValue() {
            return value;
        }

        /**
         * 获取Future对象
         *
         * @return
         */
        public Future getFuture() {
            return future;
        }
    }
}

This tool uses principal HashMap + timer thread pool implementation, map for storing key data, map the value is Cache internal class object Entity, Entity and contains the lifetime of the key value of a timer Future. External Cache class provides only put (key, value), put (key, value, expire), get (key), get (key, class), remove (key), size () several synchronization method.

When the addition of data when the key value, first calls the remove () method, removed the original data of the same key and the corresponding timing cancel cleanup tasks, and add new data to the map, and if the effective time is set, Add a timing corresponding to the timer thread pool cleanup tasks.

test

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @Author: lixk
 * @Date: 2018/5/9 16:40
 * @Description: 缓存工具类测试
 */
public class CacheTest {

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        String key = "id";
        //不设置过期时间
        System.out.println("***********不设置过期时间**********");
        Cache.put(key, 123);
        System.out.println("key:" + key + ", value:" + Cache.get(key));
        System.out.println("key:" + key + ", value:" + Cache.remove(key));
        System.out.println("key:" + key + ", value:" + Cache.get(key));

        //设置过期时间
        System.out.println("\n***********设置过期时间**********");
        Cache.put(key, "123456", 1000);
        System.out.println("key:" + key + ", value:" + Cache.get(key));
        Thread.sleep(2000);
        System.out.println("key:" + key + ", value:" + Cache.get(key));

        /******************并发性能测试************/
        System.out.println("\n***********并发性能测试************");
        //创建有10个线程的线程池,将1000000次操作分10次添加到线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Future[] futures = new Future[10];
        /********添加********/
        {
            long start = System.currentTimeMillis();
            for (int j = 0; j < 10; j++) {
                futures[j] = executorService.submit(() -> {
                    for (int i = 0; i < 100000; i++) {
                        Cache.put(Thread.currentThread().getId() + key + i, i, 300000);
                    }
                });
            }
            //等待全部线程执行完成,打印执行时间
            for (Future future : futures) {
                future.get();
            }
            System.out.printf("添加耗时:%dms\n", System.currentTimeMillis() - start);
        }

        /********查询********/
        {
            long start = System.currentTimeMillis();
            for (int j = 0; j < 10; j++) {
                futures[j] = executorService.submit(() -> {
                    for (int i = 0; i < 100000; i++) {
                        Cache.get(Thread.currentThread().getId() + key + i);
                    }
                });
            }
            //等待全部线程执行完成,打印执行时间
            for (Future future : futures) {
                future.get();
            }
            System.out.printf("查询耗时:%dms\n", System.currentTimeMillis() - start);
        }

        System.out.println("当前缓存容量:" + Cache.size());
    }
}

Test Results:

***********不设置过期时间**********
key:id, value:123
key:id, value:123
key:id, value:null

***********设置过期时间**********
key:id, value:123456
key:id, value:null

***********并发性能测试************
添加耗时:2313ms
查询耗时:335ms
当前缓存容量:1000000

Test program using a thread pool has 10 concurrent threads to simulate, execute a million times in total add and query operations, about the time in more than two seconds, `did a good job, 400,000 concurrent read and write per second should still meet the large most high concurrency scenarios ^ _ ^

Written in the last

  • First: watching the thumbs up, thank you for your approval;
  • ...
  • Second: hand forward, to share knowledge, to allow more people to learn;
  • ...
  • Third: remember point concerns, updated daily! ! !
  • ...

Interviewer: How did you implement your own caching (support high concurrent) use JDK?

Guess you like

Origin blog.51cto.com/14409778/2417780