Skip to content

Commit be6dbde

Browse files
107155: Allow caching null objects
1 parent ca86437 commit be6dbde

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

src/app/core/cache/object-cache.reducer.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,25 @@ export function objectCacheReducer(state = initialState, action: ObjectCacheActi
166166
* the new state, with the object added, or overwritten.
167167
*/
168168
function addToObjectCache(state: ObjectCacheState, action: AddToObjectCacheAction): ObjectCacheState {
169-
const existing = state[action.payload.objectToCache._links.self.href] || {} as any;
169+
const cacheLink = hasValue(action.payload.objectToCache) ? action.payload.objectToCache._links.self.href : action.payload.alternativeLink;
170+
const existing = state[cacheLink] || {} as any;
170171
const newAltLinks = hasValue(action.payload.alternativeLink) ? [action.payload.alternativeLink] : [];
171-
return Object.assign({}, state, {
172-
[action.payload.objectToCache._links.self.href]: {
173-
data: action.payload.objectToCache,
174-
timeCompleted: action.payload.timeCompleted,
175-
msToLive: action.payload.msToLive,
176-
requestUUIDs: [action.payload.requestUUID, ...(existing.requestUUIDs || [])],
177-
dependentRequestUUIDs: existing.dependentRequestUUIDs || [],
178-
isDirty: isNotEmpty(existing.patches),
179-
patches: existing.patches || [],
180-
alternativeLinks: [...(existing.alternativeLinks || []), ...newAltLinks]
181-
} as ObjectCacheEntry
182-
});
172+
if (hasValue(cacheLink)) {
173+
return Object.assign({}, state, {
174+
[cacheLink]: {
175+
data: action.payload.objectToCache,
176+
timeCompleted: action.payload.timeCompleted,
177+
msToLive: action.payload.msToLive,
178+
requestUUIDs: [action.payload.requestUUID, ...(existing.requestUUIDs || [])],
179+
dependentRequestUUIDs: existing.dependentRequestUUIDs || [],
180+
isDirty: isNotEmpty(existing.patches),
181+
patches: existing.patches || [],
182+
alternativeLinks: [...(existing.alternativeLinks || []), ...newAltLinks]
183+
} as ObjectCacheEntry
184+
});
185+
} else {
186+
return state;
187+
}
183188
}
184189

185190
/**

src/app/core/cache/object-cache.service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ export class ObjectCacheService {
6363
* An optional alternative link to this object
6464
*/
6565
add(object: CacheableObject, msToLive: number, requestUUID: string, alternativeLink?: string): void {
66-
object = this.linkService.removeResolvedLinks(object); // Ensure the object we're storing has no resolved links
66+
if (hasValue(object)) {
67+
object = this.linkService.removeResolvedLinks(object); // Ensure the object we're storing has no resolved links
68+
}
6769
this.store.dispatch(new AddToObjectCacheAction(object, new Date().getTime(), msToLive, requestUUID, alternativeLink));
6870
}
6971

@@ -139,11 +141,15 @@ export class ObjectCacheService {
139141
}
140142
),
141143
map((entry: ObjectCacheEntry) => {
142-
const type: GenericConstructor<T> = getClassForType((entry.data as any).type);
143-
if (typeof type !== 'function') {
144-
throw new Error(`${type} is not a valid constructor for ${JSON.stringify(entry.data)}`);
144+
if (hasValue(entry.data)) {
145+
const type: GenericConstructor<T> = getClassForType((entry.data as any).type);
146+
if (typeof type !== 'function') {
147+
throw new Error(`${type} is not a valid constructor for ${JSON.stringify(entry.data)}`);
148+
}
149+
return Object.assign(new type(), entry.data) as T;
150+
} else {
151+
return null;
145152
}
146-
return Object.assign(new type(), entry.data) as T;
147153
})
148154
);
149155
}

src/app/core/data/dspace-rest-response-parsing.service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export class DspaceRestResponseParsingService implements ResponseParsingService
109109
if (hasValue(match)) {
110110
embedAltUrl = new URLCombiner(embedAltUrl, `?size=${match.size}`).toString();
111111
}
112+
if (data._embedded[property] == null) {
113+
this.addToObjectCache(null, request, data, embedAltUrl);
114+
}
112115
this.process<ObjectDomain>(data._embedded[property], request, embedAltUrl);
113116
});
114117
}
@@ -226,7 +229,7 @@ export class DspaceRestResponseParsingService implements ResponseParsingService
226229
* @param alternativeURL an alternative url that can be used to retrieve the object
227230
*/
228231
addToObjectCache(co: CacheableObject, request: RestRequest, data: any, alternativeURL?: string): void {
229-
if (!isCacheableObject(co)) {
232+
if (hasValue(co) && !isCacheableObject(co)) {
230233
const type = hasValue(data) && hasValue(data.type) ? data.type : 'object';
231234
let dataJSON: string;
232235
if (hasValue(data._embedded)) {
@@ -240,7 +243,7 @@ export class DspaceRestResponseParsingService implements ResponseParsingService
240243
return;
241244
}
242245

243-
if (alternativeURL === co._links.self.href) {
246+
if (hasValue(co) && alternativeURL === co._links.self.href) {
244247
alternativeURL = undefined;
245248
}
246249

src/app/core/index/index.effects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class UUIDIndexEffects {
2727
addObject$ = createEffect(() => this.actions$
2828
.pipe(
2929
ofType(ObjectCacheActionTypes.ADD),
30-
filter((action: AddToObjectCacheAction) => hasValue(action.payload.objectToCache.uuid)),
30+
filter((action: AddToObjectCacheAction) => hasValue(action.payload.objectToCache) && hasValue(action.payload.objectToCache.uuid)),
3131
map((action: AddToObjectCacheAction) => {
3232
return new AddToIndexAction(
3333
IndexName.OBJECT,
@@ -46,7 +46,7 @@ export class UUIDIndexEffects {
4646
ofType(ObjectCacheActionTypes.ADD),
4747
map((action: AddToObjectCacheAction) => {
4848
const alternativeLink = action.payload.alternativeLink;
49-
const selfLink = action.payload.objectToCache._links.self.href;
49+
const selfLink = hasValue(action.payload.objectToCache) ? action.payload.objectToCache._links.self.href : alternativeLink;
5050
if (hasValue(alternativeLink) && alternativeLink !== selfLink) {
5151
return new AddToIndexAction(
5252
IndexName.ALTERNATIVE_OBJECT_LINK,

0 commit comments

Comments
 (0)