Skip to content

Commit

Permalink
fix: 🐛 allow caching null values
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed May 18, 2023
1 parent 637aab2 commit bd14f00
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
39 changes: 39 additions & 0 deletions src/NullableCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Runtime.Caching;

namespace Flow.Launcher.Plugin.TogglTrack
{
internal class NullableCache
{
private readonly static object NullObject = new object();
private readonly MemoryCache _cache = MemoryCache.Default;

internal bool Contains(string key)
{
return this._cache.Contains(key);
}

internal object? Get(string key)
{
var retrieved = this._cache.Get(key);

return (retrieved == NullableCache.NullObject)
? null
: retrieved;
}

internal void Set(string key, object? value, DateTimeOffset absoluteExpiration)
{
var set = (value is null)
? NullableCache.NullObject
: value;

this._cache.Set(key, set, absoluteExpiration);
}

internal object Remove(string key)
{
return this._cache.Remove(key);
}
}
}
10 changes: 1 addition & 9 deletions src/TogglTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.Caching;
using Humanizer;
using TimeSpanParserUtil;
using Flow.Launcher.Plugin.TogglTrack.TogglApi;
Expand All @@ -20,7 +19,7 @@ internal class TogglTrack
private (bool IsValid, string Token) _lastToken = (false, string.Empty);

private readonly (SemaphoreSlim Token, SemaphoreSlim Me, SemaphoreSlim RunningTimeEntries, SemaphoreSlim TimeEntries) _semaphores = (new SemaphoreSlim(1, 1), new SemaphoreSlim(1, 1), new SemaphoreSlim(1, 1), new SemaphoreSlim(1, 1));
private MemoryCache _cache = MemoryCache.Default;
private NullableCache _cache = new NullableCache();

private long? _selectedProjectId = -1;

Expand Down Expand Up @@ -59,9 +58,7 @@ internal TogglTrack(PluginInitContext context, Settings settings)

var me = await this._client.GetMe();

#pragma warning disable CS8604 // Possible null reference argument
this._cache.Set(cacheKey, me, DateTimeOffset.Now.AddDays(3));
#pragma warning restore CS8604 // Possible null reference argument

this._semaphores.Me.Release();
return me;
Expand Down Expand Up @@ -94,9 +91,7 @@ internal TogglTrack(PluginInitContext context, Settings settings)

var runningTimeEntry = await this._client.GetRunningTimeEntry();

#pragma warning disable CS8604 // Possible null reference argument
this._cache.Set(cacheKey, runningTimeEntry, DateTimeOffset.Now.AddSeconds(30));
#pragma warning restore CS8604 // Possible null reference argument

this._semaphores.RunningTimeEntries.Release();
return runningTimeEntry;
Expand Down Expand Up @@ -129,10 +124,7 @@ internal TogglTrack(PluginInitContext context, Settings settings)

var timeEntries = await this._client.GetTimeEntries();

#pragma warning disable CS8604 // Possible null reference argument
// TODO: value cannot be null
this._cache.Set(cacheKey, timeEntries, DateTimeOffset.Now.AddSeconds(30));
#pragma warning restore CS8604 // Possible null reference argument

this._semaphores.TimeEntries.Release();
return timeEntries;
Expand Down

0 comments on commit bd14f00

Please sign in to comment.