File tree Expand file tree Collapse file tree 2 files changed +30
-14
lines changed Expand file tree Collapse file tree 2 files changed +30
-14
lines changed Original file line number Diff line number Diff line change
1
+ import StorageService from "@/services/storage.service" ;
2
+
3
+ export default function useLocaleStorageCache ( ) {
4
+ const getCache = ( key ) => {
5
+ return StorageService . get ( key ) ;
6
+ } ;
7
+ const clearCache = ( key ) => StorageService . delete ( key ) ;
8
+
9
+ const setCache = ( key , value ) => StorageService . set ( key , value ) ;
10
+
11
+ return {
12
+ getCache,
13
+ clearCache,
14
+ setCache,
15
+ } ;
16
+ }
Original file line number Diff line number Diff line change 1
1
import { ref } from "vue" ;
2
2
import { useLoading } from "./loading.composable" ;
3
3
import PostsServices from "@/services/posts.services" ;
4
- import StorageService from "@/services/storage.service " ;
4
+ import useLocaleStorageCache from "./cache.composable " ;
5
5
6
6
export default function useFetchPost ( ) {
7
7
const { endLoading, isLoading, startLoading } = useLoading ( ) ;
8
+ const { getCache, setCache } = useLocaleStorageCache ( ) ;
8
9
9
10
const post = ref ( null ) ;
10
11
11
- async function fetchPost ( id ) {
12
- const cachedPosts = StorageService . get ( "cached-posts" ) || { } ;
12
+ async function fetchPost ( id ) {
13
+ const cachedPosts = getCache ( "cached-posts" ) || { } ;
13
14
14
15
if ( cachedPosts [ `post-${ id } ` ] ) {
15
16
post . value = cachedPosts [ `post-${ id } ` ] ;
16
- return
17
- }
18
- else {
17
+ return ;
18
+ } else {
19
19
startLoading ( ) ;
20
20
return PostsServices . getOneById ( id )
21
- . then ( ( response ) => {
22
- post . value = response . data ;
23
- StorageService . set ( 'cached-posts' , { ...cachedPosts , [ `post-${ id } ` ] : response . data } )
24
- return response ;
25
- } )
26
- . finally ( ( ) => {
27
- endLoading ( ) ;
28
- } ) ;
21
+ . then ( ( response ) => {
22
+ post . value = response . data ;
23
+ setCache ( 'cached-posts' , { ...cachedPosts , [ `post-${ id } ` ] : response . data } )
24
+ return response ;
25
+ } )
26
+ . finally ( ( ) => {
27
+ endLoading ( ) ;
28
+ } ) ;
29
29
}
30
30
}
31
31
You can’t perform that action at this time.
0 commit comments