Caching in Snippet

I am trying to cache an api request within a widget but it seems to save no content into the cache file…
the content from the remote request is accesable but the cache file stays empty at all times. It’s always trying to do another remote request.

// config
        'cache' => [
            'pages' => [
                'active' => true
            ]
        ],
        'cache.reviews' => true,

// snippet
        $kirby = kirby();
        $apiCache = $kirby->cache('reviews');
        $apiData  = $apiCache->get('review');
        if ($apiData === null) {
           $apiData = remote::request('url');
           $apiCache->set('review', $apiData, 15);
        }
    // i can output the content just fine e.g.
        $result = json_decode($apiData->content);
       dump($result); 
   // the cache file - in this case storage/cache/reviews/review.cache 
   // however stays empty (0 bytes)

you forgot to set the data to cache after getting them from remote.

(forgot to paste that line)

          $apiCache->set('review', $apiData, 15);

but was in my code all along

then serializing the object fails. try storing an json string or array (instead of the object).
i assume it is an object since you do $apiData->content.

that pointed me in the correct direction, thanks.

        $kirby = kirby();
        $apiCache = $kirby->cache('reviews');
        $apiData  = $apiCache->get('review');
        
        if ($apiData === null) {
          $apiData = remote::request('url');
          $apiData = json_decode($apiData->content, true); // true turns it into an array
          $apiCache->set('review', $apiData, 15);
        }
       dump($apiData);

Cache File has contents now.