Implement efficient DiskCache.clear()

Summary: public

Ability to efficiently remove all keys with a particular prefix

Reviewed By: tadeuzagallo

Differential Revision: D2658741

fb-gh-sync-id: 3770f061c83288efe645162ae84a9fd9194d2fd6
This commit is contained in:
Milen Dzhumerov 2015-11-25 03:17:50 -08:00 committed by facebook-github-bot-3
parent aaffb239ca
commit fc5a8678d3
2 changed files with 43 additions and 0 deletions

View File

@ -137,6 +137,26 @@ var AsyncStorage = {
});
},
/**
* Erases all keys with a particular prefix. Useful if all your keys have a
* specific prefix.
*/
clearPrefix: function(
prefix: string,
callback?: ?(error: ?Error) => void
): Promise {
return new Promise((resolve, reject) => {
RCTAsyncStorage.clearPrefix(prefix, function(error) {
callback && callback(convertError(error));
if (error && convertError(error)){
reject(convertError(error));
} else {
resolve(null);
}
});
});
},
/**
* Gets *all* keys known to the app, for all callers, libraries, etc. Returns a `Promise` object.
*/
@ -259,6 +279,11 @@ if (!RCTAsyncStorage.multiMerge) {
delete AsyncStorage.multiMerge;
}
// clearPrefix() only supported by certain backends
if (!RCTAsyncStorage.clearPrefix) {
delete AsyncStorage.clearPrefix;
}
function convertErrors(errs) {
if (!errs) {
return null;

View File

@ -435,6 +435,24 @@ RCT_EXPORT_METHOD(clear:(RCTResponseSenderBlock)callback)
callback(@[RCTNullIfNil(error)]);
}
RCT_EXPORT_METHOD(clearPrefix:(NSString *)prefix callack:(RCTResponseSenderBlock)callback)
{
NSDictionary *errorOut = [self _ensureSetup];
if (errorOut) {
callback(@[errorOut]);
return;
}
NSMutableArray<NSString *> *keys = [NSMutableArray array];
for (NSString *key in _manifest.allKeys) {
if ([key hasPrefix:prefix]) {
[keys addObject:key];
}
}
[self multiRemove:keys callback:callback];
}
RCT_EXPORT_METHOD(getAllKeys:(RCTResponseSenderBlock)callback)
{
NSDictionary *errorOut = [self _ensureSetup];