-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjusting interface to align with other parts of the .net framework a…
…nd improving readme.
- Loading branch information
1 parent
4a6ec7b
commit e83ff77
Showing
37 changed files
with
153 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 0 additions & 102 deletions
102
SimpleConcepts.Extensions.Caching/Distributed/DistributedCacheGetOrFetchExtensions.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
100 changes: 100 additions & 0 deletions
100
src/SimpleConcepts.Extensions.Caching/Distributed/DistributedCacheGetOrSetExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Caching.Distributed | ||
{ | ||
public static class DistributedCacheGetOrSetExtensions | ||
{ | ||
public static byte[] GetOrSet(this IDistributedCache cache, string key, Func<byte[]> valueFactory) | ||
{ | ||
return cache.GetOrSet(key, valueFactory, new DistributedCacheEntryOptions()); | ||
} | ||
|
||
public static byte[] GetOrSet(this IDistributedCache cache, string key, Func<byte[]> valueFactory, | ||
DistributedCacheEntryOptions options) | ||
{ | ||
var cached = cache.Get(key); | ||
|
||
if (cached != null) | ||
{ | ||
return cached; | ||
} | ||
|
||
var value = valueFactory(); | ||
|
||
cache.Set(key, value, options); | ||
|
||
return value; | ||
} | ||
|
||
public static Task<byte[]> GetOrSetAsync(this IDistributedCache cache, string key, Func<Task<byte[]>> valueFactory, | ||
CancellationToken token = default) | ||
{ | ||
return cache.GetOrSetAsync(key, valueFactory, new DistributedCacheEntryOptions(), token); | ||
} | ||
|
||
public static async Task<byte[]> GetOrSetAsync(this IDistributedCache cache, string key, Func<Task<byte[]>> valueFactory, | ||
DistributedCacheEntryOptions options, CancellationToken token = default) | ||
{ | ||
var cached = await cache.GetAsync(key, token); | ||
|
||
if (cached != null) | ||
{ | ||
return cached; | ||
} | ||
|
||
var value = await valueFactory(); | ||
|
||
await cache.SetAsync(key, value, options, token); | ||
|
||
return value; | ||
} | ||
|
||
public static string GetOrSetString(this IDistributedCache cache, string key, Func<string> valueFactory) | ||
{ | ||
return cache.GetOrSetString(key, valueFactory, new DistributedCacheEntryOptions()); | ||
} | ||
|
||
public static string GetOrSetString(this IDistributedCache cache, string key, Func<string> valueFactory, | ||
DistributedCacheEntryOptions options) | ||
{ | ||
var cached = cache.GetString(key); | ||
|
||
if (cached != null) | ||
{ | ||
return cached; | ||
} | ||
|
||
var value = valueFactory(); | ||
|
||
cache.SetString(key, value, options); | ||
|
||
return value; | ||
} | ||
|
||
public static Task<string> GetOrSetStringAsync(this IDistributedCache cache, string key, | ||
Func<Task<string>> valueFactory, CancellationToken token = default) | ||
{ | ||
return cache.GetOrSetStringAsync(key, valueFactory, new DistributedCacheEntryOptions(), token); | ||
} | ||
|
||
public static async Task<string> GetOrSetStringAsync(this IDistributedCache cache, string key, | ||
Func<Task<string>> valueFactory, DistributedCacheEntryOptions options, CancellationToken token = default) | ||
{ | ||
var cached = await cache.GetStringAsync(key, token); | ||
|
||
if (cached != null) | ||
{ | ||
return cached; | ||
} | ||
|
||
var value = await valueFactory(); | ||
|
||
await cache.SetStringAsync(key, value, options, token); | ||
|
||
return value; | ||
} | ||
} | ||
} |
Oops, something went wrong.