Skip to content

Commit c9056ad

Browse files
committed
fix(Cache): Did not clear cache correct
1 parent 318f464 commit c9056ad

File tree

4 files changed

+25
-51
lines changed

4 files changed

+25
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const LanguagesSchema = new mongoose.Schema({
2323
},
2424
});
2525

26-
const Languages = mongoose.model('UserModel', UserSchema)
26+
const Languages = mongoose.model('Languages', LanguagesSchema)
2727
const LanguagesTC = composeWithDataLoader(composeWithMongoose(Languages),{cacheExpiration: 700})
2828
```
2929

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"babel-runtime": "^6.22.0",
3838
"dataloader": "^1.3.0",
39+
"md5": "^2.2.1",
3940
"object-path": "^0.11.3",
4041
"string-hash": "^1.1.1"
4142
},

src/composeWithDataLoader.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { TypeComposer } from 'graphql-compose'
22
import DataLoader from 'dataloader'
3-
import SingleContinous from './singleContinous'
4-
3+
import md5 from 'md5'
54
import {
65
dataloaderOptions
76
} from './definitions'
@@ -43,7 +42,9 @@ export function composeWithDataLoader(
4342
typeComposer.setResolver( 'findById',
4443
findByIdResolver.wrapResolve(next => rp => {
4544
if (options.removeProjection) delete rp.projection
46-
SingleContinous.run(findByIdLoader, rp, 'findById', options)
45+
setTimeout(() => {
46+
let res = findByIdLoader.clear(rp)
47+
},options.cacheExpiration)
4748
return findByIdLoader.load(rp)
4849
})
4950
)
@@ -63,7 +64,9 @@ export function composeWithDataLoader(
6364
typeComposer.setResolver(
6465
'findByIds',
6566
findByIdsResolver.wrapResolve(fn => rp => {
66-
SingleContinous.run(findByIdsLoader, rp, 'findByIds', options)
67+
setTimeout(() => {
68+
let res = findByIdsLoader.clear(rp)
69+
},options.cacheExpiration)
6770
return findByIdsLoader.load(rp)
6871
})
6972
)
@@ -83,7 +86,9 @@ export function composeWithDataLoader(
8386
typeComposer.setResolver(
8487
'count',
8588
countResolver.wrapResolve(fn => rp => {
86-
SingleContinous.run(countLoader, rp, 'count', options)
89+
setTimeout(() => {
90+
let res = countLoader.clear(rp)
91+
},options.cacheExpiration)
8792
return countLoader.load(rp)
8893
})
8994
)
@@ -102,8 +107,10 @@ export function composeWithDataLoader(
102107

103108
typeComposer.setResolver(
104109
'findOne',
105-
findByIdsResolver.wrapResolve(fn => rp => {
106-
SingleContinous.run(findOneLoader, rp, 'findOne', options)
110+
findOneResolver.wrapResolve(fn => rp => {
111+
setTimeout(() => {
112+
let res = findOneLoader.clear(rp)
113+
},options.cacheExpiration)
107114
return findOneLoader.load(rp)
108115
})
109116
)
@@ -117,13 +124,15 @@ export function composeWithDataLoader(
117124
if (options.debug) console.log('New db request (findMany)')
118125
resolve(resolveParamsArray.map(rp => findManyResolver.resolve(rp)))
119126
}),
120-
{ cacheKeyFn: key => getHashKey(key)} )
127+
{ cacheKeyFn: key => getHashKey(key) } )
121128

122129
typeComposer.setResolver(
123130
'findMany',
124131
findManyResolver.wrapResolve(next => rp => {
125132
if (options.removeProjection) delete rp.projection
126-
SingleContinous.run(findManyLoader, rp, 'findMany', options)
133+
setTimeout(() => {
134+
let res = findManyLoader.clear(rp)
135+
},options.cacheExpiration)
127136
return findManyLoader.load(rp)
128137
})
129138
)
@@ -148,7 +157,9 @@ export function composeWithDataLoader(
148157
connectionFieldNames.map( field => projection.edges.node[field] = true)
149158
rp.projection = projection
150159
}
151-
SingleContinous.run(connectionLoader, rp, 'connection', options)
160+
setTimeout(() => {
161+
let res = connectionLoader.clear(rp)
162+
},options.cacheExpiration)
152163
return connectionLoader.load(rp)
153164
})
154165
)
@@ -157,11 +168,11 @@ export function composeWithDataLoader(
157168
const getHashKey = key =>{
158169
let object = {}
159170
Object.assign(object,
160-
{ args: key.args },
171+
{ args: key.args || {} },
161172
{ projection: key.projection || {} },
162173
{ rawQuery: JSON.stringify(key.rawQuery || {}) },
163174
{ context: JSON.stringify(key.context || {}) })
164-
let hash = JSON.stringify(object).split("").reduce((a,b)=>{a=((a<<5)-a)+b.charCodeAt(0);return a&a},0)
175+
let hash = md5(JSON.stringify(object))
165176
return hash
166177
}
167178

src/singleContinous.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)