src/includes/classes/Core/Utils/DirCache.php
public function set(string $primary_key, $sub_key, $value, int $expires_after = 0, array $args = [])
    {
        if (!isset($primary_key, $sub_key, $value)) {
            return; // Not possible.
        }
        $file = $this->dir; // App-specific.
        $file .= '/'.$this->subPath($primary_key, $sub_key, $args);
        $tmp_file = $this->tmp_dir.'/'.$this->c::uniqueId();

        $dir             = dirname($file); // May need to create this.
        $tmp_dir         = dirname($tmp_file); // May need to create this.
        $dir_permissions = $this->App->Config->©fs_permissions['©transient_dirs'];

        if ($expires_after > 0) {
            $expires = time() + $expires_after;
        } else {
            $expires = 0; // i.e., no expiration time.
        }
        $cache = serialize([
            'x___value'   => $value,
            'x___expires' => $expires,
        ]);
        if (!is_dir($dir)) {
            mkdir($dir, $dir_permissions, true);
        }
        if (!is_dir($tmp_dir)) {
            mkdir($tmp_dir, $dir_permissions, true);
        }
        if (file_put_contents($tmp_file, $cache) === false) {
            @unlink($tmp_file); // Cleanup on error.
            throw $this->c::issue(vars(), 'Cache `$tmp_file` write failure.');
            //
        } elseif (!rename($tmp_file, $file)) {
            @unlink($tmp_file); // Cleanup on error.
            throw $this->c::issue(vars(), 'Cache `$file` rename failure.');
        }
    }
public function set(string $primary_key, $sub_key, $value, int $expires_after = 0, array $args = [])
    {
        if (!isset($primary_key, $sub_key, $value)) {
            return; // Not possible.
        }
        $file = $this->dir; // App-specific.
        $file .= '/'.$this->subPath($primary_key, $sub_key, $args);
        $tmp_file = $this->tmp_dir.'/'.$this->c::uniqueId();

        $dir             = dirname($file); // May need to create this.
        $tmp_dir         = dirname($tmp_file); // May need to create this.
        $dir_permissions = $this->App->Config->©fs_permissions['©transient_dirs'];

        if ($expires_after > 0) {
            $expires = time() + $expires_after;
        } else {
            $expires = 0; // i.e., no expiration time.
        }
        $cache = serialize([
            'x___value'   => $value,
            'x___expires' => $expires,
        ]);
        if (!is_dir($dir)) {
            mkdir($dir, $dir_permissions, true);
        }
        if (!is_dir($tmp_dir)) {
            mkdir($tmp_dir, $dir_permissions, true);
        }
        if (file_put_contents($tmp_file, $cache) === false) {
            @unlink($tmp_file); // Cleanup on error.
            throw $this->c::issue(vars(), 'Cache `$tmp_file` write failure.');
            //
        } elseif (!rename($tmp_file, $file)) {
            @unlink($tmp_file); // Cleanup on error.
            throw $this->c::issue(vars(), 'Cache `$file` rename failure.');
        }
    }
Cache a value.

@since 170824.30708 Dir cache utils.

@param string     $primary_key   Primary key.
@param string|int $sub_key       Sub-key to set.
@param mixed      $value         The value to cache.
@param int        $expires_after Expires after (in seconds).
@param array      $args          Any behavioral args.

@NOTE Not possible to cache a `null` value because
{@link get()} returns `null` to indicate a cache miss.