当前位置:网站首页>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 :

边栏推荐
- What is the agile proportion of PMP Exam? Dispel doubts
- [to be continued] [depth first search] 547 Number of provinces
- Acwing 4300. Two operations
- Introduction to memory layout of FVP and Juno platforms
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- A three-dimensional button
- Add level control and logger level control of Solon logging plug-in
- Yolov5 adds attention mechanism
- [allocation problem] 135 Distribute candy
- win10虚拟机集群优化方案
猜你喜欢

远程升级怕截胡?详解FOTA安全升级

一个新的微型ORM开源框架
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
![[转]MySQL操作实战(三):表联结](/img/70/20bf9b379ce58761bae9955982a158.png)
[转]MySQL操作实战(三):表联结

Acwing 4300. Two operations

浅谈JVM(面试常考)

Heap sort summary
![[depth first search] 695 Maximum area of the island](/img/08/cfff4aec667216e4f146205a12c13f.jpg)
[depth first search] 695 Maximum area of the island

win10虚拟机集群优化方案

Grail layout and double wing layout
随机推荐
Quick sort summary
Shell Sort
Count sort
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
FVP和Juno平台的Memory Layout介绍
《动手学深度学习》学习笔记
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Haut OJ 1321: mode problem of choice sister
Grail layout and double wing layout
Do a small pressure test with JMeter tool
Haut OJ 1352: string of choice
A three-dimensional button
Acwing 4301. Truncated sequence
注解与反射
[转]:Apache Felix Framework配置属性
Pointnet++学习
[turn]: Apache Felix framework configuration properties
利用HashMap实现简单缓存
远程升级怕截胡?详解FOTA安全升级
[merge array] 88 merge two ordered arrays