-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: save all deferred to
cache.waiting
instead of in the storage
- Loading branch information
1 parent
99f69ce
commit ab051fc
Showing
7 changed files
with
119 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './cache'; | ||
export * from './types' | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { parse } from '@tusbar/cache-control'; | ||
import { AxiosResponse } from 'axios'; | ||
|
||
// response.config.cache?.maxAge || axios.defaults.cache.maxAge | ||
|
||
/** | ||
* Interpret the cache control header, if present. | ||
* | ||
* @param header the header to interpret. | ||
* @returns false if header is not valid, undefined if the maxAge was not specified or a number in seconds from now. | ||
*/ | ||
export function interpretCacheHeader( | ||
headers: AxiosResponse['headers'] | ||
): false | undefined | number { | ||
const cacheControl = headers['cache-control'] || ''; | ||
const { noCache, noStore, maxAge } = parse(cacheControl); | ||
|
||
// Header told that this response should not be cached. | ||
if (noCache || noStore) { | ||
return false; | ||
} | ||
|
||
if (!maxAge) { | ||
return undefined; | ||
} | ||
|
||
return Date.now() + maxAge * 1000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { AxiosCacheInstance, CacheProperties } from '../../axios'; | ||
|
||
export async function updateCache( | ||
axios: AxiosCacheInstance, | ||
data: any, | ||
entries: CacheProperties['update'] | ||
) { | ||
for (const [cacheKey, value] of Object.entries(entries)) { | ||
if (value == 'delete') { | ||
await axios.storage.remove(cacheKey); | ||
continue; | ||
} | ||
|
||
const oldValue = await axios.storage.get(cacheKey); | ||
const newValue = value(oldValue, data); | ||
|
||
if (newValue === undefined) { | ||
await axios.storage.remove(cacheKey); | ||
continue; | ||
} | ||
|
||
await axios.storage.set(cacheKey, newValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters