@@ -26,7 +26,7 @@ async function setupBazel() {
2626
2727 await setupBazelisk ( )
2828 await restoreCache ( config . bazeliskCache )
29- await restoreCache ( config . diskCache )
29+ await restoreDiskCache ( config . diskCache )
3030 await restoreCache ( config . repositoryCache )
3131 await restoreExternalCaches ( config . externalCache )
3232
@@ -143,7 +143,7 @@ async function restoreExternalCaches(cacheConfig) {
143143 }
144144}
145145
146- async function restoreCache ( cacheConfig ) {
146+ async function restoreCacheImpl ( cacheConfig , primaryKey , restoreKeys , cacheHit ) {
147147 if ( ! cacheConfig . enabled ) {
148148 return
149149 }
@@ -152,23 +152,20 @@ async function restoreCache(cacheConfig) {
152152 await setTimeout ( delay , async function ( ) {
153153 core . startGroup ( `Restore cache for ${ cacheConfig . name } ` )
154154
155- const hash = await glob . hashFiles ( cacheConfig . files . join ( '\n' ) )
156155 const name = cacheConfig . name
157156 const paths = cacheConfig . paths
158- const restoreKey = `${ config . baseCacheKey } -${ name } -`
159- const key = `${ restoreKey } ${ hash } `
160157
161- core . debug ( `Attempting to restore ${ name } cache from ${ key } ` )
158+ core . debug ( `Attempting to restore ${ name } cache from ${ primaryKey } ` )
162159
163160 const restoredKey = await cache . restoreCache (
164- paths , key , [ restoreKey ] ,
161+ paths , primaryKey , restoreKeys ,
165162 { segmentTimeoutInMs : 300000 } // 5 minutes
166163 )
167164
168165 if ( restoredKey ) {
169166 core . info ( `Successfully restored cache from ${ restoredKey } ` )
170167
171- if ( restoredKey === key ) {
168+ if ( cacheHit ( restoredKey ) ) {
172169 core . saveState ( `${ name } -cache-hit` , 'true' )
173170 }
174171 } else {
@@ -179,4 +176,28 @@ async function restoreCache(cacheConfig) {
179176 } ( ) )
180177}
181178
179+ async function restoreCache ( cacheConfig ) {
180+ const hash = await glob . hashFiles ( cacheConfig . files . join ( '\n' ) )
181+ const restoreKey = `${ config . baseCacheKey } -${ cacheConfig . name } -`
182+ const key = `${ restoreKey } ${ hash } `
183+ await restoreCacheImpl (
184+ cacheConfig , key , [ restoreKey ] ,
185+ restoredKey => restoredKey === key
186+ )
187+ }
188+
189+ async function restoreDiskCache ( cacheConfig ) {
190+ const hash = await glob . hashFiles ( cacheConfig . files . join ( '\n' ) )
191+
192+ // Since disk caches get updated on any change, each run has a unique key.
193+ // Therefore it can only be restored by prefix match, rather than exact key match.
194+ // When multiple prefix matches exist, the most recent is selected.
195+ const restoreKey = `${ config . baseCacheKey } -${ cacheConfig . name } -`
196+ const hashedRestoreKey = `${ restoreKey } ${ hash } -`
197+ await restoreCacheImpl (
198+ cacheConfig , hashedRestoreKey , [ hashedRestoreKey , restoreKey ] ,
199+ restoredKey => restoredKey . startsWith ( hashedRestoreKey )
200+ )
201+ }
202+
182203run ( )
0 commit comments