Sidhant Sidhant - 3 months ago 23
Scala Question

Spray Routing Cache dont cache 404

Hey guys im using the spray routing cache directive with routeCache method to cache my api calls.

But the issue is that if you api is returning 500 for some reason then in that case also spray is caching that and keeps returning 500 until the cache expires.

So how can i cache only 200 OK responses in spray cache and not others.

Answer

Following worked

Instead of doing

ctx.complete(ErrorCode, Error)

did

ctx.failWith(Error)

Ref:

(1) https://github.com/spray/spray/blob/master/spray-caching/src/main/scala/spray/caching/LruCache.scala#L79

With failWith the value isFailure is set and so cache being done unset

(2) http://spray.io/documentation/1.2.2/spray-routing/route-directives/failWith/

To maintain proper status code, can use custom exception handlers as explained in above link