Skip to content

Commit

Permalink
remove lite db caches.
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Jun 18, 2024
1 parent 1357209 commit 6ef998a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ public Result JobStateChange(string jobId, JobState state)
_timedHostedService.JobDelete(jobId);

// 修复 https://github.com/trueai-org/MDriveSync/issues/4
var ds = AliyunStorageDb.Instance.DB.GetAll(false);
var ds = AliyunStorageDb.Instance.DB.GetAll();
foreach (var d in ds)
{
var jo = d?.Jobs?.FirstOrDefault(x => x.Id == jobId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public Result JobStateChange(string jobId, JobState state)
// 删除作业,清除服务
_localStorageHostedService.JobDelete(jobId);

var ds = AliyunStorageDb.Instance.DB.GetAll(false);
var ds = AliyunStorageDb.Instance.DB.GetAll();
foreach (var d in ds)
{
var jo = d?.Jobs?.FirstOrDefault(x => x.Id == jobId);
Expand Down
2 changes: 1 addition & 1 deletion src/MDriveSync.Core/DB/AliyunStorageDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// </summary>
public class AliyunStorageDb : SingletonBase<AliyunStorageDb>
{
private readonly LiteRepository<AliyunStorageConfig, string> _db = new("mdrive.db", false);
private readonly LiteRepository<AliyunStorageConfig, string> _db = new("mdrive.db");

public LiteRepository<AliyunStorageConfig, string> DB
{
Expand Down
60 changes: 6 additions & 54 deletions src/MDriveSync.Core/DB/LiteRepository.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using LiteDB;
using MDriveSync.Core.Models;
using ServiceStack;
using System.Collections.Concurrent;

namespace MDriveSync.Core.DB
{
Expand All @@ -20,18 +19,13 @@ public interface IBaseId<T>
{
private static readonly object _lock = new();
private readonly LiteDatabase _db;
private readonly ConcurrentDictionary<TId, T> _cache = new();
private readonly bool? _useCache;

/// <summary>
/// 创建 LiteDB 数据库的实例。
/// </summary>
/// <param name="dbName">数据库名称,例如:log.db</param>
/// <param name="noCache">是否使用缓存</param>
public LiteRepository(string dbName, bool? noCache = null)
public LiteRepository(string dbName)
{
_useCache = noCache;

var dbPath = Path.Combine(Directory.GetCurrentDirectory(), "data", dbName);
lock (_lock)
{
Expand Down Expand Up @@ -65,10 +59,6 @@ public void Add(T entity)
{
var col = _db.GetCollection<T>();
col.Insert(entity);
if (_useCache == true)
{
_cache.TryAdd(entity.Id, entity);
}
}

// 批量增加,优化处理超过 10000 条记录的情况
Expand All @@ -83,15 +73,6 @@ public void AddRange(IEnumerable<T> entities)
{
var batch = entityList.Skip(i).Take(batchSize);
col.InsertBulk(batch);

// 更新缓存
if (_useCache == true)
{
foreach (var entity in batch)
{
_cache.TryAdd(entity.Id, entity);
}
}
}
}

Expand All @@ -100,56 +81,27 @@ public void Delete(TId id)
{
var col = _db.GetCollection<T>();
col.Delete(new BsonValue(id));
if (_useCache == true)
{
_cache.TryRemove(id, out _);
}
}

// 修改
public void Update(T entity)
{
var col = _db.GetCollection<T>();
col.Update(entity);
if (_useCache == true)
{
_cache[entity.Id] = entity;
}
}

// 查询所有
public List<T> GetAll(bool? useCache = null)
public List<T> GetAll()
{
if (useCache == null)
useCache = _useCache;

if (useCache == true)
{
if (!_cache.Any())
{
return GetAll(false);
}
return _cache.Values.ToList();
}
else
{
var col = _db.GetCollection<T>();
return col.FindAll().ToList();
}
var col = _db.GetCollection<T>();
return col.FindAll().ToList();
}

// 根据ID查询
public T Get(TId id)
{
if (_useCache == true && _cache.TryGetValue(id, out T entity))
{
return entity;
}
else
{
var col = _db.GetCollection<T>();
return col.FindById(new BsonValue(id));
}
var col = _db.GetCollection<T>();
return col.FindById(new BsonValue(id));
}
}
}
2 changes: 1 addition & 1 deletion src/MDriveSync.Core/DB/LocalStorageDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MDriveSync.Core.DB
/// </summary>
public class LocalStorageDb : SingletonBase<LocalStorageDb>
{
private readonly LiteRepository<LocalStorageConfig, string> _db = new("mdrive.db", false);
private readonly LiteRepository<LocalStorageConfig, string> _db = new("mdrive.db");

public LiteRepository<LocalStorageConfig, string> DB
{
Expand Down
4 changes: 2 additions & 2 deletions src/MDriveSync.Core/DownloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class DownloadManager
// 下载速度限制(字节/秒)
private int downloadSpeedLimit = 0;

private LiteRepository<DownloadTask, string> _taskDb = new("mdrive.db", false);
private LiteRepository<DownloadTask, string> _taskDb = new("mdrive.db");

private LiteRepository<DownloadManagerSetting, string> _settingDb = new("mdrive.db", false);
private LiteRepository<DownloadManagerSetting, string> _settingDb = new("mdrive.db");

private DownloadManager()
{
Expand Down

0 comments on commit 6ef998a

Please sign in to comment.