Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions CacheMeIfYouCan.sln
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan", "src\CacheMeIfYouCan\CacheMeIfYouCan.csproj", "{0E5C541F-0B77-4073-AEFE-0BA03EC3CA0A}"
# Visual Studio Version 16
VisualStudioVersion = 16.0.31829.152
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan", "src\CacheMeIfYouCan\CacheMeIfYouCan.csproj", "{0E5C541F-0B77-4073-AEFE-0BA03EC3CA0A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Tests", "tests\CacheMeIfYouCan.Tests\CacheMeIfYouCan.Tests.csproj", "{B4C0DCD9-FFE4-44B9-8457-37262FC5318E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Tests", "tests\CacheMeIfYouCan.Tests\CacheMeIfYouCan.Tests.csproj", "{B4C0DCD9-FFE4-44B9-8457-37262FC5318E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.ILTemplates", "sandbox\CacheMeIfYouCan.ILTemplates\CacheMeIfYouCan.ILTemplates.csproj", "{42055B9A-138C-4114-AD06-6BAA3ABBC659}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.ILTemplates", "sandbox\CacheMeIfYouCan.ILTemplates\CacheMeIfYouCan.ILTemplates.csproj", "{42055B9A-138C-4114-AD06-6BAA3ABBC659}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Redis", "src\CacheMeIfYouCan.Redis\CacheMeIfYouCan.Redis.csproj", "{EB0E7F39-18FD-46F3-800B-EC98C38055A7}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Redis", "src\CacheMeIfYouCan.Redis\CacheMeIfYouCan.Redis.csproj", "{EB0E7F39-18FD-46F3-800B-EC98C38055A7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Redis.Tests", "tests\CacheMeIfYouCan.Redis.Tests\CacheMeIfYouCan.Redis.Tests.csproj", "{09EB8E7F-EE8E-432F-8108-10255FE234EB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Redis.Tests", "tests\CacheMeIfYouCan.Redis.Tests\CacheMeIfYouCan.Redis.Tests.csproj", "{09EB8E7F-EE8E-432F-8108-10255FE234EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Polly", "src\CacheMeIfYouCan.Polly\CacheMeIfYouCan.Polly.csproj", "{7A3AFBE2-4D42-4C20-BB75-C2F01D0C36D1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Polly", "src\CacheMeIfYouCan.Polly\CacheMeIfYouCan.Polly.csproj", "{7A3AFBE2-4D42-4C20-BB75-C2F01D0C36D1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Polly.Tests", "tests\CacheMeIfYouCan.Polly.Tests\CacheMeIfYouCan.Polly.Tests.csproj", "{AF8803FC-0559-40E6-A902-72C0A77DF380}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Polly.Tests", "tests\CacheMeIfYouCan.Polly.Tests\CacheMeIfYouCan.Polly.Tests.csproj", "{AF8803FC-0559-40E6-A902-72C0A77DF380}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Serializers.ProtoBuf", "src\CacheMeIfYouCan.Serializers.ProtoBuf\CacheMeIfYouCan.Serializers.ProtoBuf.csproj", "{4B7C629B-F976-47F9-8721-5DC8DD60B8B4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Serializers.ProtoBuf", "src\CacheMeIfYouCan.Serializers.ProtoBuf\CacheMeIfYouCan.Serializers.ProtoBuf.csproj", "{4B7C629B-F976-47F9-8721-5DC8DD60B8B4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Benchmarks", "benchmarks\CacheMeIfYouCan.Benchmarks\CacheMeIfYouCan.Benchmarks.csproj", "{C133ACD8-ABF2-4DBA-9907-49726AF4AF43}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Benchmarks", "benchmarks\CacheMeIfYouCan.Benchmarks\CacheMeIfYouCan.Benchmarks.csproj", "{C133ACD8-ABF2-4DBA-9907-49726AF4AF43}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Cron", "src\CacheMeIfYouCan.Cron\CacheMeIfYouCan.Cron.csproj", "{3C906ADB-686F-46B4-859C-E425040F1CFE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Cron", "src\CacheMeIfYouCan.Cron\CacheMeIfYouCan.Cron.csproj", "{3C906ADB-686F-46B4-859C-E425040F1CFE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Cron.Tests", "tests\CacheMeIfYouCan.Cron.Tests\CacheMeIfYouCan.Cron.Tests.csproj", "{A0F382D8-3C5A-4955-A9CA-B558EEDBAD72}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Cron.Tests", "tests\CacheMeIfYouCan.Cron.Tests\CacheMeIfYouCan.Cron.Tests.csproj", "{A0F382D8-3C5A-4955-A9CA-B558EEDBAD72}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Serializers.Json", "src\CacheMeIfYouCan.Serializers.Json\CacheMeIfYouCan.Serializers.Json.csproj", "{FC6B01A7-3049-44A4-833B-31B0961D50C1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Serializers.Json", "src\CacheMeIfYouCan.Serializers.Json\CacheMeIfYouCan.Serializers.Json.csproj", "{FC6B01A7-3049-44A4-833B-31B0961D50C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Serializers.Tests", "tests\CacheMeIfYouCan.Serializers.Tests\CacheMeIfYouCan.Serializers.Tests.csproj", "{8A13A189-17C8-42A3-86DD-E1DC6BF23E56}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Serializers.Tests", "tests\CacheMeIfYouCan.Serializers.Tests\CacheMeIfYouCan.Serializers.Tests.csproj", "{8A13A189-17C8-42A3-86DD-E1DC6BF23E56}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheMeIfYouCan.Serializers.MessagePack", "src\CacheMeIfYouCan.Serializers.MessagePack\CacheMeIfYouCan.Serializers.MessagePack.csproj", "{CDA877B2-5E3A-4D48-8650-D1B20AF773D8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CacheMeIfYouCan.Serializers.MessagePack", "src\CacheMeIfYouCan.Serializers.MessagePack\CacheMeIfYouCan.Serializers.MessagePack.csproj", "{CDA877B2-5E3A-4D48-8650-D1B20AF773D8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{200A2BA5-847A-46CD-80AF-5E09BCF5D57E}"
ProjectSection(SolutionItems) = preProject
test.runsettings = test.runsettings
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -91,6 +99,10 @@ Global
{CDA877B2-5E3A-4D48-8650-D1B20AF773D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDA877B2-5E3A-4D48-8650-D1B20AF773D8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {930E63DC-AC1E-4694-BE8E-3F268FBE9630}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ var cachedFunction = CachedFunctionFactory
.Build();
```

You can either use an Application Insights telemtry processor or your own to gather performance metrics on the peformance of
distributed cache commands, just implement the ITelemetryProcessor provided by Application Insights.
If you implement an ITelemetryConfig you can provide a threshold so only slower cache calls get logged - useful to help when
diagnosing performance issues with your implementation.
To use, extend the .ConfigureFor call with the .WithApplicationInsightsTelemetry

```csharp
Func<int, Task<int>> originalFunction = ...

var cachedFunction = CachedFunctionFactory
.ConfigureFor(originalFunction)
.WithLocalCache(new DictionaryCache<int, int>())
.WithDistributedCache(new RedisCache<int, int>(...))
.WithTimeToLive(TimeSpan.FromHours(1))
.WithApplicationInsightsTelemetry(new MyTelemetryProcessor(), new MyTelemetryConfig(), "myRedisHost", "myCache");
.OnResult(r => logSuccess(r), ex => logException(ex))
.Build();
```

It is possible to create cached functions where the original function has up to 8 parameters (+ an optional
cancellation token) and the cache key can be any function of the input parameters. The original function can be
synchronous, asynchronous, or return a `ValueTask` (the underlying implementation uses ValueTasks). The resulting
Expand Down
1 change: 1 addition & 0 deletions src/CacheMeIfYouCan.Redis/CacheMeIfYouCan.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Description>Extends CacheMeIfYouCan by providing Redis cache integration</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.19.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="1.3.2" />
<PackageReference Include="StackExchange.Redis" Version="2.0.601" />
<PackageReference Include="System.Reactive" Version="4.4.1" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.ApplicationInsights.Extensibility;

namespace CacheMeIfYouCan.Redis
{
public static class CachedObjectConfigurationManagerExtensions
{
public static IDistributedCache<TKey, TValue> WithApplicationInsightsTelemetry<TKey, TValue>(
this IDistributedCache<TKey, TValue> distributedCache,
IDistributedCacheConfig distributedCacheConfig,
ITelemetryProcessor telemetryProcessor,
ITelemetryConfig telemetryConfig)
{
return new DistributedCacheApplicationInsightsWrapper<TKey, TValue>(distributedCache,
distributedCacheConfig, telemetryProcessor, telemetryConfig);
}

public static IDistributedCache<TOuterKey, TInnerKey, TValue> WithApplicationInsightsTelemetry<TOuterKey, TInnerKey, TValue>(
this IDistributedCache<TOuterKey, TInnerKey, TValue> distributedCache,
IDistributedCacheConfig distributedCacheConfig,
ITelemetryProcessor telemetryProcessor,
ITelemetryConfig telemetryConfig)
{
return new DistributedCacheApplicationInsightsWrapper<TOuterKey, TInnerKey, TValue>(distributedCache,
distributedCacheConfig, telemetryProcessor, telemetryConfig);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ApplicationInsights.Extensibility;

namespace CacheMeIfYouCan.Redis
{
public sealed class DistributedCacheApplicationInsightsWrapper<TKey, TValue>
: DistributedCacheEventsWrapperBase<TKey, TValue>
{
private readonly TelemetryProcessor _telemetryProcessor;

public DistributedCacheApplicationInsightsWrapper(IDistributedCache<TKey, TValue> innerCache,
IDistributedCacheConfig cacheConfig,
ITelemetryProcessor telemetryProcessor,
ITelemetryConfig telemetryConfig)
: base(innerCache)
{
_telemetryProcessor = new TelemetryProcessor(cacheConfig, telemetryProcessor, telemetryConfig);
}

protected override void OnTryGetCompletedSuccessfully(TKey key, bool resultSuccess,
ValueAndTimeToLive<TValue> resultValue, TimeSpan duration)
{
_telemetryProcessor.Add(duration, "StringGetWithExpiryAsync", $"Key '{key}'", true);

base.OnTryGetCompletedSuccessfully(key, resultSuccess, resultValue, duration);
}

protected override void OnSetManyCompletedSuccessfully(ReadOnlySpan<KeyValuePair<TKey, TValue>> values,
TimeSpan timeToLive, TimeSpan duration)
{
var keys = $"Keys '{string.Join(",", values.ToArray().Select(d => d.Key))}'";

_telemetryProcessor.Add(duration, "StringSetAsync", keys, true);

base.OnSetManyCompletedSuccessfully(values, timeToLive, duration);
}

protected override void OnGetManyCompletedSuccessfully(ReadOnlySpan<TKey> keys,
ReadOnlySpan<KeyValuePair<TKey, ValueAndTimeToLive<TValue>>> values, TimeSpan duration)
{
var keysText = $"Keys '{string.Join(",", keys.ToArray())}'";

_telemetryProcessor.Add(duration, "StringGetWithExpiryAsync", keysText, true);

base.OnGetManyCompletedSuccessfully(keys, values, duration);
}

protected override void OnGetManyException(ReadOnlySpan<TKey> keys, TimeSpan duration, Exception exception,
out bool exceptionHandled)
{
var keysText = $"Keys '{string.Join(",", keys.ToArray())}'";

_telemetryProcessor.Add(duration, "StringGetWithExpiryAsync", keysText, false);

base.OnGetManyException(keys, duration, exception, out exceptionHandled);
}

protected override void OnSetCompletedSuccessfully(TKey key, TValue value, TimeSpan timeToLive,
TimeSpan duration)
{
_telemetryProcessor.Add(duration, "StringSetAsync", $"Key '{key}'", true);

base.OnSetCompletedSuccessfully(key, value, timeToLive, duration);
}

protected override void OnSetException(TKey key, TValue value, TimeSpan timeToLive, TimeSpan duration,
Exception exception,
out bool exceptionHandled)
{
_telemetryProcessor.Add(duration, "StringSetAsync", $"Key '{key}'", false);

base.OnSetException(key, value, timeToLive, duration, exception, out exceptionHandled);
}

protected override void OnSetManyException(ReadOnlySpan<KeyValuePair<TKey, TValue>> values, TimeSpan timeToLive,
TimeSpan duration, Exception exception,
out bool exceptionHandled)
{
var keys = $"Keys '{string.Join(",", values.ToArray().Select(d => d.Key))}'";

_telemetryProcessor.Add(duration, "StringSetAsync", keys, false);

base.OnSetManyException(values, timeToLive, duration, exception, out exceptionHandled);
}

protected override void OnTryGetException(TKey key, TimeSpan duration, Exception exception,
out bool exceptionHandled)
{
_telemetryProcessor.Add(duration, "StringGetWithExpiryAsync", $"Key '{key}'", false);

base.OnTryGetException(key, duration, exception, out exceptionHandled);
}

protected override void OnTryRemoveCompletedSuccessfully(TKey key, bool wasRemoved, TimeSpan duration)
{
_telemetryProcessor.Add(duration, "KeyDeleteAsync", $"Key '{key}'", true);

base.OnTryRemoveCompletedSuccessfully(key, wasRemoved, duration);
}

protected override void OnTryRemoveException(TKey key, TimeSpan duration, Exception exception,
out bool exceptionHandled)
{
_telemetryProcessor.Add(duration, "KeyDeleteAsync", $"Key '{key}'", false);

base.OnTryRemoveException(key, duration, exception, out exceptionHandled);
}
}

public sealed class DistributedCacheApplicationInsightsWrapper<TOuterKey, TInnerKey, TValue>
: DistributedCacheEventsWrapperBase<TOuterKey, TInnerKey, TValue>
{
private readonly TelemetryProcessor _telemetryProcessor;

public DistributedCacheApplicationInsightsWrapper(IDistributedCache<TOuterKey, TInnerKey, TValue> innerCache,
IDistributedCacheConfig cacheConfig,
ITelemetryProcessor telemetryProcessor,
ITelemetryConfig telemetryConfig) : base(innerCache)
{
_telemetryProcessor = new TelemetryProcessor(cacheConfig, telemetryProcessor, telemetryConfig);
}

protected override void OnSetManyException(TOuterKey outerKey,
ReadOnlySpan<KeyValuePair<TInnerKey, TValue>> values, TimeSpan timeToLive, TimeSpan duration,
Exception exception, out bool exceptionHandled)
{
var keys =
$"Keys {string.Join(",", values.ToArray().Select(innerKeyValue => $"'{outerKey}.{innerKeyValue.Key}'"))}";

_telemetryProcessor.Add(duration, "StringSetAsync", keys, false);

base.OnSetManyException(outerKey, values, timeToLive, duration, exception, out exceptionHandled);
}

protected override void OnTryRemoveCompletedSuccessfully(TOuterKey outerKey, TInnerKey innerKey,
bool wasRemoved, TimeSpan duration)
{
_telemetryProcessor.Add(duration, "KeyDeleteAsync", $"Key '{outerKey}.{innerKey}'", true);

base.OnTryRemoveCompletedSuccessfully(outerKey, innerKey, wasRemoved, duration);
}

protected override void OnTryRemoveException(TOuterKey outerKey, TInnerKey innerKey, TimeSpan duration,
Exception exception,
out bool exceptionHandled)
{
_telemetryProcessor.Add(duration, "KeyDeleteAsync", $"Key '{outerKey}.{innerKey}'", false);

base.OnTryRemoveException(outerKey, innerKey, duration, exception, out exceptionHandled);
}

protected override void OnGetManyException(TOuterKey outerKey, ReadOnlySpan<TInnerKey> innerKeys,
TimeSpan duration, Exception exception,
out bool exceptionHandled)
{
var keys = $"Keys {string.Join(",", innerKeys.ToArray().Select(innerKey => $"'{outerKey}.{innerKey}'"))}";

_telemetryProcessor.Add(duration, "StringGetWithExpiryAsync", keys, false);

base.OnGetManyException(outerKey, innerKeys, duration, exception, out exceptionHandled);
}

protected override void OnSetManyCompletedSuccessfully(TOuterKey outerKey,
ReadOnlySpan<KeyValuePair<TInnerKey, TValue>> values, TimeSpan timeToLive, TimeSpan duration)
{
var keys =
$"Keys {string.Join(",", values.ToArray().Select(innerKeyValue => $"'{outerKey}.{innerKeyValue.Key}'"))}";

_telemetryProcessor.Add(duration, "StringSetAsync", keys, true);

base.OnSetManyCompletedSuccessfully(outerKey, values, timeToLive, duration);
}

protected override void OnGetManyCompletedSuccessfully(TOuterKey outerKey, ReadOnlySpan<TInnerKey> innerKeys,
ReadOnlySpan<KeyValuePair<TInnerKey, ValueAndTimeToLive<TValue>>> values, TimeSpan duration)
{
var keys = $"Keys {string.Join(",", innerKeys.ToArray().Select(innerKey => $"'{outerKey}.{innerKey}'"))}";

_telemetryProcessor.Add(duration, "StringGetWithExpiryAsync", keys, true);

base.OnGetManyCompletedSuccessfully(outerKey, innerKeys, values, duration);
}
}
}
Loading