标签:function canvas rem filename lib finally count indexof 开发
安卓开发一般都需要进行数据缓存,常用操作老司机已为你封装完毕,经常有小伙伴问怎么判断缓存是否可用,那我告诉你,你可以用这份工具进行存储和查询,具体可以查看源码,现在为你开车,Demo传送门。
put : 保存String数据到缓存中
getAsString : 读取String数据
getAsJSONObject : 读取JSONObject数据
getAsJSONArray : 读取JSONArray数据
getAsBinary : 获取byte数据
getAsObject : 读取Serializable数据
getAsBitmap : 读取bitmap数据
getAsDrawable : 读取Drawable数据
file : 获取缓存文件
remove : 除某个key
clear : 清除所有数据
public class AppACache {
// 用法例子
// ACache mCache = ACache.get(this); // 初始化,一般放在基类里
// mCache.put("test_key1","test value");
// mCache.put("test_key2", "test value", 10);// 保存10秒,如果超过10秒去获取这个key,将为null
// mCache.put("test_key3", "test value", 2 ACache.TIME_DAY);// 保存两天,如果超过两天去获取这个key,将为null
// String value = mCache.getAsString("test_key1");// 获取数据
public static final int TIME_HOUR = 60 * 60;
public static final int TIME_DAY = TIME_HOUR * 24;
private static final int MAX_SIZE = 1000 * 1000 * 50; // 50 mb
private static final int MAX_COUNT = Integer.MAX_VALUE; // 不限制存放数据的数量
private static Map<String, AppACache> mInstanceMap = new HashMap<String, AppACache>();
private ACacheManager mCache;
public static AppACache get(Context ctx) {
    return get(ctx, "ACache");
}
public static AppACache get(Context ctx, String cacheName) {
    File f = new File(ctx.getCacheDir(), cacheName);
    return get(f, MAX_SIZE, MAX_COUNT);
}
public static AppACache get(File cacheDir) {
    return get(cacheDir, MAX_SIZE, MAX_COUNT);
}
public static AppACache get(Context ctx, long max_zise, int max_count) {
    File f = new File(ctx.getCacheDir(), "ACache");
    return get(f, max_zise, max_count);
}
public static AppACache get(File cacheDir, long max_zise, int max_count) {
    AppACache manager = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid());
    if (manager == null) {
        manager = new AppACache(cacheDir, max_zise, max_count);
        mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), manager);
    }
    return manager;
}
private static String myPid() {
    return "_" + android.os.Process.myPid();
}
private AppACache(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 ACacheManager(cacheDir, max_size, max_count);
}
/**
 * Provides a means to save a cached file before the data are available.
 * Since writing about the file is complete, and its close method is called,
 * its contents will be registered in the cache. Example of use:
 *
 * ACache cache = new ACache(this) try { OutputStream stream =
 * cache.put("myFileName") stream.write("some bytes".getBytes()); // now
 * update cache! stream.close(); } catch(FileNotFoundException e){
 * e.printStackTrace() }
 */
class xFileOutputStream extends FileOutputStream {
    File file;
    public xFileOutputStream(File file) throws FileNotFoundException {
        super(file);
        this.file = file;
    }
    public void close() throws IOException {
        super.close();
        mCache.put(file);
    }
}
// =======================================
// ============ String数据 读写 ==============
// =======================================
/**
 * 保存 String数据 到 缓存中
 * 
 * @param key
 *            保存的key
 * @param value
 *            保存的String数据
 */
public void put(String key, String value) {
    File file = mCache.newFile(key);
    BufferedWriter out = null;
    try {
        out = new BufferedWriter(new FileWriter(file), 1024);
        out.write(value);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        mCache.put(file);
    }
}
/**
 * 保存 String数据 到 缓存中
 * 
 * @param key
 *            保存的key
 * @param value
 *            保存的String数据
 * @param saveTime
 *            保存的时间,单位:秒
 */
public void put(String key, String value, int saveTime) {
    put(key, Utils.newStringWithDateInfo(saveTime, value));
}
/**
 * 读取 String数据
 * 
 * @param key
 * @return String 数据
 */
public String getAsString(String key) {
    File file = mCache.get(key);
    if (!file.exists())
        return null;
    boolean removeFile = false;
    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader(file));
        String readString = "";
        String currentLine;
        while ((currentLine = in.readLine()) != null) {
            readString += currentLine;
        }
        if (!Utils.isDue(readString)) {
            return Utils.clearDateInfo(readString);
        } else {
            removeFile = true;
            return null;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (removeFile)
            remove(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);
    try {
        JSONObject obj = new JSONObject(JSONString);
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
// =======================================
// ============ 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);
    try {
        JSONArray obj = new JSONArray(JSONString);
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
// =======================================