当前位置:网站首页>利用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;
}
}
实现效果如下:
边栏推荐
- 嵌入式数据库开发编程(零)
- Leetcode word search (backtracking method)
- Yolov5 ajouter un mécanisme d'attention
- Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
- Research on the value of background repeat of background tiling
- 一个新的微型ORM开源框架
- [to be continued] [UE4 notes] L1 create and configure items
- Web APIs DOM节点
- [转]MySQL操作实战(一):关键字 & 函数
- Download xftp7 and xshell7 (official website)
猜你喜欢
随机推荐
Stm32cubemx (8): RTC and RTC wake-up interrupt
[binary search] 69 Square root of X
Cocos progress bar progresstimer
The difference between heap and stack
Collapse of adjacent vertical outer margins
Haut OJ 1357: lunch question (I) -- high precision multiplication
[turn to] MySQL operation practice (I): Keywords & functions
Generate filled text and pictures
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Pointnet++的改进
嵌入式数据库开发编程(五)——DQL
cocos_ Lua listview loads too much data
Django reports an error when connecting to the database. What is the reason
Haut OJ 1243: simple mathematical problems
Yolov5 ajouter un mécanisme d'attention
UE fantasy engine, project structure
[binary search] 34 Find the first and last positions of elements in a sorted array
Solon Logging 插件的添加器级别控制和日志器的级别控制
被舆论盯上的蔚来,何时再次“起高楼”?
Fragment addition failed error lookup