java静态类缓存_java两种缓存

更新时间:2023-07-17 00:59:46 阅读: 评论:0

激怒的意思
java静态类缓存_java两种缓存JAVA缓存有两种:
⼀、⽂件缓存,是指把数据存储在磁盘上,可以XML格式,也可以序列化⽂件DAT格式还是其它⽂件格式。
⼆、内存缓存,也就是实现⼀个类中静态Map,对这个Map进⾏常规的增删查。
其代码如下:
⽬录 [隐藏]
1 Cache类
2 CacheManager类
JAVA缓存-Cache类
动漫的英文public class Cache {
private String key;//缓存ID
private Object value;//缓存数据
private long timeOut;//更新时间
private boolean expired; //是否终⽌
public Cache() {
super();
}
public Cache(String key, Object value, long timeOut, boolean expired) {
this.key = key;
this.value = value;
this.timeOut = timeOut;
ferrino
}
public String getKey() {
return key;
}
public long getTimeOut() {
return timeOut;
}
public Object getValue() {
return value;
}
public void tKey(String string) {
key = string;
}
public void tTimeOut(long l) {
timeOut = l;
}
public void tValue(Object object) {
value = object;
ahoy}
public boolean isExpired() {
return expired;
}
public void tExpired(boolean b) {
expired = b;
}
}
//测试类,
class Test {
public static void main(String[] args) {
System.out.SimpleFlag("alksd"));
// CacheManager.putCache("abc", new Cache());
// CacheManager.putCache("def", new Cache());
// CacheManager.putCache("ccc", new Cache());
// CacheManager.clearOnly("");
/
/ Cache c = new Cache();
// for (int i = 0; i < 10; i++) {
// CacheManager.putCache("" + i, c);
// }
// CacheManager.putCache("aaaaaaaa", c);
// CacheManager.putCache("abchcy;alskd", c);
// CacheManager.putCache("cccccccc", c);
// CacheManager.putCache("abcoqiwhcy", c);
// System.out.println("删除前的⼤⼩:"+CacheSize()); // CacheAllkey();
// CacheManager.clearAll("aaaa");
// System.out.println("删除后的⼤⼩:"+CacheSize());
// CacheAllkey();
}
}
JAVA缓存-CacheManager类        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();
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));
}bridegroom
} catch (Exception ex) {
ex.printStackTrace();
世界地理奇观}
}
//清除指定的缓存
public synchronized static void clearOnly(String key) {
recycle是什么意思ve(key);
}
//载⼊缓存
public synchronized static void putCache(String key, Cache obj) {
cacheMap.put(key, obj);
}
//获取缓存信息
public static Cache getCacheInfo(String key) {
if (hasCache(key)) {
Cache cache = getCache(key);
if (cacheExpired(cache)) { //调⽤判断是否终⽌⽅法
cache.tExpired(true);
}
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()); //设置多久后更新缓存cache.tValue(obj);
cache.tExpired(expired); //缓存默认载⼊时,终⽌状态为FALSE
cacheMap.put(key, cache);
}
//重写载⼊缓存信息⽅法

本文发布于:2023-07-17 00:59:46,感谢您对本站的认可!

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

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

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