I am implementing an image file cache in my application to reduce the number of times these files are downloaded directly from the web server. While I know the Android OS may decide to clear files from the cache directory of my application to free up internal storage space, I'm not quite sure if this "cleanup" process can happen even during its execution. If I could assume the OS won't mess with the cached files during execution, I would be able to use "in memory" data structures to optimize my cache implementation. For example, I could use a map (in memory) of cached entries (files stored on the cache folder) containing time stamps of cache hits and other information that might be useful to prioritize the way cached entries are recycled. Otherwise, if I have to deal with the fact that the cache cleanup process may happen anytime, anywhere, even during app execution, use of in memory data structures would be useless because they would eventually become out of sync with the real contents of the folder in the event of the OS deciding to clean up files by itself. In which case my cache system would have to be less efficient because it would be forced to keep checking the contents of the cache folder directly in the file system everytime a new file is requested by the app. In other words, I just want to know if I can assume the contents of the cache directory wont be changed by the OS concurrently with my app because that might considerably change my cache implementation.
just delete all files from the cache directory with normal method
public void clear() {
File[] directory = getCacheDir().listFiles();
if(directory != null){
for (File file : directory ){
file.delete();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With