Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
xuxueli committed Jun 30, 2024
1 parent b4abab4 commit ae72636
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 144 deletions.
56 changes: 56 additions & 0 deletions src/main/java/com/xxl/tool/cache/CacheConf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.xxl.tool.cache;

/**
* simpie local cache tool
*
* @author xuxueli 2018-01-22 21:37:34
*/
public class CacheConf {

private String key;
private long effectTime;
private long expireTime;

public CacheConf() {
}
public CacheConf(String key, long effectTime, long expireTime) {
this.key = key;
this.effectTime = effectTime;
this.expireTime = expireTime;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public long getEffectTime() {
return effectTime;
}

public void setEffectTime(long effectTime) {
this.effectTime = effectTime;
}

public long getExpireTime() {
return expireTime;
}

public void setExpireTime(long expireTime) {
this.expireTime = expireTime;
}

// util
/**
* 是否有效(未过期)
*
* @return
*/
public boolean isValid(){
return expireTime > System.currentTimeMillis();
}

}
46 changes: 0 additions & 46 deletions src/main/java/com/xxl/tool/cache/CacheData.java

This file was deleted.

101 changes: 101 additions & 0 deletions src/main/java/com/xxl/tool/cache/CacheManage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.xxl.tool.cache;

import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* cache manage
*
* @author xuxueli 2018-01-22 21:37:34
*/
public class CacheManage {

private ConcurrentMap<String, Object> cacheData = new ConcurrentHashMap<>();
private ConcurrentMap<String, CacheConf> cacheConf = new ConcurrentHashMap<>();
private long maxLenth = 10000;

// set、get
public ConcurrentMap<String, Object> getCacheData() {
return cacheData;
}

public void setCacheData(ConcurrentMap<String, Object> cacheData) {
this.cacheData = cacheData;
}

public ConcurrentMap<String, CacheConf> getCacheConf() {
return cacheConf;
}

public void setCacheConf(ConcurrentMap<String, CacheConf> cacheConf) {
this.cacheConf = cacheConf;
}

public long getMaxLenth() {
return maxLenth;
}

public void setMaxLenth(long maxLenth) {
this.maxLenth = maxLenth;
}

// tool

/**
* capacity check
*/
public void capacityCheck() {

// capacity 0-50%:pass
if (cacheConf.size() < maxLenth * 0.5) {
return;
}
// capacity 90-100%:random clear
if (cacheConf.size() < maxLenth * 0.9) {
if (new Random().nextBoolean()) {
return;
}
}

// capacity Reach 100%:must clear
// clear expired data
Set<String> keysToRemove = new HashSet<>();
for (Map.Entry<String, CacheConf> entry: cacheConf.entrySet()) {
if (!entry.getValue().isValid()) {
keysToRemove.add(entry.getKey());
}
}
removeKeys(keysToRemove);

// valid capacity, do remove if exceed capacity
long clearCnt = cacheConf.size() - maxLenth;
if (clearCnt > 0) {
Set<String> keysToCut = new HashSet<>();
// collect key
for (String key: cacheConf.keySet()) {
keysToCut.add(key);
if (keysToCut.size() >= clearCnt) {
break;
}
}
// remove
removeKeys(keysToCut);
}

}

private void removeKeys(Set<String> keys){
if (!keys.isEmpty()) {
for (String key : keys) {
cacheData.remove(key);
cacheConf.remove(key);
}
}
}


}
Loading

0 comments on commit ae72636

Please sign in to comment.