Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not delete cache file #472

Open
leetomlee123 opened this issue Oct 14, 2024 · 0 comments
Open

can not delete cache file #472

leetomlee123 opened this issue Oct 14, 2024 · 0 comments

Comments

@leetomlee123
Copy link

    if (toRemove.contains(cacheObject.id)) return;

    toRemove.add(cacheObject.id!);
    if (_memCache.containsKey(cacheObject.key)) {
      _memCache.remove(cacheObject.key);
    }
    if (_futureCache.containsKey(cacheObject.key)) {
      await _futureCache.remove(cacheObject.key);
    }
    final file = io.File(cacheObject.relativePath);

    if (file.existsSync()) {
      try {
        await file.delete();
        // ignore: unused_catch_clause
      } on PathNotFoundException catch (e) {
        // File has already been deleted. Do nothing #184
      }
    }

cacheObject.relativePath is not real abs file path ,delete file can not work
class CustomCacheManager { static const key = 'tsl'; static final fileSystem = ExternalStorageFileSystem(key); static CacheManager instance = CacheManager( Config(key, stalePeriod: const Duration(days: 30), maxNrOfCacheObjects: 999999999, fileSystem: fileSystem), ); }
`
class ExternalStorageFileSystem implements FileSystem {
final Future _fileDir;
final String _cacheKey;

ExternalStorageFileSystem(this._cacheKey)
: _fileDir = createDirectory(_cacheKey);

static Future createDirectory(String key) async {
final baseDir = await getExternalStorageDirectory();
final path = p.join(baseDir!.path, key);

const fs = LocalFileSystem();
final directory = fs.directory(path);
await directory.create(recursive: true);
return directory;

}

@OverRide
Future createFile(String name) async {
final directory = await _fileDir;
if (!(await directory.exists())) {
await createDirectory(_cacheKey);
}
return directory.childFile(name);
}

void emptyCache() async {
final directory = await _fileDir;
if (await directory.exists()) {
await directory.delete(recursive: true);
}
}
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant