当前位置:网站首页>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 :
边栏推荐
猜你喜欢
Use of snippets in vscode (code template)
GBase数据库助力湾区数字金融发展
The present is a gift from heaven -- a film review of the journey of the soul
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Bucket sort
Fragment addition failed error lookup
object serialization
Count sort
Reader writer model
随机推荐
Web APIs DOM node
A new micro ORM open source framework
[merge array] 88 merge two ordered arrays
Drawing dynamic 3D circle with pure C language
sync.Mutex源码解读
SAP-修改系统表数据的方法
kubeadm系列-02-kubelet的配置和启动
Download xftp7 and xshell7 (official website)
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Shell Sort
Introduction to tools in TF-A
Gbase database helps the development of digital finance in the Bay Area
小程序直播+電商,想做新零售電商就用它吧!
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Research on the value of background repeat of background tiling
Research on the value of background repeat of background tiling
[interval problem] 435 Non overlapping interval
Haut OJ 1245: large factorial of CDs --- high precision factorial
Stm32cubemx (8): RTC and RTC wake-up interrupt
Programmers' experience of delivering takeout