Skip to content

Commit

Permalink
Add early expiration of Access Token in cache (#233)
Browse files Browse the repository at this point in the history
* Add early expiration of Access Token in cache

* Add tests and early expire the token after 80%
  • Loading branch information
B-Galati authored and Luís Rudge committed Oct 3, 2019
1 parent fd21bc5 commit a09dd1c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions __tests__/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ describe('cache', () => {
user: { name: 'Test' }
}
});
jest.advanceTimersByTime(1001);
jest.advanceTimersByTime(799);
expect(Object.keys(cache.cache).length).toBe(1);
jest.advanceTimersByTime(1);
expect(Object.keys(cache.cache).length).toBe(0);
});
it('expires after `user.exp` when `user.exp` < `expires_in`', () => {
Expand All @@ -73,7 +75,9 @@ describe('cache', () => {
user: { name: 'Test' }
}
});
jest.advanceTimersByTime(1001);
jest.advanceTimersByTime(799);
expect(Object.keys(cache.cache).length).toBe(1);
jest.advanceTimersByTime(1);
expect(Object.keys(cache.cache).length).toBe(0);
});
});
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const createKey = (e: CacheKeyData) => `${e.audience}::${e.scope}`;
const getExpirationTimeoutInMilliseconds = (expiresIn: number, exp: number) => {
const expTime =
(new Date(exp * 1000).getTime() - new Date().getTime()) / 1000;
return Math.min(expiresIn, expTime) * 1000;
return Math.min(expiresIn, expTime) * 1000 * 0.8;
};

export default class Cache {
Expand Down

0 comments on commit a09dd1c

Please sign in to comment.