Skip to content

Commit

Permalink
fix(Cache): Did not clear cache correct
Browse files Browse the repository at this point in the history
  • Loading branch information
st0ffern committed Apr 3, 2017
1 parent 318f464 commit c9056ad
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const LanguagesSchema = new mongoose.Schema({
},
});

const Languages = mongoose.model('UserModel', UserSchema)
const Languages = mongoose.model('Languages', LanguagesSchema)
const LanguagesTC = composeWithDataLoader(composeWithMongoose(Languages),{cacheExpiration: 700})
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"babel-runtime": "^6.22.0",
"dataloader": "^1.3.0",
"md5": "^2.2.1",
"object-path": "^0.11.3",
"string-hash": "^1.1.1"
},
Expand Down
35 changes: 23 additions & 12 deletions src/composeWithDataLoader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TypeComposer } from 'graphql-compose'
import DataLoader from 'dataloader'
import SingleContinous from './singleContinous'

import md5 from 'md5'
import {
dataloaderOptions
} from './definitions'
Expand Down Expand Up @@ -43,7 +42,9 @@ export function composeWithDataLoader(
typeComposer.setResolver( 'findById',
findByIdResolver.wrapResolve(next => rp => {
if (options.removeProjection) delete rp.projection
SingleContinous.run(findByIdLoader, rp, 'findById', options)
setTimeout(() => {
let res = findByIdLoader.clear(rp)
},options.cacheExpiration)
return findByIdLoader.load(rp)
})
)
Expand All @@ -63,7 +64,9 @@ export function composeWithDataLoader(
typeComposer.setResolver(
'findByIds',
findByIdsResolver.wrapResolve(fn => rp => {
SingleContinous.run(findByIdsLoader, rp, 'findByIds', options)
setTimeout(() => {
let res = findByIdsLoader.clear(rp)
},options.cacheExpiration)
return findByIdsLoader.load(rp)
})
)
Expand All @@ -83,7 +86,9 @@ export function composeWithDataLoader(
typeComposer.setResolver(
'count',
countResolver.wrapResolve(fn => rp => {
SingleContinous.run(countLoader, rp, 'count', options)
setTimeout(() => {
let res = countLoader.clear(rp)
},options.cacheExpiration)
return countLoader.load(rp)
})
)
Expand All @@ -102,8 +107,10 @@ export function composeWithDataLoader(

typeComposer.setResolver(
'findOne',
findByIdsResolver.wrapResolve(fn => rp => {
SingleContinous.run(findOneLoader, rp, 'findOne', options)
findOneResolver.wrapResolve(fn => rp => {
setTimeout(() => {
let res = findOneLoader.clear(rp)
},options.cacheExpiration)
return findOneLoader.load(rp)
})
)
Expand All @@ -117,13 +124,15 @@ export function composeWithDataLoader(
if (options.debug) console.log('New db request (findMany)')
resolve(resolveParamsArray.map(rp => findManyResolver.resolve(rp)))
}),
{ cacheKeyFn: key => getHashKey(key)} )
{ cacheKeyFn: key => getHashKey(key) } )

typeComposer.setResolver(
'findMany',
findManyResolver.wrapResolve(next => rp => {
if (options.removeProjection) delete rp.projection
SingleContinous.run(findManyLoader, rp, 'findMany', options)
setTimeout(() => {
let res = findManyLoader.clear(rp)
},options.cacheExpiration)
return findManyLoader.load(rp)
})
)
Expand All @@ -148,7 +157,9 @@ export function composeWithDataLoader(
connectionFieldNames.map( field => projection.edges.node[field] = true)
rp.projection = projection
}
SingleContinous.run(connectionLoader, rp, 'connection', options)
setTimeout(() => {
let res = connectionLoader.clear(rp)
},options.cacheExpiration)
return connectionLoader.load(rp)
})
)
Expand All @@ -157,11 +168,11 @@ export function composeWithDataLoader(
const getHashKey = key =>{
let object = {}
Object.assign(object,
{ args: key.args },
{ args: key.args || {} },
{ projection: key.projection || {} },
{ rawQuery: JSON.stringify(key.rawQuery || {}) },
{ context: JSON.stringify(key.context || {}) })
let hash = JSON.stringify(object).split("").reduce((a,b)=>{a=((a<<5)-a)+b.charCodeAt(0);return a&a},0)
let hash = md5(JSON.stringify(object))
return hash
}

Expand Down
38 changes: 0 additions & 38 deletions src/singleContinous.js

This file was deleted.

0 comments on commit c9056ad

Please sign in to comment.