File tree Expand file tree Collapse file tree 2 files changed +52
-1
lines changed
Expand file tree Collapse file tree 2 files changed +52
-1
lines changed Original file line number Diff line number Diff line change 1+ var CacheController = require ( '../src/Controllers/CacheController.js' ) . default ;
2+ var InMemoryCacheAdapter = require ( '../src/Adapters/Cache/InMemoryCacheAdapter' ) . default ;
3+ var SchemaCache = require ( '../src/Controllers/SchemaCache' ) . default ;
4+
5+ describe ( 'SchemaCache' , ( ) => {
6+ var schemaCache ;
7+
8+ beforeEach ( ( ) => {
9+ var cacheAdapter = new InMemoryCacheAdapter ( { } ) ;
10+ var cacheController = new CacheController ( cacheAdapter , 'appId' ) ;
11+ schemaCache = new SchemaCache ( cacheController ) ;
12+ } ) ;
13+
14+ it ( 'can retrieve a single schema after all schemas stored' , ( done ) => {
15+ var allSchemas = [ {
16+ className : 'Class1'
17+ } , {
18+ className : 'Class2'
19+ } ] ;
20+ schemaCache . setAllClasses ( allSchemas ) ;
21+ schemaCache . getOneSchema ( 'Class2' ) . then ( ( schema ) => {
22+ expect ( schema ) . not . toBeNull ( ) ;
23+ done ( ) ;
24+ } ) ;
25+ } ) ;
26+
27+ it ( 'does not return all schemas after a single schema is stored' , ( done ) => {
28+ var schema = {
29+ className : 'Class1'
30+ } ;
31+ schemaCache . setOneSchema ( 'Class1' , schema ) ;
32+ schemaCache . getAllClasses ( ) . then ( ( allSchemas ) => {
33+ expect ( allSchemas ) . toBeNull ( ) ;
34+ done ( ) ;
35+ } ) ;
36+ } ) ;
37+ } ) ;
Original file line number Diff line number Diff line change @@ -50,7 +50,21 @@ export default class SchemaCache {
5050 if ( ! this . ttl ) {
5151 return Promise . resolve ( null ) ;
5252 }
53- return this . cache . get ( this . prefix + className ) ;
53+ return this . cache . get ( this . prefix + className ) . then ( ( schema ) => {
54+ if ( schema ) {
55+ return Promise . resolve ( schema ) ;
56+ }
57+ return this . cache . get ( this . prefix + MAIN_SCHEMA ) . then ( ( cachedSchemas ) => {
58+ cachedSchemas = cachedSchemas || [ ] ;
59+ schema = cachedSchemas . find ( ( cachedSchema ) => {
60+ return cachedSchema . className === className ;
61+ } ) ;
62+ if ( schema ) {
63+ return Promise . resolve ( schema ) ;
64+ }
65+ return Promise . resolve ( null ) ;
66+ } ) ;
67+ } ) ;
5468 }
5569
5670 clear ( ) {
You can’t perform that action at this time.
0 commit comments