apache httpclient CacheStorage的一个自定义实现

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import my.httpClient.MD5Helper;

import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.client.cache.HttpCacheUpdateCallback;
import org.apache.http.client.cache.HttpCacheUpdateException;

public class DiskCache implements HttpCacheStorage {

    public DiskCache() {

    }

    public HttpCacheEntry getEntry(String url) throws IOException {

        HttpCacheEntry entry = null;

        // 一个文件一个缓存项,使用 请求的url进行hash
        String path = getPath(url);
        // System.out.println("path:" + path);
        // 判断文件是否存在
        File file = new File(path);
        if (file.exists() == false) {
            return null;
        }

        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                    path));
            entry = (HttpCacheEntry) in.readObject();
            in.close();
        //    System.out.println("object read here:");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return entry;

    }

    public void putEntry(String url, HttpCacheEntry entry) throws IOException {

        String path = getPath(url);

        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                path));
        out.writeObject(entry);

        // System.out.println("object has been written..");
        out.close();

    }

    
    
    public void removeEntry(String url) throws IOException {

        String path = getPath(url);

        // 判断文件是否存在
        File file = new File(path);
        if (file.exists() == true) {

            file.delete();
        }
    }

    
    
    public void updateEntry(String url, HttpCacheUpdateCallback callback)
            throws IOException, HttpCacheUpdateException {

        String path = getPath(url);
        HttpCacheEntry existingEntry = null;

        // 判断文件是否存在,若文件存在,则取出文件的内容
        File file = new File(path);
        if (file.exists() == true) {

            ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                    path));
            try {
                existingEntry = (HttpCacheEntry) in.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            in.close();
            System.out.println("object read here:");
        }

        HttpCacheEntry updatedEntry = callback.update(existingEntry); // 获取更新过的缓存实体

        // 不管存在不存在,都用保存新的实体项。
        putEntry(url, updatedEntry);

    }

    
    
    /*
     * 根据url地址获取缓存项在文件系统中的存储地址
     */
    private String getPath(String url) {
        String key = MD5Helper.string2MD5(url);
        String path = "c:\\http-cache\\" + key;
        return path;
    }

}

 参考资料:

http://hc.apache.org/httpcomponents-client-ga/httpclient-cache/xref/index.html

http://hc.apache.org/httpcomponents-client-ga/httpclient-cache/xref/org/apache/http/impl/client/cache/ManagedHttpCacheStorage.html

posted @ 2016-01-02 12:56  何德海  阅读(454)  评论(0编辑  收藏  举报