In the Android app development process, we usually add a cache module. The cache saves some data from the app locally, mostly by storing the data on the network request locally, so that cached data can be used directly for the duration of the cached data, reducing the pressure on the app and the server and greatly improving the user experience. The Android data cache can be either saved as a data table or cached as a file. Here I store the data primarily through the cache file and read it the next time the app is started.
Usage
The entire cache module is designed to be simple, each cached data corresponds to a key, each cached data will be stored in a file named after this key, the need to read directly. The key classes are CacheData , respectively, CacheManager CacheUtils .
- CacheData: Data encapsulation class, all the data to be cached through the CacheData encapsulation, cachedata in the ability to define the cache validity period, and through the GetData () can get the real cache data directly.
- CacheManager: Cache management class, Singleton mode design, responsible for cache storage and reading.
- Cacheutils: Cache Common classes, all cache keys should be defined in this class, and this class also defines some time constants that can be used in the cache validity period.
Here's how to use it:
- Step1:
CacheManager.init(Context context) When the app loads, it should initialize the CacheManager.
- Step2: 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 StaticString key_test ="Key_test"; Public Final Static LongExpiration_minute = -* +; Public Final Static LongExpiration_hour = -* Expiration_minute; Public Final Static LongExpiration_day = -* Expiration_hour; Public Final Static LongExpiration_week =7* EXPIRATION_DAY;}
Cachedata.java data structures used for storage
Import java.io.Serializable; Public classCachedata<t> implements Serializable {Private Longlastupdated;PrivateString key;PrivateT data;Private Longexpiration; Public CacheData(String key, T data) { This(Key, data,-1); } Public CacheData(String key, T data,LongExpiration) { This. key = key; This. expiration = expiration; SetData (data); } Public void SetData(T data) { This. data = data; This. lastupdated = System.currenttimemillis (); } PublicBooleanIsValid() {returnExpiration = =-1|| lastupdated + Expiration > System.currenttimemillis (); } PublicStringGetKey() {returnKey } PublicTGetData() {returnData }}
Cachemanager.java Read and store cache
ImportAndroid.content.Context;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream; Public class CacheManager { Private StaticCacheManager instance;Private StaticContext Mcontext; Public Static void Init(Context context) {Mcontext = context; }Private CacheManager() { } Public StaticCacheManagergetinstance() {if(Instance = =NULL) Instance =NewCacheManager ();returnInstance } Public void Addcache(CacheData cachedata) {if(CacheData = =NULL)return;Try{File File =NewFile (Mcontext.getcachedir (), Cachedata.getkey ());if(!file.exists ()) {File.createnewfile (); } ObjectOutputStream Oos =NewObjectOutputStream (NewFileOutputStream (file)); Oos.writeobject (CacheData); Oos.close (); }Catch(IOException e) {E.printstacktrace (); } } PublicCacheDataGetCache(String key) {Try{File File =NewFile (Mcontext.getcachedir (), key);if(File = =NULL)return NULL; ObjectInputStream Ois =NewObjectInputStream (NewFileInputStream (file)); CacheData cachedata = (cachedata) ois.readobject (); Ois.close ();if(Cachedata.isvalid ())returnCacheData; }Catch(IOException e) {E.printstacktrace (); }Catch(ClassNotFoundException e) {E.printstacktrace (); }return NULL; }}
Code Download
Project code hosted in GitHub repo, welcome to star and Fork.
Android data Cache-File storage