Api: How to cache response?

Created on 8 May 2017  路  2Comments  路  Source: dingo/api

I would like to put into cache the whole response and not only DB data.

I can easily cache DB fetched data like this

$products = \Cache::remember($key, 100, $callback);

        return $this
            ->response()
            ->paginator($products, new ProductTransformer());

but that's not enough for me.

How can I cache the whole output? Middleware solution is not good for me because in the same controller I want to have some further actions.

Most helpful comment

Just in case, if somebody will face with similar issue, I've solved it in the following way

// now I can put into cache the whole output content

$this
       ->response()
       ->paginator($products, (new ProductTransformer()))
       ->morph()        // force to generate output applying mentioned transformers
       ->getContent();  // read just generated content

All 2 comments

Just in case, if somebody will face with similar issue, I've solved it in the following way

// now I can put into cache the whole output content

$this
       ->response()
       ->paginator($products, (new ProductTransformer()))
       ->morph()        // force to generate output applying mentioned transformers
       ->getContent();  // read just generated content

Would just like to extend a thank you for this @nnnikolay , I had somehow missed the morph method in my own search for a solution to this issue.

Was this page helpful?
0 / 5 - 0 ratings