cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

Optimize game asset cache database (cacheManager) save when a lot files in cache.

Open Yuki001 opened this issue 1 year ago • 3 comments

Use Case

Use game cache in wechat game. The game project has a lot asset files to cache

Problem Description

When cached files count has a lot (>2000 or above),if the cache insert a file, the database will update in 2 seconds. But the json database (cacheList.json) serializing and writing is very slow , makes the game junk at every write time.

Proposed Solution

Use a database file format for asset cache instead of json file, not serialize all the data for a update. Maybe a sqlite or something else format which is fast for insert and save.

How it works

No response

Alternatives Considered

Maybe a sqlite or something else format which is fast for insert and save.

Additional Information

No response

Yuki001 avatar Mar 08 '24 02:03 Yuki001

In my project, I have modified the cacheManager to clear 1/3 when found the cache file count is larger than a threshold (2048) on game init, The modify makes my game run better. but it's not the correct solution and is only a patch. The correct solution is make cache database fast for insert and save.

Yuki001 avatar Mar 08 '24 03:03 Yuki001

Can use sqlite in wechat?

minggo avatar Mar 26 '24 02:03 minggo

May be some other methods can be used for wechat instead of direct use sqlite library

  1. the wechat api support low file io operation using fd read/write with position offset,you can use “insert to back(or deleted blocks)and mark flag to delete“ to fast write file。or you can implement a btree to orgnaize the file。
  2. according the insertion id,you can split the json file by hash id into several buckets, each buckets use one json file
  3. use wechat api‘s tempFilePaths ,not use the json file for cache check,but check the file is exist in tempFilePath to make sure it is in cache。and wechat can auto clear the tempFilePath。

Yuki001 avatar Mar 29 '24 03:03 Yuki001