不能用HashMap等,写一个查成绩缓存。
- 先封装成绩缓存,单独写一个类,这样业务逻辑层能直接调用它,后期也比较容易维护。类里面是用Vector做容器的,存放成绩的实体类,然后再用一个比赛名称做变量名,用来当查成绩的时候,先对比是不是当前缓存内的比赛,不是就直接从数据库查询。
package com.BllCache;
import java.util.Vector;
import com.Entity.V2.Report;
import com.Entity.V2.ReportSet;
public class ReportCache {
private static Vector<Report> reportsCache = new Vector<Report>();
private String competitionName;
private volatile static ReportCache reportCache;
private ReportCache(){} //单例
public static ReportCache getInstense(){
if(reportCache == null){
synchronized (ReportCache.class) {
if(reportCache == null){
reportCache = new ReportCache();
}
}
}
return reportCache;
}
//将所有成绩加载到缓存中
public void addReporttoCache(ReportSet reportSet){
System.out.println("reportSet---cache---size--" + reportSet.getSize());
Runnable ra = new scoreThread(reportSet);
Thread thread = new Thread(ra);
thread.start();
}
//清除所有缓存
public void clearCache(){
reportsCache.clear();
this.competitionName = "";
}
//从缓存中拿数据
public Report searchScoreByInfoOnCache(Report info){
Report report = null;
for(int i=0; i<reportsCache.size(); i++){
Report tempReport = reportsCache.get(i);
//对比信息
if(tempReport.getCompetitionName().equals(info.getCompetitionName()) &&
tempReport.getStudentName().equals(info.getStudentName()) &&
tempReport.getStudentTicket().equals(info.getStudentTicket()) &&
tempReport.getEnrollmentPassword().equals(info.getEnrollmentPassword())){
report = tempReport;
}
}
return report;
}
//添加item到缓存
public void addReporttoCache(Report report){
if(report != null){
reportsCache.add(report);
}
}
//返回缓存的大小
public int getCacheSize(){
return reportsCache.size();
}
//设置缓存大小
public void setCacheSize(int CacheSize){
reportsCache.setSize(CacheSize);
}
//设置缓存大赛名称
public void setCompetitionName(String competitionName) {
this.competitionName = competitionName;
}
public String getCompetitionName() {
return competitionName;
}
//数据加载到缓存
class scoreThread implements Runnable{
private ReportSet reportSet;
public scoreThread(ReportSet reportSet){
this.reportSet = reportSet;
}
@Override
public void run() {
for(int i=0; i<reportSet.getSize(); i++){
Report report = reportSet.getItem(i);
if(report!=null){
reportsCache.add(report);
}
}
System.out.println("reportsCache--cache--size--" + reportsCache.size());
}
}
}
- 写完查成绩、导入成绩缓存、清除成绩缓存的函数。要再考虑多线程的问题,因为成绩很多的时候需要再另开一个线程来运行它。还有要考虑读写的问题,当在导入成绩的时候需要先置缓存类的比赛名称为空,也就是要加读写锁。这两点还只有初步的想法,正在实现中。。。
package com.Bll;
import com.BllCache.ReportCache;
import com.Dao.CDaoFrame;
import com.Dao.IDaoFrame;
import com.Entity.V2.Report;
import com.Entity.V2.ReportSet;
public class CBllReport {
IDaoFrame iDaoFrame = CDaoFrame.getInstance();
private ReportCache reportCache = ReportCache.getInstense();
public CBllReport(){
System.out.println("CBllReport构造函数被调用");
}
/**
* 作者:LinHaiZhen
* 时间:2016-4-12 下午09:14:00
* 函数名:searchScoreByCheck
* 功能:通过输入信息查询成绩
* 参数:@param info
* 参数:@return
* 返回值:Report
*/
public Report searchScoreByInfo(Report info){
Report report = null;
//1.判断查找的比赛成绩是否为缓存的比赛
if(info.isThisCache(reportCache.getCompetitionName())){
//1.1.从缓存中查找
report = reportCache.searchScoreByInfoOnCache(info);
//1.2.如果缓存中没有,从数据库查找
if(report == null){
report = iDaoFrame.searchScoreByCheck(info);
System.out.println("从数据库查找成绩1");
}else{
System.out.println("从缓存查找成绩");
}
}else{
report = iDaoFrame.searchScoreByCheck(info);
System.out.println("从数据库查找成绩2");
}
return report;
}
/**
* 作者:LinHaiZhen
* 时间:2016-4-16 下午09:56:42
* 函数名:putReportSetToCache
* 功能:加载reportSet到缓存
* 参数:@param reportSet
* 返回值:void
*/
public boolean addReportSetToCache(String competitionName){
boolean result = false;
//1设置缓存当前比赛名称为空
reportCache.setCompetitionName("");
//2.获取要加入缓存比赛的数据
long startTime = System.nanoTime(); //开始時間
ReportSet reportSet = iDaoFrame.getReportSetByCompetitionName(competitionName);
long consumingTime = System.nanoTime() - startTime; //消耗時間
System.out.println("查询成绩时间--" + consumingTime + "纳秒");
//3.如果获取的成绩不为空,先清除当前缓存的值,再把数据加载到缓存
if(reportSet != null){
reportCache.clearCache();
long startTime1 = System.nanoTime(); //开始時間
reportCache.addReporttoCache(reportSet);
long consumingTime1 = System.nanoTime() - startTime; //消耗時間
System.out.println("放入缓存时间--" + consumingTime1 + "纳秒");
result = true;
}
//4.如果加载成功,设置缓存的比赛名称
if(result){
reportCache.setCompetitionName(competitionName);
}
return result;
}
/**
* 作者:LinHaiZhen
* 时间:2016-4-19 下午04:02:34
* 函数名:clearScoreCache
* 功能:清除缓存
* 参数:@param competitionName
* 参数:@return
* 返回值:boolean
*/
public boolean clearScoreCache(String competitionName){
boolean result = false;
if(competitionName != null){
reportCache.clearCache();
System.out.println("reportSet---bll---size--" + reportCache.getCacheSize());
result = true;
}
return result;
}
}