58789: Ability to clear request cache from index

This commit is contained in:
Kristof De Langhe
2018-11-26 13:18:17 +01:00
parent c493fc4003
commit 8be40c31f6
5 changed files with 133 additions and 11 deletions

View File

@@ -10,7 +10,8 @@ export const RequestActionTypes = {
CONFIGURE: type('dspace/core/data/request/CONFIGURE'), CONFIGURE: type('dspace/core/data/request/CONFIGURE'),
EXECUTE: type('dspace/core/data/request/EXECUTE'), EXECUTE: type('dspace/core/data/request/EXECUTE'),
COMPLETE: type('dspace/core/data/request/COMPLETE'), COMPLETE: type('dspace/core/data/request/COMPLETE'),
RESET_TIMESTAMPS: type('dspace/core/data/request/RESET_TIMESTAMPS') RESET_TIMESTAMPS: type('dspace/core/data/request/RESET_TIMESTAMPS'),
REMOVE: type('dspace/core/data/request/REMOVE')
}; };
/* tslint:disable:max-classes-per-file */ /* tslint:disable:max-classes-per-file */
@@ -82,6 +83,24 @@ export class ResetResponseTimestampsAction implements Action {
} }
} }
/**
* An ngrx action to remove a cached request
*/
export class RequestRemoveAction implements Action {
type = RequestActionTypes.REMOVE;
uuid: string;
/**
* Create a new RequestRemoveAction
*
* @param uuid
* the request's uuid
*/
constructor(uuid: string) {
this.uuid = uuid
}
}
/* tslint:enable:max-classes-per-file */ /* tslint:enable:max-classes-per-file */
/** /**
@@ -91,4 +110,5 @@ export type RequestAction
= RequestConfigureAction = RequestConfigureAction
| RequestExecuteAction | RequestExecuteAction
| RequestCompleteAction | RequestCompleteAction
| ResetResponseTimestampsAction; | ResetResponseTimestampsAction
| RequestRemoveAction;

View File

@@ -1,6 +1,6 @@
import { import {
RequestActionTypes, RequestAction, RequestConfigureAction, RequestActionTypes, RequestAction, RequestConfigureAction,
RequestExecuteAction, RequestCompleteAction, ResetResponseTimestampsAction RequestExecuteAction, RequestCompleteAction, ResetResponseTimestampsAction, RequestRemoveAction
} from './request.actions'; } from './request.actions';
import { RestRequest } from './request.models'; import { RestRequest } from './request.models';
import { RestResponse } from '../cache/response.models'; import { RestResponse } from '../cache/response.models';
@@ -38,6 +38,10 @@ export function requestReducer(state = initialState, action: RequestAction): Req
return resetResponseTimestamps(state, action as ResetResponseTimestampsAction); return resetResponseTimestamps(state, action as ResetResponseTimestampsAction);
} }
case RequestActionTypes.REMOVE: {
return removeRequest(state, action as RequestRemoveAction);
}
default: { default: {
return state; return state;
} }
@@ -95,3 +99,13 @@ function resetResponseTimestamps(state: RequestState, action: ResetResponseTimes
}); });
return newState; return newState;
} }
function removeRequest(state: RequestState, action: RequestRemoveAction): RequestState {
const newState = Object.create(null);
for (const value in state) {
if (value !== action.uuid) {
newState[value] = state[value];
}
}
return newState;
}

View File

@@ -15,23 +15,23 @@ import {
import { race as observableRace } from 'rxjs'; import { race as observableRace } from 'rxjs';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { MemoizedSelector, select, Store } from '@ngrx/store'; import { createSelector, MemoizedSelector, select, Store } from '@ngrx/store';
import { hasNoValue, hasValue, isNotUndefined } from '../../shared/empty.util'; import { hasNoValue, hasValue, isNotEmpty, isNotUndefined } from '../../shared/empty.util';
import { CacheableObject } from '../cache/object-cache.reducer'; import { CacheableObject } from '../cache/object-cache.reducer';
import { ObjectCacheService } from '../cache/object-cache.service'; import { ObjectCacheService } from '../cache/object-cache.service';
import { DSOSuccessResponse, RestResponse } from '../cache/response.models'; import { DSOSuccessResponse, RestResponse } from '../cache/response.models';
import { coreSelector, CoreState } from '../core.reducers'; import { coreSelector, CoreState } from '../core.reducers';
import { IndexName } from '../index/index.reducer'; import { IndexName, IndexState } from '../index/index.reducer';
import { pathSelector } from '../shared/selectors'; import { pathSelector } from '../shared/selectors';
import { UUIDService } from '../shared/uuid.service'; import { UUIDService } from '../shared/uuid.service';
import { RequestConfigureAction, RequestExecuteAction } from './request.actions'; import { RequestConfigureAction, RequestExecuteAction, RequestRemoveAction } from './request.actions';
import { GetRequest, RestRequest } from './request.models'; import { GetRequest, RestRequest } from './request.models';
import { RequestEntry } from './request.reducer'; import { RequestEntry } from './request.reducer';
import { CommitSSBAction } from '../cache/server-sync-buffer.actions'; import { CommitSSBAction } from '../cache/server-sync-buffer.actions';
import { RestRequestMethod } from './rest-request-method'; import { RestRequestMethod } from './rest-request-method';
import { getResponseFromEntry } from '../shared/operators'; import { getResponseFromEntry } from '../shared/operators';
import { AddToIndexAction } from '../index/index.actions'; import { AddToIndexAction, RemoveFromIndexBySubstringAction } from '../index/index.actions';
@Injectable() @Injectable()
export class RequestService { export class RequestService {
@@ -39,7 +39,8 @@ export class RequestService {
constructor(private objectCache: ObjectCacheService, constructor(private objectCache: ObjectCacheService,
private uuidService: UUIDService, private uuidService: UUIDService,
private store: Store<CoreState>) { private store: Store<CoreState>,
private indexStore: Store<IndexState>) {
} }
private entryFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, RequestEntry> { private entryFromUUIDSelector(uuid: string): MemoizedSelector<CoreState, RequestEntry> {
@@ -54,6 +55,25 @@ export class RequestService {
return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.UUID_MAPPING, uuid); return pathSelector<CoreState, string>(coreSelector, 'index', IndexName.UUID_MAPPING, uuid);
} }
private uuidsFromHrefSubstringSelector(selector: MemoizedSelector<any, IndexState>, name: string, href: string): MemoizedSelector<any, string[]> {
return createSelector(selector, (state: IndexState) => this.getUuidsFromHrefSubstring(state, name, href));
}
private getUuidsFromHrefSubstring(state: IndexState, name: string, href: string): string[] {
let result = [];
if (isNotEmpty(state)) {
const subState = state[name];
if (isNotEmpty(subState)) {
for (const value in subState) {
if (value.indexOf(href) > -1) {
result = [...result, subState[value]];
}
}
}
}
return result;
}
generateRequestId(): string { generateRequestId(): string {
return `client/${this.uuidService.generate()}`; return `client/${this.uuidService.generate()}`;
} }
@@ -119,6 +139,32 @@ export class RequestService {
} }
} }
/**
* Remove all request cache providing (part of) the href
* This also includes href-to-uuid index cache
* @param href A substring of the request(s) href
*/
removeByHrefSubstring(href: string) {
this.store.pipe(
select(this.uuidsFromHrefSubstringSelector(pathSelector<CoreState, IndexState>(coreSelector, 'index'), IndexName.REQUEST, href)),
take(1)
).subscribe((uuids: string[]) => {
for (const uuid of uuids) {
this.removeByUuid(uuid);
}
});
this.requestsOnTheirWayToTheStore = this.requestsOnTheirWayToTheStore.filter((reqHref: string) => reqHref.indexOf(href) < 0);
this.indexStore.dispatch(new RemoveFromIndexBySubstringAction(IndexName.REQUEST, href));
}
/**
* Remove request cache using the request's UUID
* @param uuid
*/
removeByUuid(uuid: string) {
this.store.dispatch(new RequestRemoveAction(uuid));
}
/** /**
* Check if a request is in the cache or if it's still pending * Check if a request is in the cache or if it's still pending
* @param {GetRequest} request The request to check * @param {GetRequest} request The request to check

View File

@@ -8,7 +8,8 @@ import { IndexName } from './index.reducer';
*/ */
export const IndexActionTypes = { export const IndexActionTypes = {
ADD: type('dspace/core/index/ADD'), ADD: type('dspace/core/index/ADD'),
REMOVE_BY_VALUE: type('dspace/core/index/REMOVE_BY_VALUE') REMOVE_BY_VALUE: type('dspace/core/index/REMOVE_BY_VALUE'),
REMOVE_BY_SUBSTRING: type('dspace/core/index/REMOVE_BY_SUBSTRING')
}; };
/* tslint:disable:max-classes-per-file */ /* tslint:disable:max-classes-per-file */
@@ -60,6 +61,30 @@ export class RemoveFromIndexByValueAction implements Action {
this.payload = { name, value }; this.payload = { name, value };
} }
}
/**
* An ngrx action to remove multiple values from the index by substring
*/
export class RemoveFromIndexBySubstringAction implements Action {
type = IndexActionTypes.REMOVE_BY_SUBSTRING;
payload: {
name: IndexName,
value: string
};
/**
* Create a new RemoveFromIndexByValueAction
*
* @param name
* the name of the index to remove from
* @param value
* the value to remove the UUID for
*/
constructor(name: IndexName, value: string) {
this.payload = { name, value };
}
} }
/* tslint:enable:max-classes-per-file */ /* tslint:enable:max-classes-per-file */

View File

@@ -2,7 +2,7 @@ import {
IndexAction, IndexAction,
IndexActionTypes, IndexActionTypes,
AddToIndexAction, AddToIndexAction,
RemoveFromIndexByValueAction RemoveFromIndexByValueAction, RemoveFromIndexBySubstringAction
} from './index.actions'; } from './index.actions';
export enum IndexName { export enum IndexName {
@@ -31,6 +31,10 @@ export function indexReducer(state = initialState, action: IndexAction): IndexSt
return removeFromIndexByValue(state, action as RemoveFromIndexByValueAction) return removeFromIndexByValue(state, action as RemoveFromIndexByValueAction)
} }
case IndexActionTypes.REMOVE_BY_SUBSTRING: {
return removeFromIndexBySubstring(state, action as RemoveFromIndexBySubstringAction)
}
default: { default: {
return state; return state;
} }
@@ -60,3 +64,16 @@ function removeFromIndexByValue(state: IndexState, action: RemoveFromIndexByValu
[action.payload.name]: newSubState [action.payload.name]: newSubState
}); });
} }
function removeFromIndexBySubstring(state: IndexState, action: RemoveFromIndexByValueAction): IndexState {
const subState = state[action.payload.name];
const newSubState = Object.create(null);
for (const value in subState) {
if (value.indexOf(action.payload.value) < 0) {
newSubState[value] = subState[value];
}
}
return Object.assign({}, state, {
[action.payload.name]: newSubState
});
}