OSCache应用示例

private static GeneralCacheAdministrator admin = new GeneralCacheAdministrator();

public List findNewsInfo(Integer hidden) {

        Session session = null;
        Query query = null;
        List list = null;
        Transaction tx = null;

        // 当前系统时间与信息截止时间差;
        String tiaojian = "to_days(now())>=to_days(infotab.startTime) and to_days(now())<=to_days(infotab.endTime)";

        String strSQL = ""
                + "FROM InfoTab as infotab where infotab.parentLanMuName is not null and infotab.hidden="
                + hidden + " and " + tiaojian
                + " order by infotab.createTime desc";

        String key = "findNewsInfo";
        boolean updated = false;
        int myRefreshPeriod = 60;

        try {
            list = (java.util.List) admin.getFromCache(key, myRefreshPeriod);
            System.out.println("来自缓存:" + strSQL);
        } catch (NeedsRefreshException nre) {
            try {
                System.out.println("来自数据库:" + strSQL);
                
                session = JhGroupSessionFactory.getSession();
                tx = session.beginTransaction();

                query = session.createQuery(strSQL);

                query.setFirstResult(0);
                query.setMaxResults(12);

                list = query.list();

                admin.putInCache(key, list);
                updated = true;

                tx.commit();

            } catch (HibernateException he) {
                he.printStackTrace();
                tx.rollback();
            } finally {
                if (!updated) {
                    admin.cancelUpdate(key);
                }
            }
        }
        return list;
    }


    public static void main(String[] args) {

        FindInfoService f = new FindInfoService();

        List list = f.findNewsInfo(Global.display);
        for (int i = 0; i < list.size(); i++) {
            InfoTab info = (InfoTab) list.get(i);
            System.out.println(info.getParentLanMuName() + "   "
                    + info.getTitle());
        }

    }

   


你可能感兴趣的:(F#)