Skip to content

Commit 976d168

Browse files
committed
implement caching in routes
1 parent bf191cc commit 976d168

File tree

16 files changed

+106
-43
lines changed

16 files changed

+106
-43
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ ACCESS_TOKEN_VALIDITY_SEC=172800
5252
REFRESH_TOKEN_VALIDITY_SEC=604800
5353
TOKEN_ISSUER=api.dev.xyz.com
5454
TOKEN_AUDIENCE=xyz.com
55+
56+
# Caching
57+
CONTENT_CACHE_DURATION_MILLIS=600000

src/cache/keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export enum Key {
33
}
44

55
export enum DynamicKey {
6-
BLOGS_TAG = 'BLOGS_TAG',
6+
BLOGS_SIMILAR = 'BLOGS_SIMILAR',
77
BLOG = 'BLOG',
88
}
99

src/cache/repository/BlogCache.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,35 @@ import { getJson, setJson } from '../query';
22
import { Types } from 'mongoose';
33
import Blog from '../../database/model/Blog';
44
import { DynamicKey, getDynamicKey } from '../keys';
5+
import { caching } from '../../config';
6+
import { addMillisToCurrentDate } from '../../helpers/utils';
57

6-
function getKey(blogId: Types.ObjectId) {
8+
function getKeyForId(blogId: Types.ObjectId) {
79
return getDynamicKey(DynamicKey.BLOG, blogId.toHexString());
810
}
911

10-
async function save(blog: Blog, expireAt: Date) {
11-
return setJson(getKey(blog._id), { ...blog }, expireAt);
12+
function getKeyForUrl(blogUrl: string) {
13+
return getDynamicKey(DynamicKey.BLOG, blogUrl);
1214
}
1315

14-
async function fetch(blogId: Types.ObjectId) {
15-
return getJson<Blog>(getKey(blogId));
16+
async function save(blog: Blog) {
17+
return setJson(
18+
getKeyForId(blog._id),
19+
{ ...blog },
20+
addMillisToCurrentDate(caching.contentCacheDuration),
21+
);
22+
}
23+
24+
async function fetchById(blogId: Types.ObjectId) {
25+
return getJson<Blog>(getKeyForId(blogId));
26+
}
27+
28+
async function fetchByUrl(blogUrl: string) {
29+
return getJson<Blog>(getKeyForUrl(blogUrl));
1630
}
1731

1832
export default {
1933
save,
20-
fetch,
34+
fetchById,
35+
fetchByUrl,
2136
};

src/cache/repository/BlogsCache.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import { getListRange, setList } from '../query';
22
import Blog from '../../database/model/Blog';
33
import { DynamicKey, getDynamicKey } from '../keys';
4+
import { addMillisToCurrentDate } from '../../helpers/utils';
5+
import { caching } from '../../config';
6+
import { Types } from 'mongoose';
47

5-
function getTagKey(tag: string) {
6-
return getDynamicKey(DynamicKey.BLOGS_TAG, tag);
8+
function getKeyForSimilar(blogId: Types.ObjectId) {
9+
return getDynamicKey(DynamicKey.BLOGS_SIMILAR, blogId.toHexString());
710
}
811

9-
async function saveForTag(tag: string, blogs: Blog[], expireAt: Date) {
10-
return setList(getTagKey(tag), blogs, expireAt);
12+
async function saveSimilarBlogs(blogId: Types.ObjectId, blogs: Blog[]) {
13+
return setList(
14+
getKeyForSimilar(blogId),
15+
blogs,
16+
addMillisToCurrentDate(caching.contentCacheDuration),
17+
);
1118
}
1219

13-
async function fetch(tag: string) {
14-
return getListRange<Blog>(getTagKey(tag));
20+
async function fetchSimilarBlogs(blogId: Types.ObjectId) {
21+
return getListRange<Blog>(getKeyForSimilar(blogId));
1522
}
1623

1724
export default {
18-
saveForTag,
19-
fetch,
25+
saveSimilarBlogs,
26+
fetchSimilarBlogs,
2027
};

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ export const redis = {
2929
port: parseInt(process.env.REDIS_PORT || '0'),
3030
password: process.env.REDIS_USER_PASSWORD || '',
3131
};
32+
33+
export const caching = {
34+
contentCacheDuration: parseInt(process.env.CONTENT_CACHE_DURATION_MILLIS || '600000'),
35+
};

src/database/model/ApiKey.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ const schema = new Schema<ApiKey>(
7474
},
7575
);
7676

77+
schema.index({ key: 1, status: 1 });
78+
7779
export const ApiKeyModel = model<ApiKey>(DOCUMENT_NAME, schema, COLLECTION_NAME);

src/database/model/Blog.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,16 @@ const schema = new Schema<Blog>(
142142
{
143143
versionKey: false,
144144
},
145-
).index(
145+
);
146+
147+
schema.index(
146148
{ title: 'text', description: 'text' },
147149
{ weights: { title: 3, description: 1 }, background: false },
148150
);
151+
schema.index({ _id: 1, status: 1 });
152+
schema.index({ blogUrl: 1, status: 1 });
153+
schema.index({ isPublished: 1, status: 1 });
154+
schema.index({ _id: 1, isPublished: 1, status: 1 });
155+
schema.index({ tag: 1, isPublished: 1, status: 1 });
149156

150157
export const BlogModel = model<Blog>(DOCUMENT_NAME, schema, COLLECTION_NAME);

src/database/model/Keystore.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ const schema = new Schema<Keystore>(
5151
},
5252
);
5353

54-
schema.index({ client: 1, primaryKey: 1 });
54+
schema.index({ client: 1 });
55+
schema.index({ client: 1, primaryKey: 1, status: 1 });
5556
schema.index({ client: 1, primaryKey: 1, secondaryKey: 1 });
5657

5758
export const KeystoreModel = model<Keystore>(DOCUMENT_NAME, schema, COLLECTION_NAME);

src/database/model/Role.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,6 @@ const schema = new Schema<Role>(
4545
},
4646
);
4747

48+
schema.index({ code: 1, status: 1 });
49+
4850
export const RoleModel = model<Role>(DOCUMENT_NAME, schema, COLLECTION_NAME);

src/database/model/User.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const schema = new Schema<User>(
7474
);
7575

7676
schema.index({ _id: 1, status: 1 });
77+
schema.index({ email: 1 });
7778
schema.index({ status: 1 });
78-
schema.index({ email: 1, status: 1 });
7979

8080
export const UserModel = model<User>(DOCUMENT_NAME, schema, COLLECTION_NAME);

0 commit comments

Comments
 (0)