The front-end monorepo project uses caching to optimize packaging speed

Why caching is needed

Company micro-application monorepo form management. Full release is required.
In the future, project-level fine-grained release will be adopted, and the platform machine will be expanded and versioned.

There is a problem

The number of micro-applications has expanded, and each build and release consumes a lot of time.

In order to avoid duplication and time-consuming, use cache to reuse the construction results.

what is cache

Caching is an effective means of improving system performance.
Caching can be found in everything from hardware CPU processors to various upper-layer applications.
For example, a CPU has a first-level cache memory and a second-level cache to improve performance.
For example, the client, CDN, database, and algorithm use a lot of space and time to cache
. As the front end, we are most familiar with the cache control of the http protocol of the browser, etc.
Another example is redis based on memory cache.

Caching characteristics

  • Maximum space: limited space cannot cache all content
  • Clearing (elimination) strategy: used to maintain limited space usage
    • First in, first out strategy FIFO (First In, First Out) Queue
    • Least used policies (LFU) are sorted by the number of visits, and the last one expires
    • The least recently used algorithm (LRU) is sorted by access time, and the oldest access expires.
  • Hit rate: generally used for log statistics, etc. to judge cache quality
    Insert image description here

caching problem

  • How to ensure consistency between the cache and the real data source.

Project implementation

Since only dist directory products are used as cache, building again will clear the dist directory, so the maximum number of caches is only 1, so there is no need to consider space issues.

Cache consistency issues need to be addressed.

How to ensure that the packaged content is consistent with the cache?

  • No modifications to the file
  • Depends on consistent environment variables

No modifications to the file

Use fast-glob to quickly obtain all file names. The file name list md5 is used as the file name key to ensure that there are no additions, deletions, modifications or changes to the file.

Quickly obtain the lastmodify of the file through fast-glob to ensure that the file content has not changed

Environment variables are consistent

Ensure that the cached build environment variables are consistent with the real built environment variables.

These are used as primary keys to determine uniqueness so that the unchanged product corresponds to the content of the build.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_48408736/article/details/118047551