forked from DevChatter/DevStreams
-
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.
Merge pull request DevChatter#71 from DevChatter/benrick/cache-Channe…
…lSearchService Adding Caching to ChannelSearchService
- Loading branch information
Showing
5 changed files
with
47 additions
and
35 deletions.
There are no files selected for viewing
4 changes: 1 addition & 3 deletions
4
src/DevChatter.DevStreams.Core/Services/IChannelSearchService.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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
using System.Collections.Generic; | ||
using DevChatter.DevStreams.Core.Model; | ||
using System.Threading.Tasks; | ||
using DevChatter.DevStreams.Core.Model; | ||
|
||
namespace DevChatter.DevStreams.Core.Services | ||
{ | ||
public interface IChannelSearchService | ||
{ | ||
List<Channel> Find(); | ||
Task<Channel> GetChannelSoundex(string standardizedChannelName); | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/DevChatter.DevStreams.Web/Caching/CachedChannelSearchService.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,35 @@ | ||
using DevChatter.DevStreams.Core.Model; | ||
using DevChatter.DevStreams.Core.Services; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace DevChatter.DevStreams.Web.Caching | ||
{ | ||
public class CachedChannelSearchService : IChannelSearchService | ||
{ | ||
private readonly IChannelSearchService _searchService; | ||
private readonly IMemoryCache _cacheLayer; | ||
|
||
public CachedChannelSearchService(IChannelSearchService searchService, IMemoryCache cacheLayer) | ||
{ | ||
_searchService = searchService; | ||
_cacheLayer = cacheLayer; | ||
} | ||
|
||
public async Task<Channel> GetChannelSoundex(string standardizedChannelName) | ||
{ | ||
string cacheKey = $"{nameof(IChannelSearchService)}-{nameof(GetChannelSoundex)}-{standardizedChannelName}"; | ||
|
||
var channel = await _cacheLayer.GetOrCreateAsync(cacheKey, CacheFallback); | ||
|
||
return channel; | ||
|
||
Task<Channel> CacheFallback(ICacheEntry entry) | ||
{ | ||
entry.SetAbsoluteExpiration(TimeSpan.FromMinutes(15)); // TODO: Store in Config Setting | ||
return _searchService.GetChannelSoundex(standardizedChannelName); | ||
} | ||
} | ||
} | ||
} |
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