Skip to content

Commit

Permalink
feature: Clear the DuckDuckGo Cache (#62)
Browse files Browse the repository at this point in the history
* feature: Clear the DuckDuckGo Cache

adding back clearing cache on query

* fixing unit tests
  • Loading branch information
RLittlesII committed Jun 24, 2021
1 parent 55bdc10 commit 81ee43e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
26 changes: 23 additions & 3 deletions src/Data/DuckDuckGo/DuckDuckGoFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,31 @@ public static IEnumerable<RelatedTopic> AsResult(this SearchResult searchResult)
/// </summary>
/// <param name="result">The result.</param>
/// <param name="cache">The cache.</param>
/// <param name="clearCache">A value determining whether to clear the cache.</param>
/// <returns>A completion notification.</returns>
public static IObservable<IChangeSet<RelatedTopic, string>> Cache(
this IObservable<IEnumerable<RelatedTopic>> result,
SourceCache<RelatedTopic, string> cache) => result
.Do(duckDuckGoQueryResults => cache.EditDiff(duckDuckGoQueryResults, (first, second) => first.FirstUrl == second.FirstUrl))
.SelectMany(_ => cache.Connect().RefCount());
SourceCache<RelatedTopic, string> cache,
bool clearCache) => result
.Do(UpdateCache(cache, clearCache))
.Select(_ => cache.Connect().RefCount())
.Switch();

private static Action<IEnumerable<RelatedTopic>> UpdateCache(SourceCache<RelatedTopic, string> cache, bool clearCache) => duckDuckGoQueryResults =>
{
if (clearCache)
{
cache
.Edit(updater =>
{
updater.Clear();
updater.AddOrUpdate(duckDuckGoQueryResults);
});
}
else
{
cache.EditDiff(duckDuckGoQueryResults, (first, second) => first.FirstUrl == second.FirstUrl);
}
};
}
}
4 changes: 2 additions & 2 deletions src/Data/DuckDuckGo/DuckDuckGoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public IObservable<IChangeSet<RelatedTopic, string>> Query(string query) => Obse
.Search(query)
.Catch(_queryException)
.Select(x => x.AsResult())
.Cache(_queryResults)
.Cache(_queryResults, false)
.Subscribe(observer));

/// <inheritdoc/>
Expand All @@ -43,7 +43,7 @@ public IObservable<IChangeSet<RelatedTopic, string>> Query(string query, bool cl
.Search(query)
.Catch(_queryException)
.Select(x => x.AsResult())
.Cache(_queryResults)
.Cache(_queryResults, clearCache)
.Subscribe(observer));

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion src/Data/DuckDuckGo/Models/Topic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Topic
/// <summary>
/// Gets or sets the first url.
/// </summary>
public string FirstURL { get; set; }
public string FirstUrl { get; set; }

/// <summary>
/// Gets or sets the icon.
Expand Down
6 changes: 3 additions & 3 deletions test/Airframe.Tests/Data/DuckGo/DuckDuckGoFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Should_Return_ChangeSet()
Text = "text"
}
})
.Cache(sourceCache)
.Cache(sourceCache, true)
.Bind(out var result)
.Subscribe();

Expand Down Expand Up @@ -68,7 +68,7 @@ public void Should_Return_Cached()
Text = "text"
}
})
.Cache(sourceCache)
.Cache(sourceCache, true)
.Bind(out var result)
.Subscribe();

Expand Down Expand Up @@ -101,7 +101,7 @@ public void Should_Clear_Cached()
Text = "text"
}
})
.Cache(sourceCache)
.Cache(sourceCache, true)
.Bind(out var result)
.Subscribe();

Expand Down

0 comments on commit 81ee43e

Please sign in to comment.