当前位置:网站首页>利用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;
}
}
实现效果如下:
边栏推荐
- Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
- Embedded database development programming (zero)
- Leetcode word search (backtracking method)
- Learning notes of "hands on learning in depth"
- cocos2dx_ Lua particle system
- Programmers' experience of delivering takeout
- Grail layout and double wing layout
- Embedded database development programming (V) -- DQL
- Bucket sort
- Transport connection management of TCP
猜你喜欢
随机推荐
Bubble sort summary
Haut OJ 1321: mode problem of choice sister
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
2022年上半年国家教师资格证考试
FVP和Juno平台的Memory Layout介绍
Judge the position of the monster in the role under unity3d
JVM call not used once in ten years
对象的序列化
Basic knowledge points
Haut OJ 1243: simple mathematical problems
Unity card flipping effect
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
2022/7/2 question summary
cocos2dx_ Lua card flip
Under the national teacher qualification certificate in the first half of 2022
Unity ugui source code graphic
Unity check whether the two objects have obstacles by ray
Unity sends messages and blocks indecent words
Binary search basis