- Home /
Remove cache after being unused
I need to store assetbundle downloaded with UnityWebRequest, so that I don't have to re-download it every time I start the game. However, I want to remove assetbundle, that was not used for certain amount of days. Therefore, I implemented Caching system. Downloading and loading from local storage works fine, but I can't find a way to set timer for deleting assetbundle. I tried Cache.expirationDelay, which seemed as the right way to do it, but it doesn't work as expected. If I create and add a new Cache and set its expirationDelay to e.g. 20, download assetbundle and then restart the app, this code block:
Cache existingCache = Caching.AddCache(existingCachePath); Debug.Log(existingCache.expirationDelay);
Prints default expirationDelay value - 12960000, even though the folder with assetbundle is already created. Is there any other way that I can find out when was the Cache created, or assetbundle most recently downloaded?
Answer by Bunny83 · Jan 14, 2020 at 06:07 PM
I haven't really used the Caching class. However the documentation is pretty clear that using AddCache will add a new cache to a list of caches. To get an existing cache you should use something like Caching.GetCacheByPath or Caching.GetCacheAt
If you have already created several caches you might want to use Caching.GetAllCachePaths to see all caches. Just check the Caching class documentation and scroll to the end to see all the available methods / properties.
The first issue with this approach is it doesn't work like that, at least not for me. If I create multiple directories, in which I create new Cache using Directory.CreateDirectory(newCachePath); newCache = Caching.AddCache(newCachePath);
and then I use Caching.GetAllCachePaths
, it really shows paths to all created Caches. However, if I restart Play mode (in Editor), and then use Caching.GetAllCachePaths, it returns only default unity cache path, even though I can see all created folders from previous run in Windows File Explorer. But even if this worked as expected, I still wouldn't be able to get cache last access time, to serve my purpose by deciding whether it has been unused long time enough to delete cache with all its files or not.