Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache): switched to concurrent cache #1609

Open
wants to merge 5 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LiteDB.Shell/LiteDB.Shell.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFrameworks>netcoreapp2.0</TargetFrameworks>
Expand Down
31 changes: 25 additions & 6 deletions LiteDB/Engine/Services/CacheService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

#if !NET35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LiteDB doesn't support NET35, so these conditions could be removed here and below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fixes v4 which targets 3.5

using System.Collections.Concurrent;
#endif

namespace LiteDB
{
internal class CacheService
{

#if NET35
/// <summary>
/// Collection to store clean only pages in cache
/// </summary>
Expand All @@ -15,6 +22,17 @@ internal class CacheService
/// Collection to store dirty only pages in cache. If page was in _clean, remove from there and insert here
/// </summary>
private Dictionary<uint, BasePage> _dirty = new Dictionary<uint, BasePage>();
#else
/// <summary>
/// Collection to store clean only pages in cache
/// </summary>
private ConcurrentDictionary<uint, BasePage> _clean = new ConcurrentDictionary<uint, BasePage>();

/// <summary>
/// Collection to store dirty only pages in cache. If page was in _clean, remove from there and insert here
/// </summary>
private ConcurrentDictionary<uint, BasePage> _dirty = new ConcurrentDictionary<uint, BasePage>();
#endif

private IDiskService _disk;
private Logger _log;
Expand Down Expand Up @@ -58,7 +76,11 @@ public void AddPage(BasePage page)
/// </summary>
public void SetDirty(BasePage page)
{
#if NET35
_clean.Remove(page.PageID);
#else
_clean.TryRemove(page.PageID, out _);
#endif
page.IsDirty = true;
_dirty[page.PageID] = page;
}
Expand Down Expand Up @@ -114,11 +136,8 @@ public void MarkDirtyAsClean()
/// </summary>
public void ClearPages()
{
lock(_clean)
{
_log.Write(Logger.CACHE, "cleaning cache");
_clean.Clear();
}
_log.Write(Logger.CACHE, "cleaning cache");
_clean.Clear();
}
}
}
}
2 changes: 1 addition & 1 deletion LiteDB/LiteDB.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup Condition=" '$(OS)' != 'Windows_NT' ">
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
Expand Down