Android Remote Image Retrieval and local cache (2)

Source: Internet
Author: User

Last modification time of the file

Java code:

  1. /**
  2. * Last modification time of the file
  3. * @ Param dir
  4. * @ Param fileName
  5. */
  6. Private void updateFileTime (String dir, String fileName ){
  7. File file = new File (dir, fileName );
  8. Long newModifiedTime = System. currentTimeMillis ();
  9. File. setLastModified (newModifiedTime );
  10. }

Copy code

Local Cache Optimization

Java code:

  1. /**
  2. * 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
  3. * Delete Files Not used recently in MySQL 40%.
  4. * @ Param dirPath
  5. * @ Param filename
  6. */
  7. Private void removeCache (String dirPath ){
  8. File dir = new File (dirPath );
  9. File [] files = dir. listFiles ();
  10. If (files = null ){
  11. Return;
  12. }
  13. Int dirSize = 0;
  14. For (int I = 0; I <files. length; I ++ ){
  15. If (files [I]. getName (). contains (WHOLESALE_CONV )){
  16. DirSize + = files [I]. length ();
  17. }
  18. }
  19. If (dirSize> CACHE_SIZE * MB | FREE_SD_SPACE_NEEDED_TO_CACHE> freeSpaceOnSd ()){
  20. Int removeFactor = (int) (0.4 * files. length) + 1 );
  21. Arrays. sort (files, newFileLastModifSort ());
  22. Log. I (TAG, "Clear some expiredcache files ");
  23. For (int I = 0; I <removeFactor; I ++ ){
  24. If (files [I]. getName (). contains (WHOLESALE_CONV )){
  25. Files [I]. delete ();
  26. }
  27. }
  28. }
  29. }
  30. /**
  31. * Delete an expired File
  32. * @ Param dirPath
  33. * @ Param filename
  34. */
  35. Private void removeExpiredCache (StringdirPath, String filename ){
  36. File file = new File (dirPath, filename );
  37. If (System. currentTimeMillis ()-file. lastModified ()> mTimeDiff ){
  38. Log. I (TAG, "Clear some expiredcache files ");
  39. File. delete ();
  40. }
  41. }

Copy code

Sort files by Time

Java code:

  1. /**
  2. * TODO sorts Objects Based on the last modification time *
  3. */
  4. ClassFileLastModifSort implements Comparator <File> {
  5. Public int compare (File arg0, File arg1 ){
  6. If (arg0.lastModified ()> arg1.lastModified ()){
  7. Return 1;
  8. } Else if (arg0.lastModified () = arg1.lastModified ()){
  9. Return 0;
  10. } Else {
  11. Return-1;
  12. }
  13. }
  14. }

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:

  1. Private final HashMap <String, Bitmap> mHardBitmapCache = new LinkedHashMap <String, Bitmap> (HARD_CACHE_CAPACITY/2, 0.75f, true ){
  2. @ Override
  3. Protected booleanremoveEldestEntry (LinkedHashMap. Entry <String, Bitmap> eldest ){
  4. If (size ()> HARD_CACHE_CAPACITY ){
  5. // When the size of a map is greater than 30, the infrequently used key is put into mSoftBitmapCache to ensure the efficiency of mHardBitmapCache.
  6. MSoftBitmapCache. put (eldest. getKey (), newSoftReference <Bitmap> (eldest. getValue ()));
  7. Return true;
  8. } Else
  9. Return false;
  10. }
  11. };

Copy code

And then set it to B cache:

Java code:

  1. /**
  2. * 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.
  3. * Bitmap uses SoftReference. When the memory space is insufficient, bitmap in the cache will be reclaimed.
  4. */
  5. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.