forked from wh1t3p1g/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tabby.dal.cache; | ||
|
||
import tabby.bean.ref.ClassReference; | ||
import tabby.bean.ref.MethodReference; | ||
|
||
/** | ||
* @author wh1t3P1g | ||
* @since 2020/10/10 | ||
*/ | ||
public interface CacheHelper { | ||
|
||
public <T> void add(T ref); | ||
|
||
public <T> void remove(T ref); | ||
|
||
public void clear(String type); | ||
|
||
public <T> void update(T ref); | ||
|
||
public void save(String path); | ||
|
||
public void loadFromFile(String path); | ||
|
||
public ClassReference loadClassRef(String name); | ||
|
||
public ClassReference loadClassRefByHandle(ClassReference.Handle handle); | ||
|
||
public MethodReference loadMethodRef(ClassReference.Handle classReference, String name, String signature); | ||
|
||
public MethodReference loadMethodRefByHandle(MethodReference.Handle handle); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package tabby.dal.cache; | ||
|
||
import tabby.bean.ref.ClassReference; | ||
import tabby.bean.ref.MethodReference; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author wh1t3P1g | ||
* @since 2020/10/10 | ||
* 这里实际选择用内存作为cache | ||
*/ | ||
public class CacheHelperImpl implements CacheHelper{ | ||
|
||
private Map<ClassReference.Handle, ClassReference> savedClassRefs = new HashMap<>(); | ||
private Map<MethodReference.Handle, MethodReference> savedMethodRefs = new HashMap<>(); | ||
|
||
@Override | ||
public <T> void add(T ref) { | ||
if(ref instanceof ClassReference){ | ||
ClassReference classRef = (ClassReference) ref; | ||
savedClassRefs.put(classRef.getHandle(), classRef); | ||
}else if(ref instanceof MethodReference){ | ||
MethodReference methodRef = (MethodReference) ref; | ||
savedMethodRefs.put(methodRef.getHandle(), methodRef); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void remove(T ref) { | ||
if(ref instanceof ClassReference){ | ||
ClassReference classRef = (ClassReference) ref; | ||
savedClassRefs.remove(classRef.getHandle()); | ||
}else if(ref instanceof MethodReference){ | ||
MethodReference methodRef = (MethodReference) ref; | ||
savedMethodRefs.remove(methodRef.getHandle()); | ||
} | ||
} | ||
|
||
@Override | ||
public void clear(String type) { | ||
if("class".equals(type)){ | ||
savedClassRefs.clear(); | ||
}else if("method".equals(type)){ | ||
savedMethodRefs.clear(); | ||
}else{ | ||
savedMethodRefs.clear(); | ||
savedClassRefs.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public <T> void update(T ref) { | ||
if(ref instanceof ClassReference){ | ||
ClassReference classRef = (ClassReference) ref; | ||
if(savedClassRefs.containsKey(classRef.getHandle())){ | ||
savedClassRefs.replace(classRef.getHandle(), classRef); | ||
return; | ||
} | ||
}else if(ref instanceof MethodReference){ | ||
MethodReference methodRef = (MethodReference) ref; | ||
if(savedMethodRefs.containsKey(methodRef.getHandle())){ | ||
savedMethodRefs.replace(methodRef.getHandle(), methodRef); | ||
return; | ||
} | ||
} | ||
// 不存在相应的key,选择新增 | ||
add(ref); | ||
} | ||
|
||
@Override | ||
public void save(String path) { | ||
// TODO 保存到文件 | ||
} | ||
|
||
@Override | ||
public void loadFromFile(String path) { | ||
// TODO 从文件中恢复以前的解析内容 | ||
} | ||
|
||
@Override | ||
public ClassReference loadClassRef(String name) { | ||
ClassReference.Handle handle = new ClassReference.Handle(name); | ||
return loadClassRefByHandle(handle); | ||
} | ||
|
||
@Override | ||
public ClassReference loadClassRefByHandle(ClassReference.Handle handle) { | ||
return savedClassRefs.get(handle); | ||
} | ||
|
||
@Override | ||
public MethodReference loadMethodRef(ClassReference.Handle classReference, String name, String signature) { | ||
MethodReference.Handle handle = new MethodReference.Handle(classReference, name, signature); | ||
return loadMethodRefByHandle(handle); | ||
} | ||
|
||
@Override | ||
public MethodReference loadMethodRefByHandle(MethodReference.Handle handle) { | ||
return savedMethodRefs.get(handle); | ||
} | ||
} |