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

Avoid copying entire cache on each optimistic read. #4319

Merged
merged 6 commits into from
Jan 17, 2019
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
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@

### Apollo Cache In-Memory (vNext)

- The speed and memory usage of optimistic reads and writes has been
improved dramatically using a new layering technique that does not
require copying the non-optimistic contents of the cache.
[PR #4319](https://github.com/apollographql/apollo-client/pull/4319/)

- The `RecordingCache` abstraction has been removed, and thus is no longer
exported from `apollo-cache-inmemory`.
[PR #4319](https://github.com/apollographql/apollo-client/pull/4319/)

- Export the optimism `wrap` function using ES2015 export syntax, instead of
CommonJS. <br/>
[@ardatan](https://github.com/ardatan) in [#4158](https://github.com/apollographql/apollo-client/pull/4158)


## Apollo Client (2.4.8)

### Apollo Client (2.4.8)
Expand Down
68 changes: 38 additions & 30 deletions packages/apollo-cache-inmemory/src/__tests__/recordingCache.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import { RecordingCache } from '../recordingCache';
import { OptimisticCacheLayer } from '../inMemoryCache';
import { ObjectCache } from '../objectCache';
import { NormalizedCacheObject } from '../types';

describe('RecordingCache', () => {
describe('OptimisticCacheLayer', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted just to git rm src/__tests__/recordingCache.ts, since src/recordingCache.ts no longer exists, but then I realized these tests could be adapted to use the new OptimisticCacheLayer class instead. Similar in spirit, though very different in implementation.

function makeLayer(root: ObjectCache) {
return new OptimisticCacheLayer('whatever', root, () => {});
}

describe('returns correct values during recording', () => {
const data = {
Human: { __typename: 'Human', name: 'Mark' },
Animal: { __typename: 'Mouse', name: '🐭' },
};
const dataToRecord = { Human: { __typename: 'Human', name: 'John' } };
let cache: RecordingCache;

const dataToRecord = {
Human: { __typename: 'Human', name: 'John' },
};

const underlyingCache = new ObjectCache(data);

let cache = makeLayer(underlyingCache);
beforeEach(() => {
cache = new RecordingCache({ ...data });
cache = makeLayer(underlyingCache);
});

it('should passthrough values if not defined in recording', () => {
cache.record(() => {
expect(cache.get('Human')).toBe(data.Human);
expect(cache.get('Animal')).toBe(data.Animal);
});
expect(cache.get('Human')).toBe(data.Human);
expect(cache.get('Animal')).toBe(data.Animal);
});

it('should return values defined during recording', () => {
const recording = cache.record(() => {
cache.set('Human', dataToRecord.Human);
expect(cache.get('Human')).toBe(dataToRecord.Human);
});
expect(recording.Human).toBe(dataToRecord.Human);
cache.set('Human', dataToRecord.Human);
expect(cache.get('Human')).toBe(dataToRecord.Human);
expect(underlyingCache.get('Human')).toBe(data.Human);
});

it('should return undefined for values deleted during recording', () => {
const recording = cache.record(() => {
expect(cache.get('Animal')).toBe(data.Animal);
// delete should be registered in the recording:
cache.delete('Animal');
expect(cache.get('Animal')).toBeUndefined();
});

expect(recording).toHaveProperty('Animal');
expect(cache.get('Animal')).toBe(data.Animal);
// delete should be registered in the recording:
cache.delete('Animal');
expect(cache.get('Animal')).toBeUndefined();
expect(cache.toObject()).toHaveProperty('Animal');
expect(underlyingCache.get('Animal')).toBe(data.Animal);
});
});

Expand All @@ -46,16 +50,20 @@ describe('RecordingCache', () => {
Human: { __typename: 'Human', name: 'Mark' },
Animal: { __typename: 'Mouse', name: '🐭' },
};
const dataToRecord = { Human: { __typename: 'Human', name: 'John' } };
let cache: RecordingCache;

const dataToRecord = {
Human: { __typename: 'Human', name: 'John' },
};

const underlyingCache = new ObjectCache(data);
let cache = makeLayer(underlyingCache);
let recording: NormalizedCacheObject;

beforeEach(() => {
cache = new RecordingCache({ ...data });
recording = cache.record(() => {
cache.set('Human', dataToRecord.Human);
cache.delete('Animal');
});
cache = makeLayer(underlyingCache);
cache.set('Human', dataToRecord.Human);
cache.delete('Animal');
recording = cache.toObject();
});

it('should contain the property indicating deletion', () => {
Expand All @@ -70,7 +78,7 @@ describe('RecordingCache', () => {
});

it('should keep the original data unaffected', () => {
expect(cache.toObject()).toEqual(data);
expect(underlyingCache.toObject()).toEqual(data);
});
});
});
Loading