Preface:

In view of the development time, often appear the same material pictures, after the same conversion and compression ratio of input to the local Cache file, did not timely cleaning, cause a lot of old files are generated, and making a lot of libraries have similar outdated processing operations, but is the processing of inert, meaning that the same Key file if has not once been called again, It does not trigger the processing of obsolete deletions, which is not a timely solution to the problem of increasing package size.

Brief principle:

How to record these files? At first, THE idea was to record the KEY through MMKV, but I found that if the information was carried, the deserialization of the Value and the serialization into a string before storing the Value, this operation was not convenient, so it would be better to directly use the database for processing, which might be relatively slow. Database table design:

@Entity
class SourceInfo(
    @PrimaryKey val md5Name: String,
    val compress: Int = 100,
    val path: String,
    val finalMd5: String,
    var usingCount :Int = 1
)
Copy the code

Here, Room database is used for management, and MD5 value and compression ratio are used as conditions to record the file path, usingCount is used as the number of times the file is called to facilitate the subsequent deletion of judgment basis, such as THE MD5 calculation of Bitmap:

val md5Name = Util.value2MD5(ByteBuffer.allocate(bitmap.byteCount).let {
                bitmap.copyPixelsToBuffer(it)
                it.array()
            }) + "[${bitmap.width}X${bitmap.height}X${format.name}]"
Copy the code

The md5Name is stored as the primary key of the data, with parameters and file compression as the sole judgment

DataHelper.getDao()
            .getSourceInfo(md5Name, quality)
            
Copy the code

The database is called to find the record, and when the record exists and the local file exists, the local file is returned directly, without writing to the local directory again.

Time to delete

When scanning, the usingCount of the content will be -1. When the value is lower than a certain value, the local file will be deleted when the next task comes. The default task time is 15 minutes, which is the minimum execution interval of Workmanager.

For example the use of

The Asset files are entered sequentially in the same file as the buffer directory.

GlobalScope.launch(Dispatchers.IO) {
                CacheFlow.streamToCache(resources.assets.open("text.txt"))
                    .flatMapConcat { CacheFlow.streamAppendCache(resources.assets.open("text.txt"),it!!) }
                    .flatMapConcat { CacheFlow.streamAppendCache(resources.assets.open("text2.txt"),it!!) }
                    .collect {
                    }
            }
Copy the code

At the end

In fact, this library is used to deal with such a similar package body because of too many cache files and design, in fact, we should pay attention to these specifications during development, we can take temporary cache files if they belong to one-time, immediately delete, should not be directly put in the cache directory. The original intention of this library is to consider that one scenario is: the same file needs to be uploaded to the network again, but before uploading, it needs to be written to the buffer directory from other places. The buffer directory is a temporary storage place, and there is uncertainty about whether it will be uploaded again, so it should be deleted later according to conditions.