tech.zhiqu.cache.AndroidCache.java Source code

Java tutorial

Introduction

Here is the source code for tech.zhiqu.cache.AndroidCache.java

Source

/**
 * Copyright (c) 2012-2013, Michael Yang ?? (www.yangfuhai.com).
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package tech.zhiqu.cache;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

/**
 * by tiger007
 */
public class AndroidCache {
    private static final String DEFAULT_NAME = "Cache";
    public static final int TIME_HOUR = 60 * 60;
    public static final int TIME_DAY = TIME_HOUR * 24;
    private static final int MAX_SIZE = 1024 * 1024 * 50; // 50 MB
    private static final int MAX_COUNT = Integer.MAX_VALUE; // ????
    private static Map<String, AndroidCache> mInstanceMap = new HashMap<>();
    private CacheService mCache;

    public static AndroidCache getInstance(Context ctx) {
        return getInstance(ctx, DEFAULT_NAME);
    }

    public static AndroidCache getInstance(Context ctx, String cacheName) {
        File file = new File(ctx.getCacheDir(), cacheName);
        return getInstance(file, MAX_SIZE, MAX_COUNT);
    }

    private static AndroidCache getInstance(File cacheDir) {
        return getInstance(cacheDir, MAX_SIZE, MAX_COUNT);
    }

    public static AndroidCache getInstance(Context ctx, long max_size, int max_count) {
        File f = new File(ctx.getCacheDir(), DEFAULT_NAME);
        return getInstance(f, max_size, max_count);
    }

    public static AndroidCache getInstance(File cacheDir, long max_size, int max_count) {
        AndroidCache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
        if (manager == null) {
            manager = new AndroidCache(cacheDir, max_size, max_count);
            mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
        }
        return manager;
    }

    private static String myPid() {
        return "_" + android.os.Process.myPid();
    }

    private AndroidCache(File cacheDir, long max_size, int max_count) {
        if (!cacheDir.exists() && !cacheDir.mkdirs()) {
            throw new RuntimeException("can't make dirs in " + cacheDir.getAbsolutePath());
        }
        mCache = new CacheService(cacheDir, max_size, max_count);
    }

    public boolean existsKey(String key) {
        return mCache.existsKey(key);
    }

    public ArrayList<String> findKeys(String key) {
        return mCache.findKeys(key);
    }

    public ArrayList<String> getValuesStartWithKey(String key) {
        return mCache.getValueList(key);
    }

    // ============ String?  ==============

    /**
     * ? String?  
     *
     * @param key   ?key
     * @param value ?String?
     */
    public void put(String key, String value) {
        mCache.setKeyValue(key, value);
    }

    /**
     * ? String?  
     *
     * @param key      ?key
     * @param value    ?String?
     * @param saveTime ???
     */
    public void put(String key, String value, int saveTime) {
        put(key, mCache.newStringWithDateInfo(saveTime, value));
    }

    /**
     * ? String?
     *
     * @param key
     * @return String ?
     */
    public String getAsString(String key) {
        return mCache.getValue(key);
    }

    // ============= JSONObject ?  ==============

    /**
     * ? JSONObject?  
     *
     * @param key   ?key
     * @param value ?JSON?
     */
    public void put(String key, JSONObject value) {
        put(key, value.toString());
    }

    /**
     * ? JSONObject?  
     *
     * @param key      ?key
     * @param value    ?JSONObject?
     * @param saveTime ???
     */
    public void put(String key, JSONObject value, int saveTime) {
        put(key, value.toString(), saveTime);
    }

    /**
     * ?JSONObject?
     *
     * @param key
     * @return JSONObject?
     */
    public JSONObject getAsJSONObject(String key) {
        String JSONString = getAsString(key);
        JSONObject obj = null;
        try {
            obj = new JSONObject(JSONString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }

    // ============ JSONArray ?  =============

    /**
     * ? JSONArray?  
     *
     * @param key   ?key
     * @param value ?JSONArray?
     */
    public void put(String key, JSONArray value) {
        put(key, value.toString());
    }

    /**
     * ? JSONArray?  
     *
     * @param key      ?key
     * @param value    ?JSONArray?
     * @param saveTime ???
     */
    public void put(String key, JSONArray value, int saveTime) {
        put(key, value.toString(), saveTime);
    }

    /**
     * ?JSONArray?
     *
     * @param key
     * @return JSONArray?
     */
    public JSONArray getAsJSONArray(String key) {
        String JSONString = getAsString(key);
        JSONArray obj = null;
        try {
            obj = new JSONArray(JSONString);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return obj;
    }

    // =======================================
    // ============== byte ?  =============
    // =======================================

    /**
     * ? byte?  
     *
     * @param key   ?key
     * @param value ??
     */
    public void put(String key, byte[] value) {
        mCache.setKeyValueByte(key, value);
    }

    /**
     * ? byte?  
     *
     * @param key      ?key
     * @param value    ??
     * @param saveTime ???
     */
    public void put(String key, byte[] value, int saveTime) {
        //put(key, CacheUtils.newByteArrayWithDateInfo(saveTime, value));
        mCache.setKeyValueObject(key, value, saveTime);
    }

    /**
     * ? byte ?
     *
     * @param key
     * @return byte ?
     */
    public byte[] getAsBinary(String key) {
        return mCache.getBinary(key);
    }

    // =======================================
    // ============= ? ?  ===============
    // =======================================

    /**
     * ? Serializable?  
     *
     * @param key   ?key
     * @param value ?value
     */
    public void put(String key, Serializable value) {
        put(key, value, -1);
    }

    /**
     * ? Serializable? 
     *
     * @param key      ?key
     * @param value    ?value
     * @param saveTime ???
     */
    public void put(String key, Serializable value, int saveTime) {

        mCache.setKeyValueObject(key, value, saveTime);

    }

    /**
     * ? Serializable?
     *
     * @param key
     * @return Serializable ?
     */
    public Object getAsObject(String key) {
        return mCache.getObject(key);

    }

    // =======================================
    // ============== bitmap ?  =============
    // =======================================

    /**
     * ? bitmap  
     *
     * @param key   ?key
     * @param value ?bitmap?
     */
    public void put(String key, Bitmap value) {
        put(key, mCache.Bitmap2Bytes(value));
    }

    /**
     * ? bitmap  
     *
     * @param key      ?key
     * @param value    ? bitmap ?
     * @param saveTime ???
     */
    public void put(String key, Bitmap value, int saveTime) {
        put(key, mCache.Bitmap2Bytes(value), saveTime);
    }

    /**
     * ? bitmap ?
     *
     * @param key
     * @return bitmap ?
     */
    public Bitmap getAsBitmap(String key) {
        if (getAsBinary(key) == null) {
            return null;
        }
        return mCache.Bytes2Bimap(getAsBinary(key));
    }

    // =======================================
    // ============= drawable ?  =============
    // =======================================

    /**
     * ? drawable  
     *
     * @param key   ?key
     * @param value ?drawable?
     */
    public void put(String key, Drawable value) {
        put(key, mCache.drawable2Bitmap(value));
    }

    /**
     * ? drawable  
     *
     * @param key      ?key
     * @param value    ? drawable ?
     * @param saveTime ???
     */
    public void put(String key, Drawable value, int saveTime) {
        put(key, mCache.drawable2Bitmap(value), saveTime);
    }

    /**
     * ? Drawable ?
     *
     * @param key
     * @return Drawable ?
     */
    public Drawable getAsDrawable(String key) {
        if (getAsBinary(key) == null) {
            return null;
        }
        return mCache.bitmap2Drawable(mCache.Bytes2Bimap(getAsBinary(key)));
    }

    /**
     * ?
     *
     * @param key
     * @return value 
     */
    public File file(String key) {
        File f = mCache.newFile(key);
        if (f.exists())
            return f;
        return null;
    }

    /**
     * ?key
     *
     * @param key
     * @return ??
     */
    public boolean remove(String key) {
        return mCache.remove(key);
    }

    /**
     * ?
     */
    public void clear() {
        mCache.clear();
    }

}