当前位置:网站首页>Using HashMap to realize simple cache
Using HashMap to realize simple cache
2022-07-05 05:25:00 【Cold heart】
Recently, there is a need to use simple caching technology , For various reasons , Can't use redis, Other caching framework technologies cannot be used , Therefore, a simple cache implementation is made , The general principle is as follows :
Define a cache initialization class (CacheInit), Used to save data and survival time , Then judge when requesting data map Whether there is a value in , If it exists and is within its lifetime , Then return directly , If it does not exist or exists and exceeds the survival time , Then get the value again , And then put in map, to update map Value in and survival time . The code is as follows :
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(){
// When valid in the global buffer , Take the buffered first
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(" Cache fetch time :"+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);
// Set the cache 10 second
applyTime.add(Calendar.SECOND, 10);
init.setApplyTime(applyTime.getTime());
System.out.println(" Non cached fetch token Time :"+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;
/** Acquisition time */
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;
}
}
The effect is as follows :
边栏推荐
- FVP和Juno平台的Memory Layout介绍
- object serialization
- Use the command character to close the keyboard command of the notebook
- [转]: OSGI规范 深入浅出
- Generate filled text and pictures
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- Grail layout and double wing layout
- 远程升级怕截胡?详解FOTA安全升级
- SAP-修改系统表数据的方法
- To the distance we have been looking for -- film review of "flying house journey"
猜你喜欢
小程序直播+電商,想做新零售電商就用它吧!
2022年上半年国家教师资格证考试
Shell Sort
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
[trans]: spécification osgi
YOLOv5-Shufflenetv2
Pointnet++学习
[轉]: OSGI規範 深入淺出
National teacher qualification examination in the first half of 2022
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
随机推荐
Embedded database development programming (zero)
使用Electron开发桌面应用
Solon 框架如何方便获取每个请求的响应时间?
FVP和Juno平台的Memory Layout介绍
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
A three-dimensional button
[es practice] use the native realm security mode on es
On-off and on-off of quality system construction
Transport connection management of TCP
质量体系建设之路的分分合合
How can the Solon framework easily obtain the response time of each request?
YOLOv5添加注意力機制
Web APIs DOM节点
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Haut OJ 1347: addition of choice -- high progress addition
Reader writer model
第六章 数据流建模—课后习题
PMP考生,请查收7月PMP考试注意事项