Last modification time of the file
Java code:
- /**
- * Last modification time of the file
- * @ Param dir
- * @ Param fileName
- */
- Private void updateFileTime (String dir, String fileName ){
- File file = new File (dir, fileName );
- Long newModifiedTime = System. currentTimeMillis ();
- File. setLastModified (newModifiedTime );
- }
Copy code
Local Cache Optimization
Java code:
- /**
- * Calculate the file size in the storage directory. When the total file size is greater than the specified CACHE_SIZE or the remaining space of the sdcard is less than that specified by FREE_SD_SPACE_NEEDED_TO_CACHE
- * Delete Files Not used recently in MySQL 40%.
- * @ Param dirPath
- * @ Param filename
- */
- Private void removeCache (String dirPath ){
- File dir = new File (dirPath );
- File [] files = dir. listFiles ();
- If (files = null ){
- Return;
- }
- Int dirSize = 0;
- For (int I = 0; I <files. length; I ++ ){
- If (files [I]. getName (). contains (WHOLESALE_CONV )){
- DirSize + = files [I]. length ();
- }
- }
- If (dirSize> CACHE_SIZE * MB | FREE_SD_SPACE_NEEDED_TO_CACHE> freeSpaceOnSd ()){
- Int removeFactor = (int) (0.4 * files. length) + 1 );
- Arrays. sort (files, newFileLastModifSort ());
- Log. I (TAG, "Clear some expiredcache files ");
- For (int I = 0; I <removeFactor; I ++ ){
- If (files [I]. getName (). contains (WHOLESALE_CONV )){
- Files [I]. delete ();
- }
- }
- }
- }
- /**
- * Delete an expired File
- * @ Param dirPath
- * @ Param filename
- */
- Private void removeExpiredCache (StringdirPath, String filename ){
- File file = new File (dirPath, filename );
- If (System. currentTimeMillis ()-file. lastModified ()> mTimeDiff ){
- Log. I (TAG, "Clear some expiredcache files ");
- File. delete ();
- }
- }
Copy code
Sort files by Time
Java code:
- /**
- * TODO sorts Objects Based on the last modification time *
- */
- ClassFileLastModifSort implements Comparator <File> {
- Public int compare (File arg0, File arg1 ){
- If (arg0.lastModified ()> arg1.lastModified ()){
- Return 1;
- } Else if (arg0.lastModified () = arg1.lastModified ()){
- Return 0;
- } Else {
- Return-1;
- }
- }
- }
Copy code
Memory storage:
If the data is saved in the memory, you can only save a certain amount, but cannot keep it in it. You need to set the data expiration time, LRU, and other algorithms. Here, you can put commonly used data in one cache (A), but not frequently in another cache (B ). To obtain data, first obtain data from A. If A does not exist, then retrieve data from B. Data in B is mainly from LRU in A. the memory reclaim here is mainly for Memory B, so that data in A can be effectively hit.
First define A cache:
Java code:
- Private final HashMap <String, Bitmap> mHardBitmapCache = new LinkedHashMap <String, Bitmap> (HARD_CACHE_CAPACITY/2, 0.75f, true ){
- @ Override
- Protected booleanremoveEldestEntry (LinkedHashMap. Entry <String, Bitmap> eldest ){
- If (size ()> HARD_CACHE_CAPACITY ){
- // When the size of a map is greater than 30, the infrequently used key is put into mSoftBitmapCache to ensure the efficiency of mHardBitmapCache.
- MSoftBitmapCache. put (eldest. getKey (), newSoftReference <Bitmap> (eldest. getValue ()));
- Return true;
- } Else
- Return false;
- }
- };
Copy code
And then set it to B cache:
Java code:
- /**
- * When the key of the mHardBitmapCache is greater than 30, the key that has not been used recently is put into the cache according to the LRU algorithm.
- * Bitmap uses SoftReference. When the memory space is insufficient, bitmap in the cache will be reclaimed.
- */
- Private final staticConcurrentHashMap <String, SoftReference <Bitmap> mSoftBitmapCache = new ConcurrentHashMap
Copy code
Series of Android remote image acquisition and local cache (1) Post link http://www.eoeandroid.com/thread-98446-1-1.html
Series of Android remote image acquisition and local cache (3) Post link http://www.eoeandroid.com/thread-98450-1-1.html