Repository: dotnet/aspnetcore
File: src/Components/Server/src/Circuits/HybridCacheCircuitPersistenceProvider.cs (C#)

Code before:
        Log.CircuitPauseStarted(_logger, circuitId);

        try
        {
            await _lock.WaitAsync(cancellation);
            await _hybridCache.SetAsync(circuitId.Secret, persistedCircuitState, _cacheWriteOptions, _tags, cancellation);

        }
        catch (Exception ex)
        {
            Log.ExceptionPersistingCircuit(_logger, circuitId, ex);
        }
        finally
        {
            _lock.Release();
        }
    }

    public async Task<PersistedCircuitState> RestoreCircuitAsync(CircuitId circuitId, CancellationToken cancellation = default)
    {
        Log.CircuitResumeStarted(_logger, circuitId);

        try
        {
            await _lock.WaitAsync(cancellation);
            var state = await _hybridCache.GetOrCreateAsync(
                circuitId.Secret,
                factory: _failOnCreate,
                options: _cacheReadOptions,
                _tags,
                cancellation);

            if (state == null)
            {
                Log.FailedToFindCircuitState(_logger, circuitId);
                return null;
            }

            await _hybridCache.RemoveAsync(circuitId.Secret, cancellation);

            Log.CircuitStateFound(_logger, circuitId);
            return state;
        }
        catch (Exception ex)
        {
            Log.ExceptionRestoringCircuit(_logger, circuitId, ex);
            return null;
        }
        finally
        {
            _lock.Release();

Review comment (security):
I don't know a lot about "circuitid", but: is this unique enough vs other potential parallel HC uses? there may be some benefit in a key prefix, and if you do that, I strongly recommend using interpolated string literals, i.e. `$"circuits/{circuitId.Secret}"`, because that should start benefitting from zero-alloc paths in .NET 10 (i.e. for cache hits, it doesn't actually allocate a `string` for the composed value, via voodoo)

Code after:
        Log.CircuitPauseStarted(_logger, circuitId);

        try
        {
            await _lock.WaitAsync(cancellation);
            await _hybridCache.SetAsync(circuitId.Secret, persistedCircuitState, _cacheWriteOptions, _tags, cancellation);
        }
        catch (Exception ex)
        {
            Log.ExceptionPersistingCircuit(_logger, circuitId, ex);
        }
        finally
        {
            _lock.Release();
        }
    }

    public async Task<PersistedCircuitState> RestoreCircuitAsync(CircuitId circuitId, CancellationToken cancellation = default)
    {
        Log.CircuitResumeStarted(_logger, circuitId);

        try
        {
            await _lock.WaitAsync(cancellation);
            var state = await _hybridCache.GetOrCreateAsync(
                circuitId.Secret,
                factory: _failOnCreate,
                options: _cacheReadOptions,
                _tags,
                cancellation);

            if (state == null)
            {
                Log.FailedToFindCircuitState(_logger, circuitId);
                return null;
            }

            await _hybridCache.RemoveAsync(circuitId.Secret, cancellation);

            Log.CircuitStateFound(_logger, circuitId);
            return state;
        }
        catch (Exception ex)
        {
            Log.ExceptionRestoringCircuit(_logger, circuitId, ex);
            return null;
        }
        finally
        {
            _lock.Release();
        }