天天看點

緩存filter及資源池模式

一。緩存過濾器模式

1。概念:緩存過濾器模式是通過使用servlet的filter來動态地緩存生成的頁面,進而提高web層的性能和伸縮性。工作原理非常簡單,當第一次請求到來時,判斷是否可以緩存,可以的話就放在緩存裡。當下次請求時,直接從緩存中取出,而不是再次請求。

2。一個簡單實作對html頁面的緩存:

緩存filter及資源池模式

package cfexample.controller;

緩存filter及資源池模式
緩存filter及資源池模式

import java.io.*;

緩存filter及資源池模式

import javax.servlet.*;

緩存filter及資源池模式

import javax.servlet.http.*;

緩存filter及資源池模式
緩存filter及資源池模式

/**

緩存filter及資源池模式

 *用來替代httpservletreponse的新對象,以提供緩存能力

緩存filter及資源池模式

 */

緩存filter及資源池模式

public class cacheresponsewrapper extends httpservletresponsewrapper {

緩存filter及資源池模式
緩存filter及資源池模式

    private cacheoutputstream outstream;

緩存filter及資源池模式
緩存filter及資源池模式

    //替換outputstream和printwriter

緩存filter及資源池模式

    private servletoutputstream stream;

緩存filter及資源池模式

    private printwriter writer;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    class cacheoutputstream extends servletoutputstream {

緩存filter及資源池模式
緩存filter及資源池模式

        private bytearrayoutputstream bos;

緩存filter及資源池模式
緩存filter及資源池模式

        cacheoutputstream() {

緩存filter及資源池模式

            bos = new bytearrayoutputstream();

緩存filter及資源池模式

        }

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        public void write(int param) throws ioexception {

緩存filter及資源池模式

            bos.write(param);

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        public void write(byte[] b, int off, int len) throws ioexception {

緩存filter及資源池模式

            bos.write(b, off, len);

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        protected byte[] getbytes() {

緩存filter及資源池模式

            return bos.tobytearray();

緩存filter及資源池模式
緩存filter及資源池模式

    }

緩存filter及資源池模式
緩存filter及資源池模式

     public cacheresponsewrapper(httpservletresponse original) {

緩存filter及資源池模式

        super(original);

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    protected servletoutputstream createoutputstream() 

緩存filter及資源池模式

        throws ioexception

緩存filter及資源池模式

    {

緩存filter及資源池模式

        outstream = new cacheoutputstream();

緩存filter及資源池模式

        return outstream;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    public servletoutputstream getoutputstream()

緩存filter及資源池模式

        throws ioexception 

緩存filter及資源池模式
緩存filter及資源池模式

        if (stream != null) {

緩存filter及資源池模式

            return stream;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        if (writer != null) {

緩存filter及資源池模式

            throw new ioexception("writer already in use");

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        stream = createoutputstream();

緩存filter及資源池模式

        return stream;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

     public printwriter getwriter() throws ioexception {

緩存filter及資源池模式
緩存filter及資源池模式

            return writer;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

            throw new ioexception("outputstream already in use");

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        writer = new printwriter(new outputstreamwriter(createoutputstream()));

緩存filter及資源池模式

        return writer;

緩存filter及資源池模式
緩存filter及資源池模式

    protected byte[] getbytes() throws ioexception {

緩存filter及資源池模式

        if (outstream != null) {

緩存filter及資源池模式

            return outstream.getbytes();

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

        return null;

緩存filter及資源池模式
緩存filter及資源池模式

}

緩存filter及資源池模式
緩存filter及資源池模式

//cachefilter.java 過濾器:

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

import java.net.*;

緩存filter及資源池模式

import java.util.*;

緩存filter及資源池模式

import java.text.*;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

import javax.servlet.filter;

緩存filter及資源池模式

import javax.servlet.filterchain;

緩存filter及資源池模式

import javax.servlet.filterconfig;

緩存filter及資源池模式

import javax.servlet.servletcontext;

緩存filter及資源池模式

import javax.servlet.servletexception;

緩存filter及資源池模式

import javax.servlet.servletrequest;

緩存filter及資源池模式

import javax.servlet.servletresponse;

緩存filter及資源池模式

public class cachefilter implements filter {

緩存filter及資源池模式
緩存filter及資源池模式

    private filterconfig filterconfig = null;

緩存filter及資源池模式
緩存filter及資源池模式

    //緩存池

緩存filter及資源池模式

    private hashmap cache;

緩存filter及資源池模式
緩存filter及資源池模式

    public cachefilter() {

緩存filter及資源池模式
緩存filter及資源池模式

    public void dofilter(servletrequest request, 

緩存filter及資源池模式

                         servletresponse response,

緩存filter及資源池模式

                         filterchain chain)

緩存filter及資源池模式

        throws ioexception, servletexception

緩存filter及資源池模式
緩存filter及資源池模式

        httpservletrequest req = (httpservletrequest) request;

緩存filter及資源池模式

        httpservletresponse res = (httpservletresponse) response;

緩存filter及資源池模式
緩存filter及資源池模式

        //緩存子中的鍵uri+查詢字元串

緩存filter及資源池模式

        string key = req.getrequesturi() + "?" + req.getquerystring();

緩存filter及資源池模式
緩存filter及資源池模式

        //隻緩存get請求的内容

緩存filter及資源池模式

        if (req.getmethod().equalsignorecase("get") && iscacheable(key)) {

緩存filter及資源池模式

            byte[] data = (byte[]) cache.get(key);

緩存filter及資源池模式
緩存filter及資源池模式

           //池中沒有,生成并存入

緩存filter及資源池模式

            if (data == null) {

緩存filter及資源池模式

                cacheresponsewrapper crw = new cacheresponsewrapper(res);

緩存filter及資源池模式

                chain.dofilter(request, crw);

緩存filter及資源池模式

                data = crw.getbytes();

緩存filter及資源池模式

                cache.put(key, data);

緩存filter及資源池模式

            } 

緩存filter及資源池模式
緩存filter及資源池模式

            // 如果有的話,直接得到傳回

緩存filter及資源池模式

            if (data != null) {

緩存filter及資源池模式

                res.setcontenttype("text/html");

緩存filter及資源池模式

                res.setcontentlength(data.length);

緩存filter及資源池模式
緩存filter及資源池模式

                try {

緩存filter及資源池模式

                    outputstream os = res.getoutputstream();

緩存filter及資源池模式

                    os.write(data);

緩存filter及資源池模式

                    os.flush();

緩存filter及資源池模式

                    os.close();

緩存filter及資源池模式

                } catch(exception ex) {

緩存filter及資源池模式

                    ex.printstacktrace();

緩存filter及資源池模式

                }

緩存filter及資源池模式

            }

緩存filter及資源池模式

        } else {

緩存filter及資源池模式

            // generate the data normally if it was not cacheable

緩存filter及資源池模式

            chain.dofilter(request, response);

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    //判斷是否可以緩存,考慮一個配置檔案配置哪些可以緩存,此處省去

緩存filter及資源池模式

    private boolean iscacheable(string key) {

緩存filter及資源池模式

        return true;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    public void init(filterconfig filterconfig) {

緩存filter及資源池模式

        this.filterconfig = filterconfig;

緩存filter及資源池模式
緩存filter及資源池模式

        cache = new hashmap();

緩存filter及資源池模式
緩存filter及資源池模式

    public void destroy() {

緩存filter及資源池模式

        cache.clear();

緩存filter及資源池模式
緩存filter及資源池模式

        cache = null;

緩存filter及資源池模式

        filterconfig = null;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

3.實際應用例子:oscache是很好的解決web層緩存的方案!!準備認真讀讀它的源代碼。

二。資源池模式:

1。概念:一個資源池就是一組預先生成的對象,它們可以被出借以便節省多次chuang建立它們所花費的時間。典型的如:ejb池(service locator一般都有一個ejb的home接口池),資料庫連接配接池。

2。優點:a。提高了應用的可伸縮性,使資源的建立和開銷不至于失控。b,産生了一個統一的有效微調點,通過運作時修改池參數來影響應用的性能等因素。

3。簡單實作:

(1)首先一個建立對象的工廠:

緩存filter及資源池模式

package pool;

緩存filter及資源池模式
緩存filter及資源池模式

public interface resourcefactory {

緩存filter及資源池模式

    public object createresource();

緩存filter及資源池模式
緩存filter及資源池模式

    //驗證傳回的資源,并提供還原

緩存filter及資源池模式

    public boolean validateresource(object o);

緩存filter及資源池模式

(2)資源池:

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

public class resourcepool {

緩存filter及資源池模式

    private resourcefactory factory;

緩存filter及資源池模式
緩存filter及資源池模式

   //參數

緩存filter及資源池模式

    private int maxobjects;

緩存filter及資源池模式

    private int curobjects;

緩存filter及資源池模式

    private boolean quit;

緩存filter及資源池模式
緩存filter及資源池模式

    //出借的資源

緩存filter及資源池模式

    private set outresources;

緩存filter及資源池模式
緩存filter及資源池模式

    //可以使用的資源

緩存filter及資源池模式

    private list inresources;

緩存filter及資源池模式
緩存filter及資源池模式

    public resourcepool(resourcefactory factory, int maxobjects) {

緩存filter及資源池模式

        this.factory = factory;

緩存filter及資源池模式

        this.maxobjects = maxobjects;

緩存filter及資源池模式
緩存filter及資源池模式

        curobjects = 0;

緩存filter及資源池模式
緩存filter及資源池模式

        outresources = new hashset(maxobjects);

緩存filter及資源池模式

        inresources = new linkedlist();

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

   //從池中取資源

緩存filter及資源池模式

    public synchronized object getresource() throws exception {

緩存filter及資源池模式

        while(!quit) {

緩存filter及資源池模式
緩存filter及資源池模式

             if (!inresources.isempty()) {

緩存filter及資源池模式

                object o = inresources.remove(0);

緩存filter及資源池模式
緩存filter及資源池模式

               if(!factory.validateresource(o))

緩存filter及資源池模式

                    o = factory.createresource();

緩存filter及資源池模式
緩存filter及資源池模式

                outresources.add(o);

緩存filter及資源池模式

                return o;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

            //放入出借池

緩存filter及資源池模式

            if(curobjects < maxobjects) {

緩存filter及資源池模式

                object o = factory.createresource();

緩存filter及資源池模式
緩存filter及資源池模式

                curobjects++;

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

            //沒有可用的,等待

緩存filter及資源池模式

            try { wait(); } catch(exception ex) {}

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

       //池子已經銷毀

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    //歸還資源

緩存filter及資源池模式

    public synchronized void returnresource(object o) {

緩存filter及資源池模式
緩存filter及資源池模式

        if(!outresources.remove(o))

緩存filter及資源池模式

            return;

緩存filter及資源池模式
緩存filter及資源池模式

        inresources.add(o);

緩存filter及資源池模式

        notify();

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

    public synchronized void destroy() {

緩存filter及資源池模式

        quit = true;

緩存filter及資源池模式

        notifyall();

緩存filter及資源池模式
緩存filter及資源池模式
緩存filter及資源池模式

4.執行個體:很多開源的資料庫連接配接池,ejb模式中的service locator等等

文章轉自莊周夢蝶  ,原文釋出5.16