@@ -2,20 +2,35 @@ import { getJson, setJson } from '../query';
2
2
import { Types } from 'mongoose' ;
3
3
import Blog from '../../database/model/Blog' ;
4
4
import { DynamicKey , getDynamicKey } from '../keys' ;
5
+ import { caching } from '../../config' ;
6
+ import { addMillisToCurrentDate } from '../../helpers/utils' ;
5
7
6
- function getKey ( blogId : Types . ObjectId ) {
8
+ function getKeyForId ( blogId : Types . ObjectId ) {
7
9
return getDynamicKey ( DynamicKey . BLOG , blogId . toHexString ( ) ) ;
8
10
}
9
11
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 ) ;
12
14
}
13
15
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 ) ) ;
16
30
}
17
31
18
32
export default {
19
33
save,
20
- fetch,
34
+ fetchById,
35
+ fetchByUrl,
21
36
} ;
0 commit comments