Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(listArchives): add cache for posts #5624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/extend/injector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cache } from 'hexo-util';
import { LocalsType, PageSchema, PostSchema } from '../types';

Check warning on line 2 in lib/extend/injector.ts

View workflow job for this annotation

GitHub Actions / linter

'LocalsType' is defined but never used

Check warning on line 2 in lib/extend/injector.ts

View workflow job for this annotation

GitHub Actions / linter

'PageSchema' is defined but never used

Check warning on line 2 in lib/extend/injector.ts

View workflow job for this annotation

GitHub Actions / linter

'PostSchema' is defined but never used

type Entry = 'head_begin' | 'head_end' | 'body_begin' | 'body_end';

Expand Down Expand Up @@ -44,7 +44,7 @@
}

getSize(entry: Entry): number {
return this.cache.apply(`${entry}-size`, Object.keys(this.store[entry]).length) as number;
return this.cache.apply(`${entry}-size`, () => Object.keys(this.store[entry]).length) as number;
}

register(entry: Entry, value: string | (() => string), to = 'default'): void {
Expand Down
9 changes: 6 additions & 3 deletions lib/plugins/helper/list_archives.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { LocalsType } from '../../types';
import type Query from 'warehouse/dist/query';
import type { LocalsType, PostSchema } from '../../types';
import { toMomentLocale } from './date';
import { url_for } from 'hexo-util';
import { url_for, Cache } from 'hexo-util';

interface Options {
format?: string;
Expand All @@ -20,6 +21,8 @@ interface Data {
count: number;
}

const postsCache = new Cache();

function listArchivesHelper(this: LocalsType, options: Options = {}) {
const { config } = this;
const archiveDir = config.archive_dir;
Expand All @@ -41,7 +44,7 @@ function listArchivesHelper(this: LocalsType, options: Options = {}) {
format = type === 'monthly' ? 'MMMM YYYY' : 'YYYY';
}

const posts = this.site.posts.sort('date', order);
const posts = config.relative_link ? postsCache.apply(`date-${order}`, () => this.site.posts.sort('date', order)) as Query<PostSchema> : this.site.posts.sort('date', order);
if (!posts.length) return result;

const data: Data[] = [];
Expand Down
Loading