Skip to content

Commit dd2ce54

Browse files
committed
Add project files.
1 parent b7a4db6 commit dd2ce54

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

AspNetCoreHero.Extensions.Caching.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30717.126
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreHero.Extensions.Caching", "AspNetCoreHero.Extensions.Caching\AspNetCoreHero.Extensions.Caching.csproj", "{67EDFFC0-A7A7-43C9-94C1-28C09B3F85B4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{67EDFFC0-A7A7-43C9-94C1-28C09B3F85B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{67EDFFC0-A7A7-43C9-94C1-28C09B3F85B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{67EDFFC0-A7A7-43C9-94C1-28C09B3F85B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{67EDFFC0-A7A7-43C9-94C1-28C09B3F85B4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C6376376-2590-48CE-B050-47D163693A53}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
5+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
6+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
7+
<Authors>Mukesh Murugan</Authors>
8+
<Company>aspnetcorehero</Company>
9+
<Product>AspNetCoreHero.Extensions.Caching</Product>
10+
<Description>Essential Caching Extensions for Distribued Caching in ASP.NET Core.</Description>
11+
<Copyright>2020</Copyright>
12+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
13+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
14+
<FileVersion>1.0.0.0</FileVersion>
15+
<Version>1.0.0.0</Version>
16+
<PackageIcon>Logo_600x600.png</PackageIcon>
17+
<PackageProjectUrl>https://github.com/aspnetcorehero/Caching</PackageProjectUrl>
18+
<RepositoryUrl>https://github.com/aspnetcorehero/Caching</RepositoryUrl>
19+
</PropertyGroup>
20+
<ItemGroup>
21+
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="5.0.0" />
22+
<PackageReference Include="AspNetCoreHero.ThrowR" Version="1.0.0" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
26+
<None Include="D:\Photoshop\ANCH\Logo_600x600.png">
27+
<Pack>True</Pack>
28+
<PackagePath></PackagePath>
29+
</None>
30+
</ItemGroup>
31+
32+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using AspNetCoreHero.ThrowR;
2+
using Microsoft.Extensions.Caching.Distributed;
3+
using System;
4+
using System.Text.Json;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
8+
namespace AspNetCoreHero.Extensions.Caching
9+
{
10+
public static class Extensions
11+
{
12+
public static async Task<T> GetAsync<T>(this IDistributedCache distributedCache, string cacheKey, CancellationToken token = default)
13+
{
14+
Throw.Exception.IfNull(distributedCache, nameof(distributedCache));
15+
Throw.Exception.IfNull(cacheKey, nameof(cacheKey));
16+
byte[] utf8Bytes = await distributedCache.GetAsync(cacheKey, token).ConfigureAwait(false);
17+
if (utf8Bytes != null)
18+
{
19+
return JsonSerializer.Deserialize<T>(utf8Bytes);
20+
}
21+
return default;
22+
}
23+
public static async Task RemoveAsync(this IDistributedCache distributedCache, string cacheKey, CancellationToken token = default)
24+
{
25+
Throw.Exception.IfNull(distributedCache, nameof(distributedCache));
26+
Throw.Exception.IfNull(cacheKey, nameof(cacheKey));
27+
await distributedCache.RemoveAsync(cacheKey, token).ConfigureAwait(false);
28+
}
29+
public static async Task SetAsync<T>(this IDistributedCache distributedCache, string cacheKey, T obj, int cacheExpirationInMinutes = 30, CancellationToken token = default)
30+
{
31+
Throw.Exception.IfNull(distributedCache, nameof(distributedCache));
32+
Throw.Exception.IfNull(cacheKey, nameof(cacheKey));
33+
Throw.Exception.IfNull(obj, nameof(obj));
34+
DistributedCacheEntryOptions options = new DistributedCacheEntryOptions()
35+
.SetSlidingExpiration(TimeSpan.FromMinutes(cacheExpirationInMinutes));
36+
byte[] utf8Bytes = JsonSerializer.SerializeToUtf8Bytes<T>(obj);
37+
await distributedCache.SetAsync(cacheKey, utf8Bytes, options, token).ConfigureAwait(false);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)