当前位置:网站首页>利用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;
}
}
实现效果如下:

边栏推荐
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- Yolov5 adds attention mechanism
- [turn to] MySQL operation practice (I): Keywords & functions
- Pause and resume of cocos2dx Lua scenario
- Cocos create Jiugongge pictures
- Under the national teacher qualification certificate in the first half of 2022
- [turn]: Apache Felix framework configuration properties
- room数据库的使用
- Cocos2dx screen adaptation
- LeetCode之單詞搜索(回溯法求解)
猜你喜欢
![To be continued] [UE4 notes] L4 object editing](/img/0f/cfe788f07423222f9eed90f4cece7d.jpg)
To be continued] [UE4 notes] L4 object editing

Optimization scheme of win10 virtual machine cluster

Embedded database development programming (zero)

Merge sort

Research on the value of background repeat of background tiling

Binary search basis

django连接数据库报错,这是什么原因

Count sort

Stm32cubemx (8): RTC and RTC wake-up interrupt

Data is stored in the form of table
随机推荐
Cocos progress bar progresstimer
2022/7/1學習總結
使用命令符关闭笔记本自带键盘命令
Romance of programmers on Valentine's Day
Research on the value of background repeat of background tiling
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
UE 虚幻引擎,项目结构
Haut OJ 1316: sister choice buys candy III
[轉]: OSGI規範 深入淺出
一个新的微型ORM开源框架
Haut OJ 1218: maximum continuous sub segment sum
[binary search] 69 Square root of X
cocos_ Lua loads the file generated by bmfont fnt
《动手学深度学习》学习笔记
Grail layout and double wing layout
Lua GBK and UTF8 turn to each other
django连接数据库报错,这是什么原因
Magnifying glass effect
UE fantasy engine, project structure
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)