Skip to content

Commit

Permalink
perf(listArchives): add cache for posts
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jan 25, 2025
1 parent 70efca7 commit af8e58d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/extend/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Injector {
}

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

0 comments on commit af8e58d

Please sign in to comment.