Pass cache keys instead of cache props when talking to the GlobalTransformCache

Reviewed By: jeanlauliac

Differential Revision: D7031064

fbshipit-source-id: a99fb2775b84ce6b866c80be68f966dcd4ea57ca
This commit is contained in:
Miguel Jimenez Esun 2018-02-22 09:14:54 -08:00 committed by Facebook Github Bot
parent 6da4bc4d30
commit e4b1e773b1
3 changed files with 28 additions and 20 deletions

View File

@ -39,6 +39,8 @@ type FetchOptions = {agent?: ?HttpAgent | HttpsAgent};
* application's top-level `Server` class. * application's top-level `Server` class.
*/ */
export type GlobalTransformCache = { export type GlobalTransformCache = {
keyOf(props: FetchProps): string,
/** /**
* Synchronously determine if it is worth trying to fetch a result from the * Synchronously determine if it is worth trying to fetch a result from the
* cache. This can be used, for instance, to exclude sets of options we know * cache. This can be used, for instance, to exclude sets of options we know
@ -50,7 +52,7 @@ export type GlobalTransformCache = {
* Try to fetch a result. It doesn't actually need to fetch from a server, * Try to fetch a result. It doesn't actually need to fetch from a server,
* the global cache could be instantiated locally for example. * the global cache could be instantiated locally for example.
*/ */
fetch(props: FetchProps): Promise<?CachedResult>, fetch(key: string): Promise<?CachedResult>,
/** /**
* Try to store a result. Callsites won't necessarily wait for the success or * Try to store a result. Callsites won't necessarily wait for the success or
@ -60,7 +62,7 @@ export type GlobalTransformCache = {
* no-op in production, and only do the storage operation from a script * no-op in production, and only do the storage operation from a script
* running on a Continuous Integration platform. * running on a Continuous Integration platform.
*/ */
store(props: FetchProps, result: CachedResult): Promise<void>, store(key: string, result: CachedResult): Promise<void>,
}; };
type FetchResultURIs = (keys: Array<string>) => Promise<Map<string, string>>; type FetchResultURIs = (keys: Array<string>) => Promise<Map<string, string>>;
@ -361,17 +363,17 @@ class URIBasedGlobalTransformCache {
* This may return `null` if either the cache doesn't have a value for that * This may return `null` if either the cache doesn't have a value for that
* key yet, or an error happened, processed separately. * key yet, or an error happened, processed separately.
*/ */
async fetch(props: FetchProps): Promise<?CachedResult> { async fetch(key: string): Promise<?CachedResult> {
const uri = await this._fetcher.fetch(this.keyOf(props)); const uri = await this._fetcher.fetch(key);
if (uri == null) { if (uri == null) {
return null; return null;
} }
return await this._fetchResultFromURI(uri); return await this._fetchResultFromURI(uri);
} }
async store(props: FetchProps, result: CachedResult): Promise<void> { async store(key: string, result: CachedResult): Promise<void> {
if (this._store != null) { if (this._store != null) {
await this._store.store(this.keyOf(props), result); await this._store.store(key, result);
} }
} }
} }

View File

@ -46,18 +46,22 @@ describe('GlobalTransformCache', () => {
const transformOptions = await getTransformOptions(); const transformOptions = await getTransformOptions();
const result = await Promise.all([ const result = await Promise.all([
cache.fetch({ cache.fetch(
cache.keyOf({
localPath: 'some/where/foo.js', localPath: 'some/where/foo.js',
sourceCode: '/* beep */', sourceCode: '/* beep */',
getTransformCacheKey: () => 'abcd', getTransformCacheKey: () => 'abcd',
transformOptions, transformOptions,
}), }),
cache.fetch({ ),
cache.fetch(
cache.keyOf({
localPath: 'some/where/else/bar.js', localPath: 'some/where/else/bar.js',
sourceCode: '/* boop */', sourceCode: '/* boop */',
getTransformCacheKey: () => 'abcd', getTransformCacheKey: () => 'abcd',
transformOptions, transformOptions,
}), }),
),
]); ]);
expect(result).toMatchSnapshot(); expect(result).toMatchSnapshot();
}); });

View File

@ -263,7 +263,7 @@ class Module {
globalCache: GlobalTransformCache, globalCache: GlobalTransformCache,
): Promise<TransformedCode> { ): Promise<TransformedCode> {
const result = await this._transformCodeFor(cacheProps); const result = await this._transformCodeFor(cacheProps);
globalCache.store(cacheProps, result); globalCache.store(globalCache.keyOf(cacheProps), result);
return result; return result;
} }
@ -274,7 +274,9 @@ class Module {
if (globalCache == null || !globalCache.shouldFetch(cacheProps)) { if (globalCache == null || !globalCache.shouldFetch(cacheProps)) {
return await this._transformCodeFor(cacheProps); return await this._transformCodeFor(cacheProps);
} }
const globalCachedResult = await globalCache.fetch(cacheProps); const globalCachedResult = await globalCache.fetch(
globalCache.keyOf(cacheProps),
);
if (globalCachedResult != null) { if (globalCachedResult != null) {
return globalCachedResult; return globalCachedResult;
} }