Skip to content

Commit 896e198

Browse files
committed
Add functions: Get, TryGet, Set
1 parent 23c569b commit 896e198

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

changes.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## :poop: Change Log
44

5+
---
6+
## Version 1.0.2 - 4 March 2018
7+
8+
* Added `TItem Get<TItem>(object key)`
9+
* Added `bool TryGetValue<TItem>(object key, out TItem value)`
10+
* Added `TItem Set<TItem>(string cacheGroup, object key, TItem value, double seconds)`
511

612
---
713
## Version 1.0.1 - 3 March 2018

source/DotNetCoreMemoryCacheService.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
<PackageReleaseNotes>Initial release is not at all "complete". But it does everything that I need it to do for my project. Feel free to make a pull request if you need additional functionality. Should be easy to add to.</PackageReleaseNotes>
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1919
<PackageIconUrl>http://geekymonkey.com/favicon.ico</PackageIconUrl>
20-
<Version>1.0.1</Version>
20+
<Version>1.0.2</Version>
2121
</PropertyGroup>
2222

2323
<ItemGroup>
2424
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
2525
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
2626
</ItemGroup>
2727

28+
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
29+
<Exec Command="del $(ProjectDir)\bin\Release\*.nupkg&#xD;&#xA;" />
30+
</Target>
31+
2832
</Project>

source/MemoryCacheService.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,41 @@ public Task<TItem> GetOrCreateAsync<TItem>(string cacheGroup, object key, double
136136
}); ;
137137
}
138138

139-
/* Future: Additional MemoryCache functions that could be added
140-
*
141-
public static object Get(this IMemoryCache cache, object key);
142-
public static TItem Get<TItem>(this IMemoryCache cache, object key);
143-
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value);
144-
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, DateTimeOffset absoluteExpiration);
145-
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, TimeSpan absoluteExpirationRelativeToNow);
146-
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, IChangeToken expirationToken);
147-
public static TItem Set<TItem>(this IMemoryCache cache, object key, TItem value, MemoryCacheEntryOptions options);
148-
public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out TItem value);
149-
*/
139+
/// <summary>
140+
/// Get an item from the cache
141+
/// </summary>
142+
/// <typeparam name="TItem">Type of item</typeparam>
143+
/// <param name="key">Cache key</param>
144+
/// <returns>Item from the cache</returns>
145+
public TItem Get<TItem>(object key)
146+
{
147+
return MemoryCache.Get<TItem>(key);
148+
}
149+
150+
/// <summary>
151+
/// Try to get a value from the cache
152+
/// </summary>
153+
/// <typeparam name="TItem">Type of item</typeparam>
154+
/// <param name="key">Unique item key</param>
155+
/// <param name="value">Output cache item</param>
156+
/// <returns>True if found</returns>
157+
public bool TryGetValue<TItem>(object key, out TItem value)
158+
{
159+
return this.MemoryCache.TryGetValue(key, out value);
160+
}
161+
162+
/// <typeparam name="TItem">Type of item</typeparam>
163+
/// <param name="cacheGroup">Cache Group name that can be used to remove items from the cache</param>
164+
/// <param name="key">Unique item key</param>
165+
/// <param name="seconds">Seconds to hold the item in the cache</param>
166+
public TItem Set<TItem>(string cacheGroup, object key, TItem value, double seconds)
167+
{
168+
var options = new MemoryCacheEntryOptions
169+
{
170+
AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(seconds)
171+
};
172+
options.ExpirationTokens.Add(new CancellationChangeToken(this.GetOrCreateGroupCancellationToken(cacheGroup).Token));
173+
return this.MemoryCache.Set(key, value, options);
174+
}
150175
}
151176
}

0 commit comments

Comments
 (0)