|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
5 | 7 | using System.Security.Cryptography; |
6 | 8 | using System.Text.Json; |
7 | | -using Microsoft.Shared.Diagnostics; |
| 9 | +#if NET |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +#endif |
| 13 | + |
| 14 | +#pragma warning disable S109 // Magic numbers should not be used |
| 15 | +#pragma warning disable SA1202 // Elements should be ordered by access |
| 16 | +#pragma warning disable SA1502 // Element should not be on a single line |
8 | 17 |
|
9 | 18 | namespace Microsoft.Extensions.AI; |
10 | 19 |
|
11 | 20 | /// <summary>Provides internal helpers for implementing caching services.</summary> |
12 | 21 | internal static class CachingHelpers |
13 | 22 | { |
14 | 23 | /// <summary>Computes a default cache key for the specified parameters.</summary> |
15 | | - /// <typeparam name="TValue">Specifies the type of the data being used to compute the key.</typeparam> |
16 | | - /// <param name="value">The data with which to compute the key.</param> |
17 | | - /// <param name="serializerOptions">The <see cref="JsonSerializerOptions"/>.</param> |
18 | | - /// <returns>A string that will be used as a cache key.</returns> |
19 | | - public static string GetCacheKey<TValue>(TValue value, JsonSerializerOptions serializerOptions) |
20 | | - => GetCacheKey(value, false, serializerOptions); |
21 | | - |
22 | | - /// <summary>Computes a default cache key for the specified parameters.</summary> |
23 | | - /// <typeparam name="TValue">Specifies the type of the data being used to compute the key.</typeparam> |
24 | | - /// <param name="value">The data with which to compute the key.</param> |
25 | | - /// <param name="flag">Another data item that causes the key to vary.</param> |
| 24 | + /// <param name="values">The data with which to compute the key.</param> |
26 | 25 | /// <param name="serializerOptions">The <see cref="JsonSerializerOptions"/>.</param> |
27 | 26 | /// <returns>A string that will be used as a cache key.</returns> |
28 | | - public static string GetCacheKey<TValue>(TValue value, bool flag, JsonSerializerOptions serializerOptions) |
| 27 | + public static string GetCacheKey(ReadOnlySpan<object?> values, JsonSerializerOptions serializerOptions) |
29 | 28 | { |
30 | | - _ = Throw.IfNull(value); |
31 | | - _ = Throw.IfNull(serializerOptions); |
32 | | - serializerOptions.MakeReadOnly(); |
33 | | - |
34 | | - var jsonKeyBytes = JsonSerializer.SerializeToUtf8Bytes(value, serializerOptions.GetTypeInfo(typeof(TValue))); |
35 | | - |
36 | | - if (flag && jsonKeyBytes.Length > 0) |
37 | | - { |
38 | | - // Make an arbitrary change to the hash input based on the flag |
39 | | - // The alternative would be including the flag in "value" in the |
40 | | - // first place, but that's likely to require an extra allocation |
41 | | - // or the inclusion of another type in the JsonSerializerContext. |
42 | | - // This is a micro-optimization we can change at any time. |
43 | | - jsonKeyBytes[0] = (byte)(byte.MaxValue - jsonKeyBytes[0]); |
44 | | - } |
| 29 | + Debug.Assert(serializerOptions is not null, "Expected serializer options to be non-null"); |
| 30 | + Debug.Assert(serializerOptions!.IsReadOnly, "Expected serializer options to already be read-only."); |
45 | 31 |
|
46 | 32 | // The complete JSON representation is excessively long for a cache key, duplicating much of the content |
47 | 33 | // from the value. So we use a hash of it as the default key, and we rely on collision resistance for security purposes. |
48 | 34 | // If a collision occurs, we'd serve the cached LLM response for a potentially unrelated prompt, leading to information |
49 | 35 | // disclosure. Use of SHA256 is an implementation detail and can be easily swapped in the future if needed, albeit |
50 | 36 | // invalidating any existing cache entries that may exist in whatever IDistributedCache was in use. |
51 | | -#if NET8_0_OR_GREATER |
| 37 | + |
| 38 | +#if NET |
| 39 | + IncrementalHashStream? stream = IncrementalHashStream.ThreadStaticInstance ?? new(); |
| 40 | + IncrementalHashStream.ThreadStaticInstance = null; |
| 41 | + |
| 42 | + foreach (object? value in values) |
| 43 | + { |
| 44 | + JsonSerializer.Serialize(stream, value, serializerOptions.GetTypeInfo(typeof(object))); |
| 45 | + } |
| 46 | + |
52 | 47 | Span<byte> hashData = stackalloc byte[SHA256.HashSizeInBytes]; |
53 | | - SHA256.HashData(jsonKeyBytes, hashData); |
| 48 | + stream.GetHashAndReset(hashData); |
| 49 | + IncrementalHashStream.ThreadStaticInstance = stream; |
| 50 | + |
54 | 51 | return Convert.ToHexString(hashData); |
55 | 52 | #else |
| 53 | + MemoryStream stream = new(); |
| 54 | + foreach (object? value in values) |
| 55 | + { |
| 56 | + JsonSerializer.Serialize(stream, value, serializerOptions.GetTypeInfo(typeof(object))); |
| 57 | + } |
| 58 | + |
56 | 59 | using var sha256 = SHA256.Create(); |
57 | | - var hashData = sha256.ComputeHash(jsonKeyBytes); |
58 | | - return BitConverter.ToString(hashData).Replace("-", string.Empty); |
| 60 | + stream.Position = 0; |
| 61 | + var hashData = sha256.ComputeHash(stream.GetBuffer(), 0, (int)stream.Length); |
| 62 | + |
| 63 | + var chars = new char[hashData.Length * 2]; |
| 64 | + int destPos = 0; |
| 65 | + foreach (byte b in hashData) |
| 66 | + { |
| 67 | + int div = Math.DivRem(b, 16, out int rem); |
| 68 | + chars[destPos++] = ToHexChar(div); |
| 69 | + chars[destPos++] = ToHexChar(rem); |
| 70 | + |
| 71 | + static char ToHexChar(int i) => (char)(i < 10 ? i + '0' : i - 10 + 'A'); |
| 72 | + } |
| 73 | + |
| 74 | + Debug.Assert(destPos == chars.Length, "Expected to have filled the entire array."); |
| 75 | + |
| 76 | + return new string(chars); |
59 | 77 | #endif |
60 | 78 | } |
| 79 | + |
| 80 | +#if NET |
| 81 | + /// <summary>Provides a stream that writes to an <see cref="IncrementalHash"/>.</summary> |
| 82 | + private sealed class IncrementalHashStream : Stream |
| 83 | + { |
| 84 | + /// <summary>A per-thread instance of <see cref="IncrementalHashStream"/>.</summary> |
| 85 | + /// <remarks>An instance stored must be in a reset state ready to be used by another consumer.</remarks> |
| 86 | + [ThreadStatic] |
| 87 | + public static IncrementalHashStream? ThreadStaticInstance; |
| 88 | + |
| 89 | + /// <summary>Gets the current hash and resets.</summary> |
| 90 | + public void GetHashAndReset(Span<byte> bytes) => _hash.GetHashAndReset(bytes); |
| 91 | + |
| 92 | + /// <summary>The <see cref="IncrementalHash"/> used by this instance.</summary> |
| 93 | + private readonly IncrementalHash _hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); |
| 94 | + |
| 95 | + protected override void Dispose(bool disposing) |
| 96 | + { |
| 97 | + _hash.Dispose(); |
| 98 | + base.Dispose(disposing); |
| 99 | + } |
| 100 | + |
| 101 | + public override void WriteByte(byte value) => Write(new ReadOnlySpan<byte>(in value)); |
| 102 | + public override void Write(byte[] buffer, int offset, int count) => _hash.AppendData(buffer, offset, count); |
| 103 | + public override void Write(ReadOnlySpan<byte> buffer) => _hash.AppendData(buffer); |
| 104 | + |
| 105 | + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) |
| 106 | + { |
| 107 | + Write(buffer, offset, count); |
| 108 | + return Task.CompletedTask; |
| 109 | + } |
| 110 | + |
| 111 | + public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default) |
| 112 | + { |
| 113 | + Write(buffer.Span); |
| 114 | + return ValueTask.CompletedTask; |
| 115 | + } |
| 116 | + |
| 117 | + public override void Flush() { } |
| 118 | + public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| 119 | + |
| 120 | + public override bool CanWrite => true; |
| 121 | + public override bool CanRead => false; |
| 122 | + public override bool CanSeek => false; |
| 123 | + public override long Length => throw new NotSupportedException(); |
| 124 | + public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } |
| 125 | + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| 126 | + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| 127 | + public override void SetLength(long value) => throw new NotSupportedException(); |
| 128 | + } |
| 129 | +#endif |
61 | 130 | } |
0 commit comments