Android data cache-file storage, android file storage
During Android APP development, we usually add cache modules. Cache stores some APP data locally. Most of the data requested by the network is stored locally, so that the cached data can be directly used within the cache data validity period, this reduces the pressure on apps and servers and greatly improves user experience. Android data cache can be saved as data tables or files. Here, I mainly store data through cached files and read the data at the next startup of the APP.
Usage
The design concept of the entire Cache module is very simple. Each cached data corresponds to a key, and each cached data is stored in a file named after this key, which can be directly read if necessary. The key classes areCacheData
,CacheManager
,CacheUtils
.
- CacheData: Data encapsulation class. All data to be cached is encapsulated by CacheData. CacheData defines the cache validity period and can directly obtain real cache data through getData.
- CacheManager: Cache Management class, designed in singleton mode, responsible for cache storage and reading.
- CacheUtils: A common cache class. All cache keys should be defined in this class. This class also defines some time constants that can be used during the cache validity period.
The usage is as follows:
- Step 1:
CacheManager.init(Context context)
The CacheManager should be initialized during APP loading.
- Step 2: The custom Model needs to be serialized, packaged using CacheData, and then stored and read using CacheManager.
Source
CacheUtils. java is mainly used to store some common variables.
public class CacheUtils { public final static String KEY_TEST = "key_test"; public final static long EXPIRATION_MINUTE = 60 * 1000; public final static long EXPIRATION_HOUR = 60 * EXPIRATION_MINUTE; public final static long EXPIRATION_DAY = 24 * EXPIRATION_HOUR; public final static long EXPIRATION_WEEK = 7 * EXPIRATION_DAY;}
The data structure that CacheData. java uses to store
import java.io.Serializable;public class CacheData<T> implements Serializable { private long lastUpdated; private String key; private T data; private long expiration; public CacheData(String key, T data) { this(key, data, -1); } public CacheData(String key, T data, long expiration) { this.key = key; this.expiration = expiration; setData(data); } public void setData(T data) { this.data = data; this.lastUpdated = System.currentTimeMillis(); } public boolean isValid() { return expiration == -1 || lastUpdated + expiration > System.currentTimeMillis(); } public String getKey() { return key; } public T getData() { return data; }}
CacheManager. java read and store Cache
import android.content.Context;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class CacheManager { private static CacheManager instance; private static Context mContext; public static void init(Context context) { mContext = context; } private CacheManager() { } public static CacheManager getInstance() { if (instance == null) instance = new CacheManager(); return instance; } public void addCache(CacheData cacheData) { if (cacheData == null) return; try { File file = new File(mContext.getCacheDir(), cacheData.getKey()); if (!file.exists()) { file.createNewFile(); } ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); oos.writeObject(cacheData); oos.close(); } catch (IOException e) { e.printStackTrace(); } } public CacheData getCache(String key) { try { File file = new File(mContext.getCacheDir(), key); if (file == null) return null; ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); CacheData cacheData = (CacheData) ois.readObject(); ois.close(); if (cacheData.isValid()) return cacheData; } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; }}
Code Download
The project code is hosted in Github Repo. Welcome to star and fork.