【无标题】Java 缓存中间件系统,该中间件的缓存写入后,经过指定时间缓存要自动失效。 在读取缓存时,如果缓存不存在,需要异步调用 AService.get(K) 接口获取实时数据更新 到缓存中

本文介绍了如何设计一个Java缓存中间件系统,其中缓存写入后会自动在指定时间后失效。在读取时,如果缓存不存在,会异步调用AService.get方法获取最新数据并更新缓存。使用了Future和ExecutorService进行异步操作,同时包含定时任务管理缓存失效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计一个 Java 缓存中间件系统,该中间件的缓存写入后,经过指定时间缓存要自动失效。 在读取缓存时,如果缓存不存在,需要异步调用 AService.get(K) 接口获取实时数据更新 到缓存中。

代码框架(供候选人作答)

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

public class CacheMiddleware<K, V> {
private Map<K, V> cacheMap;
private Map<K, Future> futureMap;
private ExecutorService executorService;
private AService aService;
private long expirationTime; // 缓存过期时间,单位:毫秒

public CacheMiddleware(AService aService, long expirationTime) {
this.cacheMap = new HashMap<>();
this.futureMap = new HashMap<>();
this.executorService = Executors.newFixedThreadPool(10);
this.aService = aService;
this.expirationTime = expirationTime;
}

public V get(K key) {

}

public void put(K key, V value) {
}

public void remove(K key) {
}

public void update(K key) {
}

private void scheduleExpiration(K key) {
}
}

上代码

以下是一个可能的实现框架,其中包含异步调用 AService.get(K) 接口来更新缓存:

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

public class CacheMiddleware<K, V> {
    private Map<K, V> cacheMap;
    private Map<K, Future<V>> futureMap;
    private ExecutorService executorService;
    private AService aService;
    private long expirationTime; // 缓存过期时间,单位:毫秒

    public CacheMiddleware(AService aService, long expirationTime) {
        this.cacheMap = new HashMap<>();
        this.futureMap = new HashMap<>();
        this.executorService = Executors.newFixedThreadPool(10);
        this.aService = aService;
        this.expirationTime = expirationTime;
    }

    public V get(K key) {
        V value = cacheMap.get(key);

        if (value == null) {
            // 缓存不存在,异步调用 AService.get(K) 接口获取实时数据
            Future<V> future = futureMap.get(key);

            if (future == null) {
                // 使用 Callable 包装 AService.get(K) 接口的调用
                Callable<V> callable = () -> aService.get(key);
                future = executorService.submit(callable);

                // 将 Future 放入 futureMap,以便后续获取结果
                futureMap.put(key, future);
            }

            try {
                // 获取异步调用的结果,设置超时时间
                value = future.get(100, TimeUnit.MILLISECONDS);
                
                // 更新缓存
                put(key, value);

                // 移除对应的 Future
                futureMap.remove(key);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                // 异常处理
                e.printStackTrace();
            }
        }

        return value;
    }

    public void put(K key, V value) {
        cacheMap.put(key, value);

        // 启动定时任务,用于在指定时间后移除缓存
        scheduleExpiration(key);
    }

    public void remove(K key) {
        cacheMap.remove(key);
    }

    public void update(K key) {
        // 异步更新缓存
        Future<V> future = executorService.submit(() -> aService.get(key));

        // 将 Future 放入 futureMap,以便后续获取结果
        futureMap.put(key, future);
    }

    private void scheduleExpiration(K key) {
        // 定时任务,用于在指定时间后移除缓存
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        scheduledExecutorService.schedule(() -> {
            cacheMap.remove(key);
            futureMap.remove(key);
        }, expirationTime, TimeUnit.MILLISECONDS);
        scheduledExecutorService.shutdown();
    }
}

interface AService {
    <K, V> V get(K key);
}

在这个实现中,使用了 Future 来处理异步调用,同时在获取缓存时,如果缓存不存在会异步调用 AService.get(K) 接口获取实时数据。同时,通过定时任务来在指定时间后移除缓存。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星光不问赶路人-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值