mercurius-cache
Advanced tools
Comparing version 0.5.0 to 0.6.0
20
index.js
@@ -6,3 +6,3 @@ 'use strict' | ||
module.exports = fp(async function (app, { all, policy, ttl, cacheSize, skip, remoteCache, onHit, onMiss }) { | ||
module.exports = fp(async function (app, { all, policy, ttl, cacheSize, skip, storage, onHit, onMiss }) { | ||
if (typeof policy !== 'object' && !all) { | ||
@@ -25,3 +25,3 @@ throw new Error('policy must be an object') | ||
buildCache() | ||
setupSchema(app.graphql.schema, policy, all, cache, skip, remoteCache, onHit, onMiss) | ||
setupSchema(app.graphql.schema, policy, all, cache, skip, storage, onHit, onMiss) | ||
}, | ||
@@ -41,3 +41,3 @@ | ||
buildCache() | ||
setupSchema(schema, policy, all, cache, skip, remoteCache, onHit, onMiss) | ||
setupSchema(schema, policy, all, cache, skip, storage, onHit, onMiss) | ||
}) | ||
@@ -53,3 +53,3 @@ | ||
function setupSchema (schema, policy, all, cache, skip, remoteCache, onHit, onMiss) { | ||
function setupSchema (schema, policy, all, cache, skip, storage, onHit, onMiss) { | ||
const schemaTypeMap = schema.getTypeMap() | ||
@@ -70,3 +70,3 @@ for (const schemaType of Object.values(schemaTypeMap)) { | ||
const originalFieldResolver = field.resolve | ||
field.resolve = makeCachedResolver(schemaType.toString(), fieldName, cache, originalFieldResolver, policy, skip, remoteCache, onHit, onMiss) | ||
field.resolve = makeCachedResolver(schemaType.toString(), fieldName, cache, originalFieldResolver, policy, skip, storage, onHit, onMiss) | ||
} | ||
@@ -79,3 +79,3 @@ } | ||
function makeCachedResolver (prefix, fieldName, cache, originalFieldResolver, policy, skip, remoteCache, onHit, onMiss) { | ||
function makeCachedResolver (prefix, fieldName, cache, originalFieldResolver, policy, skip, storage, onHit, onMiss) { | ||
const name = prefix + '.' + fieldName | ||
@@ -113,4 +113,4 @@ onHit = onHit.bind(null, prefix, fieldName) | ||
}, async function ({ self, arg, ctx, info, extendKey }, key) { | ||
if (remoteCache) { | ||
const val = await remoteCache.get(name + '~' + key) | ||
if (storage) { | ||
const val = await storage.get(name + '~' + key) | ||
if (val) { | ||
@@ -123,4 +123,4 @@ onHit() | ||
const res = await originalFieldResolver(self, arg, ctx, info) | ||
if (remoteCache) { | ||
await remoteCache.set(name + '~' + key, res) | ||
if (storage) { | ||
await storage.set(name + '~' + key, res) | ||
} | ||
@@ -127,0 +127,0 @@ return res |
{ | ||
"name": "mercurius-cache", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Cache the results of your GraphQL resolvers, for Mercurius", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -96,3 +96,3 @@ # mercurius-cache | ||
extend the key to cache responses by different request, for example to enable custom cache per user; see [example/cache-per-user.js](example/cache-per-user.js) for a complete use case. | ||
extend the key to cache responses by different request, for example to enable custom cache per user; see [examples/cache-per-user.js](examples/cache-per-user.js) for a complete use case. | ||
@@ -120,9 +120,9 @@ ```js | ||
- **remoteCache** | ||
- **storage** | ||
default cache is in memory, the remote cache is useful for a larger cache. See [example/redis.js](example/redis.js) for a complete use case. | ||
default cache is in memory, but a different storage can be used for a larger cache. See [examples/redis.js](examples/redis.js) for a complete use case. | ||
Example | ||
```js | ||
remoteCache: { | ||
storage: { | ||
async get (key) { | ||
@@ -129,0 +129,0 @@ // fetch by key from storage |
@@ -522,3 +522,3 @@ 'use strict' | ||
ttl: 1, | ||
remoteCache: { | ||
storage: { | ||
async get (key) { | ||
@@ -525,0 +525,0 @@ pass('get called with ' + key) |
@@ -148,3 +148,3 @@ 'use strict' | ||
}, | ||
remoteCache: { | ||
storage: { | ||
async get (key) { | ||
@@ -151,0 +151,0 @@ return map.get(key) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40636