mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
107155: Allow caching null objects
This commit is contained in:
31
src/app/core/cache/object-cache.reducer.ts
vendored
31
src/app/core/cache/object-cache.reducer.ts
vendored
@@ -166,20 +166,25 @@ export function objectCacheReducer(state = initialState, action: ObjectCacheActi
|
|||||||
* the new state, with the object added, or overwritten.
|
* the new state, with the object added, or overwritten.
|
||||||
*/
|
*/
|
||||||
function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheAction): ObjectCacheState {
|
function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheAction): ObjectCacheState {
|
||||||
const existing = state[action.payload.objectToCache._links.self.href] || {} as any;
|
const cacheLink = hasValue(action.payload.objectToCache) ? action.payload.objectToCache._links.self.href : action.payload.alternativeLink;
|
||||||
|
const existing = state[cacheLink] || {} as any;
|
||||||
const newAltLinks = hasValue(action.payload.alternativeLink) ? [action.payload.alternativeLink] : [];
|
const newAltLinks = hasValue(action.payload.alternativeLink) ? [action.payload.alternativeLink] : [];
|
||||||
return Object.assign({}, state, {
|
if (hasValue(cacheLink)) {
|
||||||
[action.payload.objectToCache._links.self.href]: {
|
return Object.assign({}, state, {
|
||||||
data: action.payload.objectToCache,
|
[cacheLink]: {
|
||||||
timeCompleted: action.payload.timeCompleted,
|
data: action.payload.objectToCache,
|
||||||
msToLive: action.payload.msToLive,
|
timeCompleted: action.payload.timeCompleted,
|
||||||
requestUUIDs: [action.payload.requestUUID, ...(existing.requestUUIDs || [])],
|
msToLive: action.payload.msToLive,
|
||||||
dependentRequestUUIDs: existing.dependentRequestUUIDs || [],
|
requestUUIDs: [action.payload.requestUUID, ...(existing.requestUUIDs || [])],
|
||||||
isDirty: isNotEmpty(existing.patches),
|
dependentRequestUUIDs: existing.dependentRequestUUIDs || [],
|
||||||
patches: existing.patches || [],
|
isDirty: isNotEmpty(existing.patches),
|
||||||
alternativeLinks: [...(existing.alternativeLinks || []), ...newAltLinks]
|
patches: existing.patches || [],
|
||||||
} as ObjectCacheEntry
|
alternativeLinks: [...(existing.alternativeLinks || []), ...newAltLinks]
|
||||||
});
|
} as ObjectCacheEntry
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
16
src/app/core/cache/object-cache.service.ts
vendored
16
src/app/core/cache/object-cache.service.ts
vendored
@@ -63,7 +63,9 @@ export class ObjectCacheService {
|
|||||||
* An optional alternative link to this object
|
* An optional alternative link to this object
|
||||||
*/
|
*/
|
||||||
add(object: CacheableObject, msToLive: number, requestUUID: string, alternativeLink?: string): void {
|
add(object: CacheableObject, msToLive: number, requestUUID: string, alternativeLink?: string): void {
|
||||||
object = this.linkService.removeResolvedLinks(object); // Ensure the object we're storing has no resolved links
|
if (hasValue(object)) {
|
||||||
|
object = this.linkService.removeResolvedLinks(object); // Ensure the object we're storing has no resolved links
|
||||||
|
}
|
||||||
this.store.dispatch(new AddToObjectCacheAction(object, new Date().getTime(), msToLive, requestUUID, alternativeLink));
|
this.store.dispatch(new AddToObjectCacheAction(object, new Date().getTime(), msToLive, requestUUID, alternativeLink));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,11 +141,15 @@ export class ObjectCacheService {
|
|||||||
}
|
}
|
||||||
),
|
),
|
||||||
map((entry: ObjectCacheEntry) => {
|
map((entry: ObjectCacheEntry) => {
|
||||||
const type: GenericConstructor<T> = getClassForType((entry.data as any).type);
|
if (hasValue(entry.data)) {
|
||||||
if (typeof type !== 'function') {
|
const type: GenericConstructor<T> = getClassForType((entry.data as any).type);
|
||||||
throw new Error(`${type} is not a valid constructor for ${JSON.stringify(entry.data)}`);
|
if (typeof type !== 'function') {
|
||||||
|
throw new Error(`${type} is not a valid constructor for ${JSON.stringify(entry.data)}`);
|
||||||
|
}
|
||||||
|
return Object.assign(new type(), entry.data) as T;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return Object.assign(new type(), entry.data) as T;
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -109,6 +109,9 @@ export class DspaceRestResponseParsingService implements ResponseParsingService
|
|||||||
if (hasValue(match)) {
|
if (hasValue(match)) {
|
||||||
embedAltUrl = new URLCombiner(embedAltUrl, `?size=${match.size}`).toString();
|
embedAltUrl = new URLCombiner(embedAltUrl, `?size=${match.size}`).toString();
|
||||||
}
|
}
|
||||||
|
if (data._embedded[property] == null) {
|
||||||
|
this.addToObjectCache(null, request, data, embedAltUrl);
|
||||||
|
}
|
||||||
this.process<ObjectDomain>(data._embedded[property], request, embedAltUrl);
|
this.process<ObjectDomain>(data._embedded[property], request, embedAltUrl);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -226,7 +229,7 @@ export class DspaceRestResponseParsingService implements ResponseParsingService
|
|||||||
* @param alternativeURL an alternative url that can be used to retrieve the object
|
* @param alternativeURL an alternative url that can be used to retrieve the object
|
||||||
*/
|
*/
|
||||||
addToObjectCache(co: CacheableObject, request: RestRequest, data: any, alternativeURL?: string): void {
|
addToObjectCache(co: CacheableObject, request: RestRequest, data: any, alternativeURL?: string): void {
|
||||||
if (!isCacheableObject(co)) {
|
if (hasValue(co) && !isCacheableObject(co)) {
|
||||||
const type = hasValue(data) && hasValue(data.type) ? data.type : 'object';
|
const type = hasValue(data) && hasValue(data.type) ? data.type : 'object';
|
||||||
let dataJSON: string;
|
let dataJSON: string;
|
||||||
if (hasValue(data._embedded)) {
|
if (hasValue(data._embedded)) {
|
||||||
@@ -240,7 +243,7 @@ export class DspaceRestResponseParsingService implements ResponseParsingService
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alternativeURL === co._links.self.href) {
|
if (hasValue(co) && alternativeURL === co._links.self.href) {
|
||||||
alternativeURL = undefined;
|
alternativeURL = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -27,7 +27,7 @@ export class UUIDIndexEffects {
|
|||||||
addObject$ = createEffect(() => this.actions$
|
addObject$ = createEffect(() => this.actions$
|
||||||
.pipe(
|
.pipe(
|
||||||
ofType(ObjectCacheActionTypes.ADD),
|
ofType(ObjectCacheActionTypes.ADD),
|
||||||
filter((action: AddToObjectCacheAction) => hasValue(action.payload.objectToCache.uuid)),
|
filter((action: AddToObjectCacheAction) => hasValue(action.payload.objectToCache) && hasValue(action.payload.objectToCache.uuid)),
|
||||||
map((action: AddToObjectCacheAction) => {
|
map((action: AddToObjectCacheAction) => {
|
||||||
return new AddToIndexAction(
|
return new AddToIndexAction(
|
||||||
IndexName.OBJECT,
|
IndexName.OBJECT,
|
||||||
@@ -46,7 +46,7 @@ export class UUIDIndexEffects {
|
|||||||
ofType(ObjectCacheActionTypes.ADD),
|
ofType(ObjectCacheActionTypes.ADD),
|
||||||
map((action: AddToObjectCacheAction) => {
|
map((action: AddToObjectCacheAction) => {
|
||||||
const alternativeLink = action.payload.alternativeLink;
|
const alternativeLink = action.payload.alternativeLink;
|
||||||
const selfLink = action.payload.objectToCache._links.self.href;
|
const selfLink = hasValue(action.payload.objectToCache) ? action.payload.objectToCache._links.self.href : alternativeLink;
|
||||||
if (hasValue(alternativeLink) && alternativeLink !== selfLink) {
|
if (hasValue(alternativeLink) && alternativeLink !== selfLink) {
|
||||||
return new AddToIndexAction(
|
return new AddToIndexAction(
|
||||||
IndexName.ALTERNATIVE_OBJECT_LINK,
|
IndexName.ALTERNATIVE_OBJECT_LINK,
|
||||||
|
Reference in New Issue
Block a user