当前位置:网站首页>利用HashMap实现简单缓存
利用HashMap实现简单缓存
2022-07-05 05:18:00 【心寒丶】
最近有一个需求需要用到简单缓存技术,由于各种原因,无法使用redis,无法使用其他缓存框架技术,因此简单做了一个缓存实现,大致原理如下:
定义一个缓存初始化类(CacheInit),用来保存数据和存活时间,然后请求获取数据的时候判断map中是否存在值,如果存在且在存活时间之内,则直接返回,如果不存在或者存在且超过存活时间,则重新获取值,然后再放入map,更新map中的值及存活时间。代码如下:
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author :Hanjun
* @date 2022/7/4
*/
@RestController
@RequestMapping("/cacheTest")
public class CacheTestControl {
private static Map<String, Object> finalQYMap = new HashMap<>();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@RequestMapping(value = "/getToken")
public String test(){
// 全局缓冲中有效时,优先取缓冲的
if (finalQYMap.size() > 0 && finalQYMap.get("test") != null) {
CacheInit init = (CacheInit) finalQYMap.get("test");
Calendar applyTime = Calendar.getInstance();
applyTime.setTime(init.getApplyTime());
Calendar nowTime = Calendar.getInstance();
nowTime.setTime(new Date());
if(nowTime.compareTo(applyTime) < 0) {
System.out.println("缓存获取时间:"+dateFormat.format(new Date()));
return init.getAccessToken();
}
}
String token = "testToken123456";
CacheInit init = new CacheInit();
init.setAccessToken(token);
Date now = new Date();
Calendar applyTime = Calendar.getInstance();
applyTime.setTime(now);
//设置缓存10秒
applyTime.add(Calendar.SECOND, 10);
init.setApplyTime(applyTime.getTime());
System.out.println("非缓存获取token时间:"+dateFormat.format(new Date()));
finalQYMap.put("test", init);
return token;
}
}
package com.sinosoft.controller;
import java.util.Date;
/**
* @author :Hanjun
* @date 2022/7/4
*/
public class CacheInit {
private String accessToken;
/** 获取时间*/
private Date applyTime;
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public Date getApplyTime() {
return applyTime;
}
public void setApplyTime(Date applyTime) {
this.applyTime = applyTime;
}
}
实现效果如下:
边栏推荐
- PMP考生,请查收7月PMP考试注意事项
- Transport connection management of TCP
- Embedded database development programming (V) -- DQL
- Unity sends messages and blocks indecent words
- Ue4/ue5 illusory engine, material part (III), material optimization at different distances
- Haut OJ 1347: addition of choice -- high progress addition
- [trans]: spécification osgi
- Basic knowledge points of dictionary
- [to be continued] [depth first search] 547 Number of provinces
- Yolov5 adds attention mechanism
猜你喜欢
Download and use of font icons
Merge sort
[turn to] MySQL operation practice (III): table connection
Embedded database development programming (VI) -- C API
Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
嵌入式数据库开发编程(六)——C API
Web APIs DOM节点
Heap sort summary
[to be continued] [UE4 notes] L1 create and configure items
win10虚拟机集群优化方案
随机推荐
The difference between heap and stack
Unity check whether the two objects have obstacles by ray
Haut OJ 1352: string of choice
LeetCode之單詞搜索(回溯法求解)
Fragment addition failed error lookup
Chinese notes of unit particle system particle effect
PMP考生,请查收7月PMP考试注意事项
[turn]: OSGi specification in simple terms
Haut OJ 1218: maximum continuous sub segment sum
Solon Auth 认证框架使用演示(更简单的认证框架)
[turn to] MySQL operation practice (I): Keywords & functions
Solon 框架如何方便获取每个请求的响应时间?
Simple HelloWorld color change
Data is stored in the form of table
Unity find the coordinates of a point on the circle
The present is a gift from heaven -- a film review of the journey of the soul
PMP candidates, please check the precautions for PMP examination in July
嵌入式数据库开发编程(六)——C API
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
JVM call not used once in ten years