java静态map缓存_java使用静态hashmap实现简单缓存(示例代码)

更新时间:2023-07-17 01:05:29 阅读: 评论:0

java静态map缓存_java使⽤静态hashmap实现简单缓存(⽰
例代码)
因项⽬需要的数据查询较多,数量⼤,所以要⽤到缓存,在未引⽤redis的情况下,仅仅只做⼀个速度的提升
1、Cache类
public class Cache {
private String key;//缓存ID,根据iD往hashmap存放值
private E value;//缓存数据 ,需要缓存的对象实体
private long timeOut;//更新时间安徽安庆大学
private boolean expired; //是否过期
public Cache() {
super();
}
public Cache(String key, E value, long timeOut, boolean expired) {
this.key = key;
this.value = value;
this.timeOut = timeOut+System.currentTimeMillis();
}
public String getKey() {
return key;
}
public long getTimeOut() {
return timeOut;
}
sometimes的用法public E getValue() {
return value;
}
public void tKey(String string) {
key = string;
}
aviv nevo
public void tTimeOut(long l) {
timeOut = l;
}
public void tValue(E object) {
value = object;
}
public boolean isExpired() {
return expired;
}
第82届奥斯卡public void tExpired(boolean b) {
expired = b;
}
}
2、CacheManager类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
/**
* Created by admin on 2018/1/2.
*/
public class CacheManager {
private static HashMap cacheMap = new HashMap(); //单实例构造⽅法
private CacheManager() {
super();
}
/
/获取布尔值的缓存
public static boolean getSimpleFlag(String key){
try{
return (Boolean) (key);
}catch(NullPointerException e){
return fal;
}
}
public static long getServerStartdt(String key){
try {
return ((key);
} catch (Exception ex) {
return 0;
}
}
//设置布尔值的缓存
public synchronized static boolean tSimpleFlag(String key,boolean flag){
if (flag && getSimpleFlag(key)) {//假如为真不允许被覆盖
return fal;
}el{
cacheMap.put(key, flag);
return true;
}
}
public synchronized static boolean tSimpleFlag(String key,long rverbegrundt){
if ((key) == null) {
cacheMap.put(key,rverbegrundt);
return true;
}el{
return fal;
}
}
//得到缓存。同步静态⽅法
private synchronized static Cache getCache(String key) {
return (Cache) (key);
}
//判断是否存在⼀个缓存
private synchronized static boolean hasCache(String key) {
ainsKey(key);
}
//清除所有缓存
public synchronized static void clearAll() {
cacheMap.clear();
}
/
/清除某⼀类特定缓存,通过遍历HASHMAP下的所有对象,来判断它的KEY与传⼊的TYPE是否匹配
public synchronized static void clearAll(String type) {
Iterator i = Set().iterator();
高中英语必修5课文
String key;
ArrayList arr = new ArrayList();
try {
while (i.hasNext()) {
java.util.Map.Entry entry = (java.util.Map.Entry) i.next();
key = (String) Key();
if (key.startsWith(type)) { //如果匹配则删除掉
arr.add(key);
}
}
for (int k = 0; k < arr.size(); k++) {
(k));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
//清除指定的缓存
public synchronized static void clearOnly(String key) { ve(key);
}
//载⼊缓存
public synchronized static void putCache(String key, Cache obj) { cacheMap.put(key, obj);
}
//获取缓存信息
mygrepublic static Cache getCacheInfo(String key) {
if (hasCache(key)) {
Cache cache = getCache(key);连锁反应英文版
if (cacheExpired(cache)) { //调⽤判断是否终⽌⽅法
cache.tExpired(true);
return null;
}
汤姆索亚历险记主要内容return cache;
}el{
return null;
}
}
//载⼊缓存信息
public static void putCacheInfo(String key, Cache obj, long dt,boolean expired) {
Cache cache = new Cache();
cache.tKey(key);
cache.tTimeOut(dt + System.currentTimeMillis()); //设置多久后更新缓存
ndocache.tValue(obj);
cache.tExpired(expired); //缓存默认载⼊时,终⽌状态为FALSE
cacheMap.put(key, cache);
}
//重写载⼊缓存信息⽅法
public static void putCacheInfo(String key,Cache obj,long dt){
Cache cache = new Cache();
cache.tKey(key);
cache.tTimeOut(dt+System.currentTimeMillis());
cache.tValue(obj);
cache.tExpired(fal);
cacheMap.put(key,cache);
}
//判断缓存是否终⽌
public static boolean cacheExpired(Cache cache) {
if (null == cache) { //传⼊的缓存不存在
return fal;
}
long nowDt = System.currentTimeMillis(); //系统当前的毫秒数
黄英文long cacheDt = TimeOut(); //缓存内的过期毫秒数
if (cacheDt <= 0||cacheDt>nowDt) { //过期时间⼩于等于零时,或者过期时间⼤于当前时间时,则为FALSE return fal;

本文发布于:2023-07-17 01:05:29,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1100516.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:缓存   需要   是否   判断   静态   信息   查询   安徽
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图