在 Apollo DataSource 中使用 redis 缓存的正确方法是什么 class
What is the correct way to use the redis cache inside an Apollo DataSource class
我有以下class方法:
async order(id,{ttlInSeconds}={}){
const cachedOrder=await this.cache.get(this.cacheKey(id))
if (cachedOrder){
return JSON.parse(cachedOrder)
}
const order=await this.models.Order.findOne({
where:{
id:id
}
})
if(ttlInSeconds){
await this.cache.set(this.cacheKey(id),JSON.stringify(order),{ttl:ttlInSeconds})
}
return order
}
但它一直没有得到解决,我做错了什么?
我在扩展 Apollo 的 DataSource 时也遇到过这个问题 class。我不知道为什么,但你不应该等待缓存设置值,即使在规范中很难 它 等待值(https://github.com/apollographql/apollo-server/blob/0aa0e4b20ef97576ce92733698a7842b61d8280e/packages/apollo-server-caching/src/KeyValueCache.ts#L10-L14)。结果,试试这个:
async order(id,{ttlInSeconds}={}){
const cachedOrder=await this.cache.get(this.cacheKey(id))
if (cachedOrder){
return JSON.parse(cachedOrder)
}
const order=await this.models.Order.findOne({
where:{
id:id
}
})
if(ttlInSeconds){
this.cache.set(this.cacheKey(id),JSON.stringify(order),{ttl:ttlInSeconds})
}
return order
}
我有以下class方法:
async order(id,{ttlInSeconds}={}){
const cachedOrder=await this.cache.get(this.cacheKey(id))
if (cachedOrder){
return JSON.parse(cachedOrder)
}
const order=await this.models.Order.findOne({
where:{
id:id
}
})
if(ttlInSeconds){
await this.cache.set(this.cacheKey(id),JSON.stringify(order),{ttl:ttlInSeconds})
}
return order
}
但它一直没有得到解决,我做错了什么?
我在扩展 Apollo 的 DataSource 时也遇到过这个问题 class。我不知道为什么,但你不应该等待缓存设置值,即使在规范中很难 它 等待值(https://github.com/apollographql/apollo-server/blob/0aa0e4b20ef97576ce92733698a7842b61d8280e/packages/apollo-server-caching/src/KeyValueCache.ts#L10-L14)。结果,试试这个:
async order(id,{ttlInSeconds}={}){
const cachedOrder=await this.cache.get(this.cacheKey(id))
if (cachedOrder){
return JSON.parse(cachedOrder)
}
const order=await this.models.Order.findOne({
where:{
id:id
}
})
if(ttlInSeconds){
this.cache.set(this.cacheKey(id),JSON.stringify(order),{ttl:ttlInSeconds})
}
return order
}