Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

caching for listAll methods

jonaswinkler1d8765100c7d
diff --git a/src-ui/src/app/services/rest/abstract-paperless-service.ts b/src-ui/src/app/services/rest/abstract-paperless-service.ts
index 16064c702..3feed320e 100644
--- a/src-ui/src/app/services/rest/abstract-paperless-service.ts
+++ b/src-ui/src/app/services/rest/abstract-paperless-service.ts
@@ -1,5 +1,6 @@
import { HttpClient, HttpParams } from '@angular/common/http'
-import { Observable } from 'rxjs'
+import { Observable, of, Subject } from 'rxjs'
+import { map, publishReplay, refCount } from 'rxjs/operators'
import { ObjectWithId } from 'src/app/data/object-with-id'
import { Results } from 'src/app/data/results'
import { environment } from 'src/environments/environment'
@@ -51,8 +52,28 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
return this.http.get<Results<T>>(this.getResourceUrl(), {params: httpParams})
}
+ private _listAll: Observable<Results<T>>
+
listAll(ordering?: string, extraParams?): Observable<Results<T>> {
- return this.list(1, 100000, ordering, extraParams)
+ if (!this._listAll) {
+ this._listAll = this.list(1, 100000, ordering, extraParams).pipe(
+ publishReplay(1),
+ refCount()
+ )
+ }
+ return this._listAll
+ }
+
+ getCached(id: number): Observable<T> {
+ return this.listAll().pipe(
+ map(list => list.results.find(o => o.id == id))
+ )
+ }
+
+ getCachedMany(ids: number[]): Observable<T[]> {
+ return this.listAll().pipe(
+ map(list => ids.map(id => list.results.find(o => o.id == id)))
+ )
}
get(id: number): Observable<T> {
@@ -60,14 +81,17 @@ export abstract class AbstractPaperlessService<T extends ObjectWithId> {
}
create(o: T): Observable<T> {
+ this._listAll = null
return this.http.post<T>(this.getResourceUrl(), o)
}
delete(o: T): Observable<any> {
+ this._listAll = null
return this.http.delete(this.getResourceUrl(o.id))
}
update(o: T): Observable<T> {
+ this._listAll = null
return this.http.put<T>(this.getResourceUrl(o.id), o)
}
}
\ No newline at end of file