Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support inv-by cache dependencies from HAL bodies #426

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export default class Client {
cacheState(state: State) {

this.cache.store(state);
for(const invByLink of state.links.getMany('inv-by')) {
this.addCacheDependency(resolve(invByLink), state.uri);
}

const resource = this.resources.get(state.uri);
if (resource) {
// We have a resource for this object, notify it as well.
Expand All @@ -175,9 +179,29 @@ export default class Client {
* expire, if another one expires.
*
* A server can populate this list using the `inv-by' link.
*
* @deprecated This property will go private in a future release.
*/
public cacheDependencies: Map<string, Set<string>> = new Map();

/**
* Adds a cache dependency between two resources.
*
* If the 'target' resource ever expires, it will cause 'dependentUri' to
* also expire.
*
* Both argument MUST be absolute urls.
*/
addCacheDependency(targetUri: string, dependentUri: string): void {

if (this.cacheDependencies.has(targetUri)) {
this.cacheDependencies.get(targetUri)!.add(dependentUri);
} else {
this.cacheDependencies.set(targetUri, new Set([dependentUri]));
}

}

/**
* Helper function for clearing the cache for a resource.
*
Expand Down
15 changes: 0 additions & 15 deletions src/middlewares/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ export default function(client: Client): FetchMiddleware {

const response = await next(request);

// If the response had a Link: rel=inv-by header, it means that when the
// target uri's cache expires, the uri of this resource should also
// expire.
if (response.headers.has('Link')) {
for (const httpLink of LinkHeader.parse(response.headers.get('Link')!).rel('inv-by')) {
const uri = resolve(request.url, httpLink.uri);
if (client.cacheDependencies.has(uri)) {
client.cacheDependencies.get(uri)!.add(request.url);
} else {
client.cacheDependencies.set(uri, new Set([request.url]));
}
}
}


if (isSafeMethod(request.method)) {
return response;
}
Expand Down
100 changes: 74 additions & 26 deletions test/unit/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,37 +152,85 @@ describe('Client', () => {
});
});

it('should invalidate dependent resources if they were previously linked with "inv-by"', async () => {

const client = new Client('https://example.org');
client.cache.store(new BaseState({
client,
uri: 'https://example.org/foo',
data: 'hello',
headers: new Headers(),
links: new Links('http://example.org/foo'),
}));

const request1 = new Request('https://example.org/foo', {
method: 'GET',
});
describe('inv-by links', () => {

const headers = new Headers();
headers.append('Link', '</bar>; rel="inv-by"');
client.use( async req => {
return new Response('OK', { headers });
});
it('should invalidate dependent resources if they were previously linked with "inv-by"', async () => {

await client.fetcher.fetch(request1);
expect(client.cache.has('https://example.org/foo')).to.equal(true);
const client = new Client('https://example.org');
client.cacheState(new BaseState({
client,
uri: 'https://example.org/foo',
data: 'hello',
headers: new Headers(),
links: new Links('http://example.org/foo'),
}));

const headers = new Headers();
headers.append('Link', '</bar>; rel="inv-by"');
client.use( async req => {
return new Response('OK', { headers });
});

// Get the representation of /foo, which should set up the inv-by link
await client.go('https://example.org/foo').refresh();

expect(client.cache.has('https://example.org/foo')).to.equal(true);

// Lets invalidate /bar, which should also invalidate /foo
const request2 = new Request('https://example.org/bar', {
method: 'PUT',
});
await client.fetcher.fetch(request2);

expect(client.cache.has('https://example.org/foo')).to.equal(false);

// Lets invalidate /bar, which should also invalidate /foo
const request2 = new Request('https://example.org/bar', {
method: 'PUT',
});
await client.fetcher.fetch(request2);

expect(client.cache.has('https://example.org/foo')).to.equal(false);
it('should also work when the inv-by dependency was set up via a link in an embedded HAL document', async() => {

const embeddedLinks = new Links('https://example.org/parent');
embeddedLinks.set('inv-by', '/bar');

const client = new Client('https://example.org');
client.cacheState(new BaseState({
client,
uri: 'https://example.org/parent',
data: 'hello',
headers: new Headers(),
links: new Links('http://example.org/parent'),
embedded: [

// This is the real resource we're interested in expiring
new BaseState({
client,
uri: 'https://example.org/foo',
links: embeddedLinks,
headers: new Headers(),
data: 'sup',
}),
]
}));

const headers = new Headers();
headers.append('Link', '</bar>; rel="inv-by"');
client.use( async req => {
return new Response('OK', { headers });
});

// Get the representation of /foo, which should set up the inv-by link
await client.go('https://example.org/foo').refresh();

expect(client.cache.has('https://example.org/foo')).to.equal(true);

// Lets invalidate /bar, which should also invalidate /foo
const request2 = new Request('https://example.org/bar', {
method: 'PUT',
});
await client.fetcher.fetch(request2);

expect(client.cache.has('https://example.org/foo')).to.equal(false);

});

});

Expand Down