我有一个Spring控制器,用于将对象返回给webapp。但是在创建对象时需要花费太多时间,因为它需要从数据库加载数据并使用它执行一些计算。
我使用Hibernate JPA,我没有配置缓存,我没有beans-config.xml,因为我使用基于java的注释来配置我的Spring系统。
我的目标是加快这个过程。
我在我的代码中看到问题,没有使用" ORM magic"但现在我决定不保存RAM,但加快了这个请求(当我重新设计这个凌乱的代码时)。
Spring控制器:
@RequestMapping(value = "/lab/getAlgo", method = RequestMethod.POST)
public @ResponseBody Algo14web getPicksByLeague()
{
Algo1DAO ad = new Algo1DAO();
Algo14web al = new Algo14web(ad.getAlgo(1)); // Long task
return al;
}
Algo1DAO方法getAlgo:
public Algo1 getAlgo(int aid){
Criteria criteria = session.createCriteria(Algo1.class);
criteria.add(Restrictions.eq("id",aid));
@SuppressWarnings("unchecked")
List<Algo1> results = criteria.list();
if(results.size()<1)return null;
return results.get(0);
}
Algo1课程:
@Entity
public class Algo1{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(columnDefinition="DATETIME")
private Date dateFrom; // start counting from this date to past
@Column(columnDefinition="DATETIME")
private Date dateTo; // date for last count date
@Column(length = 3)
private int pointNumber; // total goal per match
@Column(length = 3)
private int historyDeep; // history tree deep
private boolean ast; // allow some teams
@ManyToMany
private List<Match_soccer> matches = new ArrayList<Match_soccer>();
....
Match_soccer类:
@Entity
public class Match_soccer extends Match{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int mid;
@Transient
private boolean win;
@Transient
private List<Odd> odds;
@Transient
private Odd odd;
...
奇数课程:
@Entity
public class Odd {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int oid;
private int mid;
@Column(length = 40)
private String bookmaker;
@Column(length = 6)
private float size;
@Column(length = 5)
private float over;
@Column(length = 5)
private float under;
@Transient
private float sodd;
控制器返回web app json字符串(此字符串每天只更改4次):
{"deep":1,"goalsLimit":1,
"matches":[{"start":1438952400000,"sport":null,"country":"Iran","league":"Persian gulf pro league","ht":"Gostaresh","at":"Tractor","hr":1,"ar":3,"status":"OK","beid":"WplksZ06","mid":25,"win":false,"odds":null,"odd":{"oid":2476,"mid":25,"bookmaker":"Betsafe","size":2.5,"over":2.4,"under":1.51,"sodd":1.51},"resSum":4},{"start":1438966800000,"sport":null,"country":"France","league":"Ligue 2","ht":"Brest","at":"Nimes","hr":2,"ar":0,"status":"OK","beid":"bebW2dfJ"...
Algo14web方法(使用构造函数):
private void setMatchesStatus(){
OddDAO od = new OddDAO();
for (int i = 0; i < matches.size(); i++) {
matches.get(i).setWin(checkOrwin(matches.get(i).getResSum()));
if(matches.get(i).isWin())win++;
else lost++;
List<Odd> odds = od.getBymid(matches.get(i).getMid());
if(odds == null){
System.out.println("Odds null");
odds = Collector.getUOOdds(matches.get(i)); // long task
System.out.println("Try insert odds size "+odds.size());
if(odds.size() == 0){
Odd odd = new Odd();
odd.setMid(matches.get(i).getMid());
odds.add(odd);
}
od.insertOdds(odds);
}
matches.get(i).setOdd(this.setBestOUodd(odds));
}
proc = Math.round(win/matches.size()*100);
}
private Odd setBestOUodd(List<Odd> odds){
Odd bodd = null;
for (Odd odd : odds) {
if(odd.getSize() == limit){
if(bodd == null || (getOddSize(bodd)<getOddSize(odd)))bodd = odd;
}
}
if(bodd != null)
bodd.setSodd(getOddSize(bodd));
return bodd;
}
不知何故,在我看来,这个决定是Spring Cache Abstraction
,但我不熟悉它。
答案 0 :(得分:0)
public static Algo14web al;
@RequestMapping(value = "/lab/getAlgo", method = RequestMethod.POST)
public @ResponseBody Algo14web getPicksByLeague()
{
if(this.al == null || time2update){
Algo1DAO ad = new Algo1DAO();
al = new Algo14web(ad.getAlgo(1)); // Long task
}
return al;
}