-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
234 additions
and
229 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace EvenireDB; | ||
|
||
public interface IStreamsCache | ||
{ | ||
void Update(Guid streamId, CachedEvents entry); | ||
ValueTask<CachedEvents> GetEventsAsync(Guid streamId, CancellationToken cancellationToken); | ||
bool ContainsKey(Guid streamId); | ||
} |
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
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,41 @@ | ||
using EvenireDB.Persistence; | ||
using EvenireDB.Utils; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace EvenireDB; | ||
|
||
internal class StreamsCache : IStreamsCache | ||
{ | ||
private readonly ICache<Guid, CachedEvents> _cache; | ||
private readonly ILogger<StreamsCache> _logger; | ||
private readonly IEventsProvider _repo; | ||
|
||
public StreamsCache( | ||
ILogger<StreamsCache> logger, | ||
ICache<Guid, CachedEvents> cache, | ||
IEventsProvider repo) | ||
{ | ||
_logger = logger; | ||
_cache = cache; | ||
_repo = repo; | ||
} | ||
|
||
private async ValueTask<CachedEvents> Factory(Guid streamId, CancellationToken cancellationToken) | ||
{ | ||
_logger.ReadingStreamFromRepository(streamId); | ||
|
||
var persistedEvents = new List<Event>(); | ||
await foreach (var @event in _repo.ReadAsync(streamId, cancellationToken: cancellationToken)) | ||
persistedEvents.Add(@event); | ||
return new CachedEvents(persistedEvents, new SemaphoreSlim(1)); | ||
} | ||
|
||
public ValueTask<CachedEvents> GetEventsAsync(Guid streamId, CancellationToken cancellationToken) | ||
=> _cache.GetOrAddAsync(streamId, this.Factory, cancellationToken); | ||
|
||
public void Update(Guid streamId, CachedEvents entry) | ||
=> _cache.AddOrUpdate(streamId, entry); | ||
|
||
public bool ContainsKey(Guid streamId) | ||
=> _cache.ContainsKey(streamId); | ||
} |
Oops, something went wrong.