-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support caching pages * Add tests for notion/cache * Add clearPages function * Improve caching logic * Update cache for setPageProperty calls * Improve cache expiration strategy * Improve test coverage
- Loading branch information
Showing
4 changed files
with
121 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { PageObjectResponse } from '@notionhq/client/build/src/api-endpoints.js' | ||
import { getPage, putPage, clearPages, cleanupPages } from '../../src/notion/cache.js' | ||
|
||
const page: PageObjectResponse = { | ||
object: 'page', | ||
parent: { | ||
type: 'database_id', | ||
database_id: '' | ||
}, | ||
properties: undefined, | ||
icon: { | ||
type: 'emoji', | ||
emoji: '😀' | ||
}, | ||
cover: { | ||
type: 'external', | ||
external: { | ||
url: '' | ||
} | ||
}, | ||
created_by: { | ||
id: '', | ||
object: 'user' | ||
}, | ||
last_edited_by: { | ||
id: '', | ||
object: 'user' | ||
}, | ||
id: '', | ||
created_time: '', | ||
last_edited_time: '', | ||
archived: false, | ||
url: '' | ||
} | ||
|
||
describe('cached pages operations', () => { | ||
it('put, get, clear, and get', () => { | ||
clearPages() // reset cache | ||
|
||
putPage(page) | ||
expect(getPage(page.id)).toBe(page) | ||
clearPages() | ||
expect(getPage(page.id)).toBe(null) | ||
}) | ||
|
||
it('cleanup', () => { | ||
clearPages() // reset cache | ||
|
||
putPage(page) | ||
expect(getPage(page.id)).toBe(page) | ||
|
||
// cleanupPages decrements ttl | ||
// and removes pages with ttl === 0 | ||
// so the page should be removed | ||
// after two calls to cleanupPages | ||
cleanupPages() | ||
expect(getPage(page.id)).toBe(page) | ||
cleanupPages() | ||
expect(getPage(page.id)).toBe(null) | ||
}) | ||
}) |
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,38 @@ | ||
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints" | ||
|
||
let cachedPages: Record<string, { | ||
page: PageObjectResponse, | ||
ttl: number | ||
}> = {} | ||
|
||
export function getPage(pageId: string) { | ||
if (pageId in cachedPages) { | ||
const { page, ttl } = cachedPages[pageId] | ||
if (ttl === 0) return null | ||
return page | ||
} | ||
return null | ||
} | ||
|
||
export function putPage(page: PageObjectResponse) { | ||
cachedPages[page.id] = { | ||
page, | ||
ttl: 2 // count down to 0 | ||
} | ||
} | ||
|
||
// cleanupPages removes pages with ttl === 0 | ||
// and decrements ttl for all other pages | ||
export function cleanupPages() { | ||
for (const pageId in cachedPages) { | ||
if (cachedPages[pageId].ttl === 0) { | ||
delete cachedPages[pageId] | ||
} else { | ||
cachedPages[pageId].ttl -= 1 | ||
} | ||
} | ||
} | ||
|
||
export function clearPages() { | ||
cachedPages = {} | ||
} |
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