`ngrx` - 如何使用 `effects` 实现缓存

`ngrx` - how to implement the caching with `effects`

我喜欢将缓存与我的效果相结合。但没有结果。我这样做的方式可能不正确。

任何人纠正我以解决问题。

这是我的代码:

constructor(private courseService:CourseService, private actions:Actions,
    private store:Store<StateSetupConfig>){}

@Effect()
    EffLoadCourse = this.actions.pipe(
        ofType(LoadCourse.TYPE),
        withLatestFrom(this.store.pipe(select(subscribes.getCourses)),
            (action, courses) => {
                console.log('courses ::', courses)//getting logged,
                return courses
            }

        ),
       //but each time backend call initiated!!?
        mergeMap((action:LoadCourse) => this.courseService.getCourse().pipe(
            map((courses:ModelCourse[]) => (new LoadCourseSuccess(courses))),
            catchError(err => of(new LoadCourseFail(err)))
        ))

    )

问题是,尽管我返回当前页面,但我收到后端调用而不是从商店提供。哪里错了?这里需要添加什么条件?

提前致谢。

我想出了以下解决方案:它对我有用!!

@Effect()
        EffLoadCourse = this.actions.pipe(
            ofType(LoadCourse.TYPE),
            withLatestFrom(
                this.store.pipe(select(subscribes.getCourses)), //getting it from store
                (action:LoadCourse, courses: ModelCourse[]) => courses
            ),
            mergeMap((courses:ModelCourse[]) => {

                if(courses.length){
                    return of(courses).pipe(
                        map((courses:ModelCourse[]) => (new LoadCourseSuccess(courses))),
                        catchError(err => of(new LoadCourseFail(err)))
                    )
                }

                return this.courseService.getCourse().pipe(
                    map((courses:ModelCourse[]) => (new LoadCourseSuccess(courses))),
                    catchError(err => of(new LoadCourseFail(err)))
                )
            })
        )