Skip to content

Commit

Permalink
Removed in-memory cache, rely on CDN cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalikmay committed Feb 14, 2024
1 parent c8eaba2 commit f041987
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "seobot",
"version": "1.0.7",
"version": "1.0.8",
"description": "Client library for the SEObot API",
"main": "dist/index.js",
"files": [
Expand Down
18 changes: 0 additions & 18 deletions src/blog/cache.ts

This file was deleted.

25 changes: 6 additions & 19 deletions src/blog/client.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { Cache } from './cache';
import { Blog } from '../types';
import slugify from 'slugify';

export class BlogClient {
private _key: string;
private _baseCache: Cache<Blog.IArticleIndex[]>;
private _articleCache: Cache<Blog.IArticle>;

constructor(key: string, ttl: number = 180_000) {
constructor(key: string) {
if (!key) throw Error('SEObot API key must be provided. You can use the DEMO key a8c58738-7b98-4597-b20a-0bb1c2fe5772 for testing');
this._key = key;
this._baseCache = new Cache<Blog.IArticleIndex[]>(ttl);
this._articleCache = new Cache<Blog.IArticle>(ttl);
}

private _decompressIndex(short: Blog.IArticleIndexCompressed): Blog.IArticleIndex {
Expand All @@ -37,22 +32,14 @@ export class BlogClient {
}

private async _fetchIndex(): Promise<Blog.IArticleIndex[]> {
const base = await this._baseCache.get(async () => {
const response = await fetch(`https://cdn.seobotai.com/${this._key}/system/base.json`, { cache: 'no-store' });
const index = (await response.json()) as Blog.IArticleIndexCompressed[];
return index.map(i => this._decompressIndex(i)).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
});

return base;
const response = await fetch(`https://cdn.seobotai.com/${this._key}/system/base.json`, { cache: 'no-store' });
const index = (await response.json()) as Blog.IArticleIndexCompressed[];
return index.map(i => this._decompressIndex(i)).sort((a, b) => b.createdAt.localeCompare(a.createdAt));
}

private async _fetchArticle(id: string): Promise<Blog.IArticle> {
const post = await this._articleCache.get(async () => {
const postData = await fetch(`https://cdn.seobotai.com/${this._key}/blog/${id}.json`, { cache: 'no-store' });
const post = (await postData.json()) as Blog.IArticle;
return post;
});

const postData = await fetch(`https://cdn.seobotai.com/${this._key}/blog/${id}.json`, { cache: 'no-store' });
const post = (await postData.json()) as Blog.IArticle;
return post;
}

Expand Down

0 comments on commit f041987

Please sign in to comment.