Skip to content

[FEATURE] Allow factory execution to prefer fail-safe, otherwise report error #567

@Tvde1

Description

@Tvde1

Problem

FusionCache only activates failsafe when the factory throws or times out. If the factory returns any value, even an error-type value, FusionCache treats the operation as successful. This prevents using fail-safe with structured error-return types like Result<T>.

I wish that the factory can say "this operation failed, so I wish to trigger the fail-safe. But if there is no fail-safe, then return this value"

Solution

Provide an API that lets the factory explicitly mark the execution as failed after it has completed, while still returning a value. This should activate failsafe if a cached value exists, but still return the factory output if no failsafe value is available.

Alternatives

The alternative is to turn on fail-safe, and provide a generic "The factory has failed" error code when GetOrSetAsync has returned null.

Additional context

Add any other context or screenshots about the feature request here.

My scenario is as follows:

var result = await _cache.GetOrSetAsync<Result<List<SomeContent>>>(
    cacheKey,
    async (context, ct) =>
    {
        var factoryResult = await DoExpensiveCall(query, ct);

        if (factoryResult.ErrorCode != null)
        {
            // Here we want to fall back to failsafe if available
            context.PreferFailsafe();

            // We return the error so the user still knows what exactly went wrong (described in the Result<>.
            // This will also be the new fail-safe value (also for throttling)
            return factoryResult;
        }

        return factoryResult;
    },
    options =>
    {
        options.Duration = TimeSpan.FromMinutes(10);
        options.SetFailSafe(true, TimeSpan.FromHours(1));
    },
    token: cancellationToken);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions