Skip to content

Instantly share code, notes, and snippets.

@damiensawyer
Last active August 14, 2020 05:06
Show Gist options
  • Save damiensawyer/171e62878002d80b4791cfaac62c292f to your computer and use it in GitHub Desktop.
Save damiensawyer/171e62878002d80b4791cfaac62c292f to your computer and use it in GitHub Desktop.
EventCacheFunctions
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using DCAP.Common.Classes.Fields;
using DCAP.Common.Classes.Monads.Strict.Option;
using DCAP.Common.Helpers.Cache;
using DCAP.CQRS.Classes.StrongTypes;
using DCAP.CQRS.Classes.StrongTypes.Command;
using DCAP.CQRS.Interfaces;
using DCAP.CQRS.Interfaces.Command;
using DCAP.CQRS.Repositories.Interfaces;
namespace DCAP.CQRS.Services.Caching
{
/// <summary>
/// Functions which generally take a <see cref="CQRSEventCache" /> and <see cref="IEventReadRepository" /> and get events from those.
/// I had this as a class / service, but it seemed to be complicated with registrations etc. This way, the <see cref="CQRSEventCache" /> can be registered as singleton (or instantiated as needed) and the
/// <see cref="IEventReadRepository" /> can be instance per lifetime etc so as to participate in transactions. Yay Functional patterns!
/// </summary>
public static class EventCacheFunctions
{
public static void AddEventToCache(this IEvent<AggregateTypeId> @event, CQRSEventCache cache)
=> cache.AddItemToCache(@event, @event.EventId.Value);
public static async Task<Option<IEvent<AggregateTypeId>>> GetEvent(this CQRSEventCache cache, Option<PostgresSchema> postgresSchema, EventId id, IEventReadRepository readRepository)
{
var value = cache.ItemFromCache<IEvent<AggregateTypeId>>(id);
if (value.HasValue) return value;
var persisted = await readRepository.EventById(postgresSchema, id);
if (!persisted.HasValue) return Option<IEvent<AggregateTypeId>>.Nothing;
cache.AddItemToCache(persisted.Value, persisted.Value.EventId);
return persisted.Value.ToOptionStrict();
}
public static async Task<Collection<IEvent<AggregateTypeId>>> GetEventsAsync(this CQRSEventCache cache, Option<PostgresSchema> postgresSchema, IEnumerable<EventId> ids, IEventReadRepository readRepository)
{
var allIds = ids.ToList();
var missingIds = new HashSet<EventId>();
var result = new Collection<IEvent<AggregateTypeId>>();
foreach (var id in allIds)
{
var item = cache.ItemFromCache<IEvent<AggregateTypeId>>(id);
if (item.HasValue)
result.Add(item.Value);
else
missingIds.Add(id);
}
if (!missingIds.Any())
return result;
var newItems = await readRepository.EventsById(postgresSchema, missingIds);
foreach (var newItem in newItems)
{
cache.AddItemToCache(newItem, newItem.EventId);
result.Add(newItem);
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment