Java 缓存搜索结果实现

Java 缓存搜索结果实现,java,mysql,caching,Java,Mysql,Caching,数据库:mysql——表结构为 ------------------------------------------ phone_no (This is PK) | month | year ------------------------------------------ varchar(15) | INT (2) | INT(4) 这有数以百万计的记录,现在当一个用户在一个月内第一次访问该网站时,我必须在数据库中输入一个条目,否则什么也不会发生,这是为

数据库:mysql——表结构为

------------------------------------------
phone_no (This is PK)    | month   | year
------------------------------------------
varchar(15)              | INT (2) | INT(4)
这有数以百万计的记录,现在当一个用户在一个月内第一次访问该网站时,我必须在数据库中输入一个条目,否则什么也不会发生,这是为了报告的目的。我的Java代码是:

The VO ---->
         public class UesrAccessInfo {
            private String phone_no;
            private int year;
            private int month;
             .. getters and setters ...
        }
现在,访问该表的java代码是:

/*以下方法是用java代码实现的,没有数据库访问*/

String phone_no = getPhoneNumber(); 
int currentMonth = getCurMonth(); 
int currentYear = getCurYear(; 

if (phone_no == null ) {
     request.setAttribute("firstAccessInCurrentMonth", false);
} 
else{
    UesrAccessInfo oUesrAccessInfo = new UesrAccessInfo();
    oUesrAccessInfo.setPhone_no(phone_no);
    UserAcessHistoryDBUtil.getUesrAccessInfo(oUesrAccessInfo);

    //////////// Code  for getUesrAccessInfo() in UserAcessHistoryDBUtil class /////////////
      String sql = "select month , year from user_access_history where phone_no= ?";
      String[][] rs = new MyDBAccessor().getPreparedTable(sql,new String[] { oUesrAccessInfo.getMsisdn() });
      if (rs != null && rs.length > 0) {
         for (String[] item : rs) {
            oUesrAccessInfo.setMonth(Integer.parseInt(item[0]));
            oUesrAccessInfo.setMonth(Integer.parseInt(item[1]));
         }
      }else{
        oUesrAccessInfo.setMonth(0);
      }
    }
    /////////////////////////////////////////////////////////////////
    /**
     * User is already present in database
     */
    if(oUesrAccessInfo.getMonth() != 0){

        /**
         * User has already accessed the site in current month
         */
        if(oUesrAccessInfo.getYear() == currentYear && oUesrAccessInfo.getMonth() == currentMonth){
            request.setAttribute("firstAccessInCurrentMonth", false);
        }
        else{
            UserAcessHistoryDBUtil.updateUserAccessHistory(phone_no, ""+ currentMonth, "" + currentYear);
            request.setAttribute("firstAccessInCurrentMonth", true);
        }
    }
    /**
     * User is not present in database
     */
    else{
        UserAcessHistoryDBUtil.createUserAccessHistory(phone_no,""+ currentMonth, "" + currentYear);
        request.setAttribute("firstAccessInCurrentMonth", true);
    }
}
因此,我现在面临的是性能问题。我不能使用缓存,因为服务器中可能存在内存不足错误

任何建议,为了提高性能,我是mysql新手。

我的建议

  • 因为这样做只是为了报告目的,不会 影响用户,你为什么不呢
  • 您可以对某个表中的每个访问进行插入,并创建一个批处理过程,该批处理过程将对当前插入的表进行批量插入。至少这样可以避免运行select查询
谢谢你的建议,但最后我实现了ehcache:)很抱歉,实际上我当时担心的是缓存会使用jvm内存,因此可能会崩溃,但我发现通过ehcache,我也可以使用外部内存。很抱歉,我不知道缓存。最终的实现将是获取、更新数据并将其插入缓存,当超过某个限制时,后端作业将更新数据库。:)