谁能写个OSCache缓存对象的例子啊,网上搜的大多是缓存页面的,请教高手啊

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【gs_bg】截止到2008-07-27 21:29:29的历史汇总数据(不包括此帖):
    发帖的总数量:21                       发帖的总分数:610                      每贴平均分数:29                       
    回帖的总数量:16                       得分贴总数量:2                        回帖的得分率:12%                      
    结贴的总数量:21                       结贴的总分数:610                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   

    你最好看一下OSCache的文档与示例,这个才是最直接的方法。
      

  3.   

    使用或继承GeneralCacheAdministrator 类,
    如:
    public class DisplayChart extends HttpServlet { /**  
     * Default constructor.  
     */
    public DisplayChart() {
    super();
    } public void init() throws ServletException {
    return;
    } public static GeneralCacheAdministrator cacheAdmin = new GeneralCacheAdministrator(); public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { String path = getServletContext().getRealPath("/");
    File file = null;
    SimpleDateFormat sdf = new SimpleDateFormat("hh-mm-ss");
    try {
    file = (File) cacheAdmin.getFromCache(sdf.format(new Date()));
    System.out.println("来自缓存!" + sdf.format(new Date()));
    } catch (NeedsRefreshException e) {
    file = new File(path + "xmls\\Pipe11.xml");
    cacheAdmin.putInCache(sdf.format(new Date()), file);
    System.out.println("--缓存没有!" + sdf.format(new Date()));
    }
    sendResponse(file, response);
    return;
    } /**  
     * 把文件用响应流写出  
     * @param file  
     * @param response  
     * @throws IOException  
     */
    public void sendResponse(File file, HttpServletResponse response)
    throws IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
    file));
    BufferedOutputStream bos = new BufferedOutputStream(response
    .getOutputStream());
    byte[] input = new byte[1024];
    boolean eof = false;
    while (!eof) {
    int length = bis.read(input);
    if (length == -1) {
    eof = true;
    } else {
    bos.write(input, 0, length);
    }
    }
    bos.flush();
    bis.close();
    bos.close();
    }}
      

  4.   

    Util类
    public static GeneralCacheAdministrator admin = null;    public static GeneralCacheAdministrator getGeneralCacheAdministrator() {
            if (admin == null) {
                admin = new GeneralCacheAdministrator();
            }
            return admin;
        }
    public class NewsAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    Vector newses1 = null;
    GeneralCacheAdministrator admin = Util.getGeneralCacheAdministrator();

    try {
    newses1 = (Vector) admin.getFromCache("news",1800); } catch (NeedsRefreshException nre) { News01EntityDAO dao1 = new News01EntityDAO();
    String condition = "select * from (select * from  NEWS01 where isShow=1 and datetime is not null order by datetime desc )where rownum<=18";
    newses1 = dao1.getNewssiteSql(condition);
    try {
    admin.putInCache("news", newses1); } catch (Exception ex) {
    admin.cancelUpdate("news");
    ex.printStackTrace();
    }
    }
    request.setAttribute("news", newses1);
    return mapping.findForward("news");
    }
    }