Skip to content

Commit

Permalink
Updated MongoFramework from 0.18.0 to 0.21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Turnerj committed Aug 17, 2020
1 parent b669e43 commit 42f72d8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MongoFramework" Version="0.18.0" />
<PackageReference Include="MongoFramework" Version="0.21.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using CacheTower.Providers.Database.MongoDB.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoFramework.Infrastructure.Commands;
using MongoFramework.Infrastructure.Mapping;

namespace CacheTower.Providers.Database.MongoDB.Commands
{
public class CleanupCommand : IWriteCommand<DbCachedEntry>
{
public Type EntityType => typeof(DbCachedEntry);

public IEnumerable<WriteModel<DbCachedEntry>> GetModel()
{
var filter = Builders<DbCachedEntry>.Filter.Lt(e => e.Expiry, DateTime.UtcNow);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using CacheTower.Providers.Database.MongoDB.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoFramework.Infrastructure.Commands;
using MongoFramework.Infrastructure.Mapping;

namespace CacheTower.Providers.Database.MongoDB.Commands
{
public class EvictCommand : IWriteCommand<DbCachedEntry>
{
private string CacheKey { get; }

public Type EntityType => typeof(DbCachedEntry);

public EvictCommand(string cacheKey)
{
CacheKey = cacheKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using CacheTower.Providers.Database.MongoDB.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Driver;
using MongoFramework.Infrastructure.Commands;
using MongoFramework.Infrastructure.Mapping;
using MongoDB.Bson.Serialization;

namespace CacheTower.Providers.Database.MongoDB.Commands
{
public class SetCommand : IWriteCommand<DbCachedEntry>
{
public DbCachedEntry Entry { get; }

public Type EntityType => typeof(DbCachedEntry);

public SetCommand(DbCachedEntry dbCachedEntry)
{
Entry = dbCachedEntry;
Expand Down
33 changes: 14 additions & 19 deletions src/CacheTower.Providers.Database.MongoDB/MongoDbCacheLayer.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using CacheTower.Providers.Database.MongoDB.Commands;
using CacheTower.Providers.Database.MongoDB.Entities;
using MongoFramework;
using MongoFramework.Infrastructure;
using MongoFramework.Infrastructure.Commands;
using MongoFramework.Infrastructure.Indexing;
using MongoFramework.Infrastructure.Mapping;
using MongoFramework.Infrastructure.Linq;

namespace CacheTower.Providers.Database.MongoDB
{
public class MongoDbCacheLayer : IAsyncCacheLayer
{
private bool? IsDatabaseAvailable { get; set; }
private IEntityReader<DbCachedEntry> EntityReader { get; }
private ICommandWriter<DbCachedEntry> CommandWriter { get; }
private IEntityIndexWriter<DbCachedEntry> IndexWriter { get; }

private IMongoDbConnection Connection { get; }

private bool HasSetIndexes = false;

public MongoDbCacheLayer(IMongoDbConnection connection)
{
EntityReader = new EntityReader<DbCachedEntry>(connection);
CommandWriter = new CommandWriter<DbCachedEntry>(connection);
IndexWriter = new EntityIndexWriter<DbCachedEntry>(connection);
Connection = connection;
}

private Task TryConfigureIndexes()
private async Task TryConfigureIndexes()
{
if (!HasSetIndexes)
{
HasSetIndexes = true;
return IndexWriter.ApplyIndexingAsync();
await EntityIndexWriter.ApplyIndexingAsync<DbCachedEntry>(Connection);
}

return Task.CompletedTask;
}

public async Task CleanupAsync()
{
await TryConfigureIndexes();
await CommandWriter.WriteAsync(new[] { new CleanupCommand() });
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new CleanupCommand() }, default);
}

public async Task EvictAsync(string cacheKey)
{
await TryConfigureIndexes();
await CommandWriter.WriteAsync(new[] { new EvictCommand(cacheKey) });
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { new EvictCommand(cacheKey) }, default);
}

public async Task<CacheEntry<T>> GetAsync<T>(string cacheKey)
{
await TryConfigureIndexes();

var dbEntry = EntityReader.AsQueryable().Where(e => e.CacheKey == cacheKey).FirstOrDefault();
var provider = new MongoFrameworkQueryProvider<DbCachedEntry>(Connection);
var queryable = new MongoFrameworkQueryable<DbCachedEntry>(provider);

var dbEntry = queryable.Where(e => e.CacheKey == cacheKey).FirstOrDefault();
var cacheEntry = default(CacheEntry<T>);

if (dbEntry != default)
Expand All @@ -76,7 +71,7 @@ public async Task SetAsync<T>(string cacheKey, CacheEntry<T> cacheEntry)
Value = cacheEntry.Value
});

await CommandWriter.WriteAsync(new[] { command });
await EntityCommandWriter.WriteAsync<DbCachedEntry>(Connection, new[] { command }, default);
}

public async Task<bool> IsAvailableAsync(string cacheKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MongoDB.Driver;
using MongoFramework;
using MongoFramework.Infrastructure.Indexing;
using Moq;

namespace CacheTower.Tests.Providers.Database.MongoDB
Expand Down Expand Up @@ -36,10 +37,13 @@ public async Task GetSetCache()
[TestMethod]
public async Task IsCacheAvailable()
{
EntityIndexWriter.ClearCache();

await AssertCacheAvailabilityAsync(new MongoDbCacheLayer(MongoDbHelper.GetConnection()), true);

var connectionMock = new Mock<IMongoDbConnection>();
connectionMock.Setup(c => c.GetDatabase()).Throws<Exception>();
EntityIndexWriter.ClearCache();

await AssertCacheAvailabilityAsync(new MongoDbCacheLayer(connectionMock.Object), false);
}
Expand Down

0 comments on commit 42f72d8

Please sign in to comment.