Best Practice for storing aggregate data by date or other criteria?

I settled on using wp_cache_set and wp_cache_get which seems to do well. I create a key based on the class, function, and parameters:

$cacheKey = self::createCacheKey( __CLASS__, __FUNCTION__, $referenceYear, $referenceMonth );

createCacheKey() looks like:

protected static function createCacheKey( ...$args ): string {
    return implode('-', $args);
}

And I’m sure to use an appropriate expiration. For dates that are not this year or month I have longer expirations set.

Getting and returning the cache looks like this at the top of a function:

    $cacheKey = self::createCacheKey( __CLASS__,
                                      __FUNCTION__,
                                      $referenceYear, 
                                      $referenceMonth );
    $cache    = wp_cache_get( $cacheKey, self::CACHE_GROUP );
    if ( $cache ) {
        return $cache;
    }

And at the end of those functions I use:

    wp_cache_set( $cacheKey, $output, 
                  self::CACHE_GROUP, $cacheExpiration );

This seems like it’s doing what I want and improves performance though it appears the default place things are stored and how reliable this is depends on server configuration.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)