Skip to content

Commit

Permalink
feature: Update to .net 5 (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson authored Jan 22, 2021
1 parent 4334d91 commit 445b500
Show file tree
Hide file tree
Showing 42 changed files with 540 additions and 157 deletions.
4 changes: 2 additions & 2 deletions src/Akavache.Core/Akavache.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="MSBuild.Sdk.Extras">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid90;tizen40</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net461;uap10.0.16299</TargetFrameworks>
<TargetFrameworks>netstandard2.0;Xamarin.iOS10;Xamarin.Mac20;MonoAndroid10.0;tizen40;net5.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net461;uap10.0.16299;net5.0-windows</TargetFrameworks>
<AssemblyName>Akavache.Core</AssemblyName>
<RootNamespace>Akavache</RootNamespace>
<Description>An asynchronous, persistent key-value store for desktop and mobile applications on .NET</Description>
Expand Down
36 changes: 26 additions & 10 deletions src/Akavache.Core/BlobCache/InMemoryBlobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public DateTimeKind? ForcedDateTimeKind
{
_dateTimeKind = value;

if (_jsonDateTimeContractResolver != null)
if (_jsonDateTimeContractResolver is not null)
{
_jsonDateTimeContractResolver.ForceDateTimeKindOverride = value;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public IObservable<byte[]> Get(string key)
return ExceptionHelper.ObservableThrowObjectDisposedException<byte[]>("InMemoryBlobCache");
}

CacheEntry entry;
CacheEntry? entry;
lock (_cache)
{
if (!_cache.TryGetValue(key, out entry))
Expand All @@ -207,7 +207,12 @@ public IObservable<byte[]> Get(string key)
}
}

if (entry.ExpiresAt != null && Scheduler.Now > entry.ExpiresAt.Value)
if (entry is null)
{
return ExceptionHelper.ObservableThrowKeyNotFoundException<byte[]>(key);
}

if (entry.ExpiresAt is not null && Scheduler.Now > entry.ExpiresAt.Value)
{
lock (_cache)
{
Expand All @@ -228,7 +233,7 @@ public IObservable<byte[]> Get(string key)
return ExceptionHelper.ObservableThrowObjectDisposedException<DateTimeOffset?>("InMemoryBlobCache");
}

CacheEntry entry;
CacheEntry? entry;
lock (_cache)
{
if (!_cache.TryGetValue(key, out entry))
Expand All @@ -237,6 +242,11 @@ public IObservable<byte[]> Get(string key)
}
}

if (entry is null)
{
return ExceptionHelper.ObservableThrowKeyNotFoundException<DateTimeOffset?>(key);
}

return Observable.Return<DateTimeOffset?>(entry.CreatedAt, Scheduler);
}

Expand All @@ -251,7 +261,7 @@ public IObservable<IEnumerable<string>> GetAllKeys()
lock (_cache)
{
return Observable.Return(_cache
.Where(x => x.Value.ExpiresAt == null || x.Value.ExpiresAt >= Scheduler.Now)
.Where(x => x.Value.ExpiresAt is null || x.Value.ExpiresAt >= Scheduler.Now)
.Select(x => x.Key)
.ToList());
}
Expand Down Expand Up @@ -315,7 +325,7 @@ public IObservable<T> GetObject<T>(string key)
return ExceptionHelper.ObservableThrowObjectDisposedException<T>("InMemoryBlobCache");
}

CacheEntry entry;
CacheEntry? entry;
lock (_cache)
{
if (!_cache.TryGetValue(key, out entry))
Expand All @@ -324,7 +334,12 @@ public IObservable<T> GetObject<T>(string key)
}
}

if (entry.ExpiresAt != null && Scheduler.Now > entry.ExpiresAt.Value)
if (entry is null)
{
return ExceptionHelper.ObservableThrowKeyNotFoundException<T>(key);
}

if (entry.ExpiresAt is not null && Scheduler.Now > entry.ExpiresAt.Value)
{
lock (_cache)
{
Expand Down Expand Up @@ -357,9 +372,10 @@ public IObservable<IEnumerable<T>> GetAllObjects<T>()
{
return Observable.Return(
_cache
.Where(x => x.Value.TypeName == typeof(T).FullName && (x.Value.ExpiresAt == null || x.Value.ExpiresAt >= Scheduler.Now))
.Where(x => x.Value.TypeName == typeof(T).FullName && (x.Value.ExpiresAt is null || x.Value.ExpiresAt >= Scheduler.Now))
.Select(x => DeserializeObject<T>(x.Value.Value))
.ToList(), Scheduler);
.ToList(),
Scheduler);
}
}

Expand Down Expand Up @@ -404,7 +420,7 @@ public IObservable<Unit> Vacuum()

lock (_cache)
{
var toDelete = _cache.Where(x => x.Value.ExpiresAt != null && Scheduler.Now > x.Value.ExpiresAt).ToArray();
var toDelete = _cache.Where(x => x.Value.ExpiresAt is not null && Scheduler.Now > x.Value.ExpiresAt).ToArray();
foreach (var kvp in toDelete)
{
_cache.Remove(kvp.Key);
Expand Down
10 changes: 4 additions & 6 deletions src/Akavache.Core/BulkOperationsMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,10 @@ public static IObservable<IDictionary<string, T>> GetObjects<T>(this IBlobCache
}

return keys.ToObservable()
.SelectMany(x =>
{
return blobCache.GetObject<T>(x)
.Select(y => new KeyValuePair<string, T>(x, y))
.Catch<KeyValuePair<string, T>, KeyNotFoundException>(_ => Observable.Empty<KeyValuePair<string, T>>());
})
.SelectMany(x => blobCache.GetObject<T>(x)
.Where(y => y is not null)
.Select(y => new KeyValuePair<string, T>(x, y!))
.Catch<KeyValuePair<string, T>, KeyNotFoundException>(_ => Observable.Empty<KeyValuePair<string, T>>()))
.ToDictionary(k => k.Key, v => v.Value);
}

Expand Down
15 changes: 13 additions & 2 deletions src/Akavache.Core/DependencyResolverMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,19 @@ public static void InitializeAkavache(this IMutableDependencyResolver resolver,

var fdr = typeof(DependencyResolverMixin);

if (fdr == null || fdr.AssemblyQualifiedName is null)
if (fdr is null || fdr.AssemblyQualifiedName is null)
{
throw new Exception($"Cannot find valid assembly name for the {nameof(DependencyResolverMixin)} class.");
}

var assemblyName = new AssemblyName(
fdr.AssemblyQualifiedName.Replace(fdr.FullName + ", ", string.Empty));

if (assemblyName.Name is null)
{
throw new InvalidOperationException("Could not find a valid name for assembly");
}

foreach (var ns in namespaces)
{
var targetType = ns + ".Registrations";
Expand All @@ -56,7 +61,13 @@ public static void InitializeAkavache(this IMutableDependencyResolver resolver,
continue;
}

var registerer = (IWantsToRegisterStuff)Activator.CreateInstance(registerTypeClass);
var registerer = (IWantsToRegisterStuff?)Activator.CreateInstance(registerTypeClass);

if (registerer is null)
{
continue;
}

registerer.Register(resolver, readonlyDependencyResolver);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Akavache.Core/Json/JsonDateTimeContractResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public JsonDateTimeContractResolver(IContractResolver? contractResolver, DateTim
public override JsonContract ResolveContract(Type type)
{
var contract = ExistingContractResolver?.ResolveContract(type);
if (contract?.Converter != null)
if (contract?.Converter is not null)
{
return contract;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Akavache.Core/Json/JsonDateTimeOffsetTickConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer

public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Date && reader.Value != null)
if (reader.TokenType == JsonToken.Date && reader.Value is not null)
{
return (DateTimeOffset)reader.Value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Akavache.Core/Json/JsonDateTimeTickConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ public override bool CanConvert(Type objectType)
return null;
}

if (reader.TokenType == JsonToken.Date && reader.Value != null)
if (reader.TokenType == JsonToken.Date && reader.Value is not null)
{
return (DateTime)reader.Value;
}

if ((objectType == typeof(DateTime) || objectType == typeof(DateTime?)) && reader.Value != null)
if ((objectType == typeof(DateTime) || objectType == typeof(DateTime?)) && reader.Value is not null)
{
var ticks = (long)reader.Value;

Expand Down
35 changes: 20 additions & 15 deletions src/Akavache.Core/Json/JsonSerializationMixin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static IObservable<Unit> InsertAllObjects<T>(this IBlobCache blobCache, I
/// <param name="key">The key to look up in the cache
/// modified key name. If this is true, GetAllObjects will not find this object.</param>
/// <returns>A Future result representing the object in the cache.</returns>
public static IObservable<T> GetObject<T>(this IBlobCache blobCache, string key)
public static IObservable<T?> GetObject<T>(this IBlobCache blobCache, string key)
{
if (blobCache is null)
{
Expand Down Expand Up @@ -122,7 +122,7 @@ public static IObservable<IEnumerable<T>> GetAllObjects<T>(this IBlobCache blobC
.Where(y =>
y.StartsWith(GetTypePrefixedKey(string.Empty, typeof(T)), StringComparison.InvariantCulture))
.ToObservable())
.SelectMany(x => blobCache.GetObject<T>(x)
.SelectMany(x => blobCache.GetObject<T>(x).Where(x => x is not null).Select(x => x!)
.Catch(Observable.Empty<T>()))
.ToList();
}
Expand All @@ -147,14 +147,14 @@ public static IObservable<IEnumerable<T>> GetAllObjects<T>(this IBlobCache blobC
/// <typeparam name="T">The type of item to get.</typeparam>
/// <returns>A Future result representing the deserialized object from
/// the cache.</returns>
public static IObservable<T> GetOrFetchObject<T>(this IBlobCache blobCache, string key, Func<IObservable<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null)
public static IObservable<T?> GetOrFetchObject<T>(this IBlobCache blobCache, string key, Func<IObservable<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null)
{
if (blobCache is null)
{
throw new ArgumentNullException(nameof(blobCache));
}

return blobCache.GetObject<T>(key).Catch<T, Exception>(ex =>
return blobCache.GetObject<T>(key).Catch<T?, Exception>(ex =>
{
var prefixedKey = blobCache.GetHashCode().ToString(CultureInfo.InvariantCulture) + key;

Expand Down Expand Up @@ -184,7 +184,7 @@ public static IObservable<T> GetOrFetchObject<T>(this IBlobCache blobCache, stri
/// <param name="absoluteExpiration">An optional expiration date.</param>
/// <returns>A Future result representing the deserialized object from
/// the cache.</returns>
public static IObservable<T> GetOrFetchObject<T>(this IBlobCache blobCache, string key, Func<Task<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null)
public static IObservable<T?> GetOrFetchObject<T>(this IBlobCache blobCache, string key, Func<Task<T>> fetchFunc, DateTimeOffset? absoluteExpiration = null)
{
if (blobCache is null)
{
Expand All @@ -210,7 +210,7 @@ public static IObservable<T> GetOrFetchObject<T>(this IBlobCache blobCache, stri
/// <param name="absoluteExpiration">An optional expiration date.</param>
/// <returns>A Future result representing the deserialized object from
/// the cache.</returns>
public static IObservable<T> GetOrCreateObject<T>(this IBlobCache blobCache, string key, Func<T> fetchFunc, DateTimeOffset? absoluteExpiration = null)
public static IObservable<T?> GetOrCreateObject<T>(this IBlobCache blobCache, string key, Func<T> fetchFunc, DateTimeOffset? absoluteExpiration = null)
{
if (blobCache is null)
{
Expand Down Expand Up @@ -275,7 +275,7 @@ public static IObservable<T> GetOrCreateObject<T>(this IBlobCache blobCache, str
/// <returns>An Observable stream containing either one or two
/// results (possibly a cached version, then the latest version).</returns>
[SuppressMessage("Design", "CA2000: call dispose", Justification = "Disposed by member")]
public static IObservable<T> GetAndFetchLatest<T>(
public static IObservable<T?> GetAndFetchLatest<T>(
this IBlobCache blobCache,
string key,
Func<IObservable<T>> fetchFunc,
Expand All @@ -291,7 +291,7 @@ public static IObservable<T> GetAndFetchLatest<T>(

#pragma warning disable CS8604 // Possible null reference argument.
var fetch = Observable.Defer(() => blobCache.GetObjectCreatedAt<T>(key))
.Select(x => fetchPredicate == null || x == null || fetchPredicate(x.Value))
.Select(x => fetchPredicate is null || x is null || fetchPredicate(x.Value))
.Where(x => x)
.SelectMany(_ =>
{
Expand All @@ -305,23 +305,28 @@ public static IObservable<T> GetAndFetchLatest<T>(

return fetchObs
.SelectMany(x =>
cacheValidationPredicate != null && !cacheValidationPredicate(x)
cacheValidationPredicate is not null && !cacheValidationPredicate(x)
? Observable.Return(default(T))
: blobCache.InvalidateObject<T>(key).Select(__ => x))
.SelectMany(x =>
cacheValidationPredicate != null && !cacheValidationPredicate(x)
cacheValidationPredicate is not null && !cacheValidationPredicate(x)
? Observable.Return(default(T))
: blobCache.InsertObject(key, x, absoluteExpiration).Select(__ => x));
});

var result = blobCache.GetObject<T>(key).Select(x => new Tuple<T, bool>(x, true))
.Catch(Observable.Return(new Tuple<T, bool>(default(T), false)));
if (fetch is null)
{
return Observable.Throw<T>(new Exception("Could not find a valid way to fetch the value"));
}

var result = blobCache.GetObject<T>(key).Select(x => (x, true))
.Catch(Observable.Return((default(T), false)));

#pragma warning restore CS8604 // Possible null reference argument.

return result.SelectMany(x => x.Item2 ? Observable.Return(x.Item1) : Observable.Empty<T>())
.Concat(fetch)
.Multicast(new ReplaySubject<T>())
.Multicast(new ReplaySubject<T?>())
.RefCount();
}

Expand Down Expand Up @@ -356,7 +361,7 @@ public static IObservable<T> GetAndFetchLatest<T>(
/// if the fetched value should be cached.</param>
/// <returns>An Observable stream containing either one or two
/// results (possibly a cached version, then the latest version).</returns>
public static IObservable<T> GetAndFetchLatest<T>(
public static IObservable<T?> GetAndFetchLatest<T>(
this IBlobCache blobCache,
string key,
Func<Task<T>> fetchFunc,
Expand Down Expand Up @@ -447,7 +452,7 @@ internal static byte[] SerializeObject<T>(T value)
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value, settings));
}

internal static IObservable<T> DeserializeObject<T>(byte[] x)
internal static IObservable<T?> DeserializeObject<T>(byte[] x)
{
var settings = Locator.Current.GetService<JsonSerializerSettings>();

Expand Down
29 changes: 15 additions & 14 deletions src/Akavache.Core/KeyedOperations/KeyedOperationQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public IObservable<Unit> ShutdownQueue()
{
lock (_queuedOps)
{
if (_shutdownObs != null)
if (_shutdownObs is not null)
{
return _shutdownObs;
}
Expand All @@ -113,7 +113,7 @@ public IObservable<Unit> ShutdownQueue()
var sub = _resultObs.Materialize()
.Where(x => x.Kind != NotificationKind.OnNext)
.SelectMany(x =>
(x.Kind == NotificationKind.OnError) ?
(x.Kind == NotificationKind.OnError && x.Exception is not null) ?
Observable.Throw<Unit>(x.Exception) :
Observable.Return(Unit.Default))
.Multicast(_shutdownObs);
Expand Down Expand Up @@ -156,19 +156,20 @@ private IObservable<T> SafeStart<T>(Func<T> calculationFunc)
var ret = new AsyncSubject<T>();
Observable.Start(
() =>
{
try
{
var val = calculationFunc();
ret.OnNext(val);
ret.OnCompleted();
}
catch (Exception ex)
{
this.Log().Warn(ex, "Failure running queued op");
ret.OnError(ex);
}
}, _scheduler);
try
{
var val = calculationFunc();
ret.OnNext(val);
ret.OnCompleted();
}
catch (Exception ex)
{
this.Log().Warn(ex, "Failure running queued op");
ret.OnError(ex);
}
},
_scheduler);

return ret;
}
Expand Down
Loading

0 comments on commit 445b500

Please sign in to comment.