APP本地缓存CacheManager

CacheManager(缓存)

通过将Bean对象缓存到SD,Bean对象需要序列化实现

CacheManager.java
public class CacheManager<T extends Serializable> {

    private static String DIR = Environment.getExternalStorageDirectory().getPath();
    private static int SPACE_MIN_CACHE = 1024 * 1024 * 20;//20M
    private static CacheManager manager;

    private CacheManager() {
    }

    public synchronized static CacheManager getInstance() {
        if (manager == null) {
            manager = new CacheManager();
        }
        return manager;
    }

    public void clearCache() {
        File file = null;
        File[] files = null;
        if (BaseUtil.checkSDMounted()) {
            file = new File(DIR);
            files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    f.delete();
                }
            }
        }
    }

    public synchronized void putCacheFile(String key, T t, long time) {
        if (!TextUtils.isEmpty(key)) {
            String md_key = BaseUtil.getMD5(key);
            if (!TextUtils.isEmpty(md_key)) {
                final CacheItem<T> cacheItem = new CacheItem<T>(md_key, t, time);
                saveCacheFile(md_key, cacheItem);
            }
        }
    }

    private boolean saveCacheFile(String md_key, CacheItem cacheItem) {
        if (BaseUtil.getSDSize() > SPACE_MIN_CACHE) {
            saveCacheObject(cacheItem);
            return true;
        }
        return false;
    }

    private void saveCacheObject(CacheItem cacheItem) {
        if (cacheItem != null) {
            BaseUtil.saveObject(cacheItem, DIR + cacheItem.getKey());
        }
    }

    public synchronized T getCacheFile(String key) {
        if (!TextUtils.isEmpty(key)) {
            String md_key = BaseUtil.getMD5(key);
            if (!TextUtils.isEmpty(md_key)) {
                String path = DIR + md_key;
                if (!TextUtils.isEmpty(path)) {
                    CacheItem<T> cacheItem = BaseUtil.getObject(path);
                    if (cacheItem != null) {
                        if (System.currentTimeMillis() < cacheItem.getTime()) {
                            return cacheItem.getData();
                        }
                    }
                }
            }
        }
        return null;
    }

    public void initCache() {
        if (BaseUtil.checkSDMounted()) {
            if (BaseUtil.getSDSize() < SPACE_MIN_CACHE) {
                clearCache();
            } else {
                File file = new File(DIR);
                if (!file.exists()) {
                    file.mkdirs();
                }
            }
        }
    }
}

BaseUtil.java

public class BaseUtil {

    private static final char[] HEX = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};


    public static String getMD5(String key) {

        if (!TextUtils.isEmpty(key)) {
            try {
                final MessageDigest md = MessageDigest.getInstance(key);
                md.update(key.trim().getBytes());
                final byte[] bytes = md.digest();
                return toHexString(bytes);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }
        return key;
    }

    private static String toHexString(final byte[] bytes) {

        final StringBuilder sb = new StringBuilder();

        if (bytes != null) {
            for (byte b : bytes) {
                sb.append(HEX[(b & 0xf0)] >>> 4);
                sb.append(HEX[b & 0x0f]);
            }
        }

        return sb.toString();
    }

    public static boolean checkSDMounted() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state) && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
    }

    public static int getSDSize() {
        String path = Environment.getExternalStorageDirectory().getPath();
        final StatFs s = new StatFs(path);
        int block = s.getAvailableBlocks();
        return s.getBlockSize() * block;
    }

    public static <T extends Serializable> void saveObject(T t, String path) {

        if (!TextUtils.isEmpty(path)) {
            ObjectOutputStream oos = null;
            final File file = new File(path);
            try {
                oos = new ObjectOutputStream(new FileOutputStream(file));
                oos.writeObject(t);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (oos != null) {
                    try {
                        oos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static <T extends Serializable> T getObject(String path) {
        if (!TextUtils.isEmpty(path)) {
            ObjectInputStream ois = null;
            final File file = new File(path);
            if (file.exists()) {
                try {
                    ois = new ObjectInputStream(new FileInputStream(file));
                    return (T) ois.readObject();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    if (ois != null) {
                        try {
                            ois.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        return null;
    }

}

CacheItem.java

public class CacheItem<T extends Serializable> implements Serializable {
    private String key;//key
    private T data;//缓存的数据
    private long time;//时间戳

    public CacheItem(String key, T data, long time) {
        this.key = key;
        this.data = data;
        this.time = time;
    }

    public String getKey() {
        return key;
    }

    public T getData() {
        return data;
    }

    public long getTime() {
        return time;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值