From a5ecc2c1933bf69f89ad76e4e4268c5cb0b3b48d Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Thu, 23 May 2024 19:19:52 +0530 Subject: [PATCH] avoid throwing err --- src/core/lambda/cache.rs | 27 ++++++++++++++------------- src/core/lambda/io.rs | 20 +++++++++++--------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/core/lambda/cache.rs b/src/core/lambda/cache.rs index 218fddd2ae..2f393902c6 100644 --- a/src/core/lambda/cache.rs +++ b/src/core/lambda/cache.rs @@ -40,20 +40,21 @@ impl Eval for Cache { ) -> Pin> + 'a + Send>> { Box::pin(async move { if let Expression::IO(io) = self.expr.deref() { - let key = io - .cache_key(&ctx) - .ok_or(anyhow::anyhow!("Unable to generate Cache Key"))?; - - if let Some(val) = ctx.request_ctx.runtime.cache.get(&key).await? { - Ok(val) + let key = io.cache_key(&ctx); + if let Some(key) = key { + if let Some(val) = ctx.request_ctx.runtime.cache.get(&key).await? { + Ok(val) + } else { + let val = self.expr.eval(ctx.clone()).await?; + ctx.request_ctx + .runtime + .cache + .set(key, val.clone(), self.max_age) + .await?; + Ok(val) + } } else { - let val = self.expr.eval(ctx.clone()).await?; - ctx.request_ctx - .runtime - .cache - .set(key, val.clone(), self.max_age) - .await?; - Ok(val) + self.expr.eval(ctx).await } } else { Ok(self.expr.eval(ctx).await?) diff --git a/src/core/lambda/io.rs b/src/core/lambda/io.rs index 3215943e2e..cc93946755 100644 --- a/src/core/lambda/io.rs +++ b/src/core/lambda/io.rs @@ -51,15 +51,17 @@ impl Eval for IO { ) -> Pin> + 'a + Send>> { if ctx.request_ctx.upstream.dedupe { Box::pin(async move { - let key = self - .cache_key(&ctx) - .ok_or(anyhow::anyhow!("Unable to generate Cache Key"))?; - ctx.request_ctx - .cache - .get_or_eval(key, move || Box::pin(self.eval_inner(ctx))) - .await - .as_ref() - .clone() + let key = self.cache_key(&ctx); + if let Some(key) = key { + ctx.request_ctx + .cache + .get_or_eval(key, move || Box::pin(self.eval_inner(ctx))) + .await + .as_ref() + .clone() + } else { + self.eval_inner(ctx).await + } }) } else { Box::pin(self.eval_inner(ctx))