How to use transient in this code for related post?

If you set a transient it will go in wp_options table… that is probably not the best thing to do because you will be creating a transient for each post that way and that could mean a LOT of transients in your options table.

It makes more sense to store it as post meta and then expire it yourself with a simple check by recording your own data expiry time. eg:

$related = get_post_meta($post->ID, 'related_posts');
if ($related) {
    $expires = get_post_meta($post->ID, 'related_expires');
    if ($expires > time()) {
        $related = false;
        delete_post_meta($post->ID, 'related_posts');
        delete_post_meta($post->ID, 'related_expires');
    }
} 
if (!$related) {
    $related = get_posts(array(
        'author' => $author_id,
        'category__in' => wp_get_post_categories($post->ID),
        'numberposts' => 8,
        'orderby' => 'rand',
        'post__not_in' => array(
            $post->ID
        )
    ));
    if ($related) {
         add_post_meta($post->ID, 'related_posts', $related);
         add_post_meta($post->ID, 'related_expires', time()+(24*60*60));
    }
}
if ($related) {
    foreach ($related as $post) {
        setup_postdata($post);
        contenedor();
    }
}
wp_reset_postdata();