add cache size hard cap based on last access time, remove more unnecessary things

This commit is contained in:
Stanley Dimant
2022-07-05 00:55:07 +02:00
parent 90ec056a11
commit 7141c11059
6 changed files with 26 additions and 49 deletions

View File

@@ -55,6 +55,7 @@ namespace MareSynchronos
public string CacheFolder { get; set; } = string.Empty; public string CacheFolder { get; set; } = string.Empty;
public Dictionary<string, string> ClientSecret { get; set; } = new(); public Dictionary<string, string> ClientSecret { get; set; } = new();
public Dictionary<string, string> CustomServerList { get; set; } = new(); public Dictionary<string, string> CustomServerList { get; set; } = new();
public int MaxLocalCacheInGiB { get; set; } = 20;
public bool InitialScanComplete { get; set; } = false; public bool InitialScanComplete { get; set; } = false;
public int MaxParallelScan public int MaxParallelScan

View File

@@ -122,7 +122,6 @@ public class CharacterDataFactory
_dalamudUtil.WaitWhileCharacterIsDrawing(_dalamudUtil.PlayerPointer); _dalamudUtil.WaitWhileCharacterIsDrawing(_dalamudUtil.PlayerPointer);
var cache = new CharacterData var cache = new CharacterData
{ {
JobId = _dalamudUtil.PlayerJobId,
GlamourerString = _ipcManager.GlamourerGetCharacterCustomization(_dalamudUtil.PlayerCharacter), GlamourerString = _ipcManager.GlamourerGetCharacterCustomization(_dalamudUtil.PlayerCharacter),
ManipulationString = _ipcManager.PenumbraGetMetaManipulations(_dalamudUtil.PlayerName) ManipulationString = _ipcManager.PenumbraGetMetaManipulations(_dalamudUtil.PlayerName)
}; };

View File

@@ -19,10 +19,9 @@ namespace MareSynchronos.Managers
private FileSystemWatcher? _cacheDirWatcher; private FileSystemWatcher? _cacheDirWatcher;
private FileSystemWatcher? _penumbraDirWatcher; private FileSystemWatcher? _penumbraDirWatcher;
private Task? _rescanTask; private Task? _rescanTask;
private CancellationTokenSource _rescanTaskCancellationTokenSource = new CancellationTokenSource(); private readonly CancellationTokenSource _rescanTaskCancellationTokenSource = new();
private CancellationTokenSource _rescanTaskRunCancellationTokenSource = new CancellationTokenSource(); private CancellationTokenSource _rescanTaskRunCancellationTokenSource = new();
private CancellationTokenSource? _scanCancellationTokenSource; private CancellationTokenSource? _scanCancellationTokenSource;
private Task? _scanTask;
public FileCacheManager(IpcManager ipcManager, Configuration pluginConfiguration) public FileCacheManager(IpcManager ipcManager, Configuration pluginConfiguration)
{ {
Logger.Verbose("Creating " + nameof(FileCacheManager)); Logger.Verbose("Creating " + nameof(FileCacheManager));
@@ -143,17 +142,18 @@ namespace MareSynchronos.Managers
private void RecalculateFileCacheSize() private void RecalculateFileCacheSize()
{ {
FileCacheSize = 0; FileCacheSize = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder).Sum(f => new FileInfo(f).Length);
foreach (var file in Directory.EnumerateFiles(_pluginConfiguration.CacheFolder))
if (FileCacheSize <= _pluginConfiguration.MaxLocalCacheInGiB * 1024 * 1024 * 1024) return;
var allFiles = Directory.EnumerateFiles(_pluginConfiguration.CacheFolder)
.Select(f => new FileInfo(f)).OrderBy(f => f.LastAccessTime).ToList();
while (FileCacheSize > _pluginConfiguration.MaxLocalCacheInGiB * 1024 * 1024 * 1024)
{ {
try var oldestFile = allFiles.First();
{ FileCacheSize -= oldestFile.Length;
FileCacheSize += new FileInfo(file).Length; File.Delete(oldestFile.FullName);
} allFiles.Remove(oldestFile);
catch
{
// whatever
}
} }
} }
@@ -182,7 +182,6 @@ namespace MareSynchronos.Managers
await using var db = new FileCacheContext(); await using var db = new FileCacheContext();
foreach (var item in listCopy.Distinct()) foreach (var item in listCopy.Distinct())
{ {
var fi = new FileInfo(item); var fi = new FileInfo(item);
if (!fi.Exists) if (!fi.Exists)
{ {
@@ -215,10 +214,13 @@ namespace MareSynchronos.Managers
Directory.EnumerateFiles(penumbraDir, "*.*", SearchOption.AllDirectories) Directory.EnumerateFiles(penumbraDir, "*.*", SearchOption.AllDirectories)
.Select(s => s.ToLowerInvariant()) .Select(s => s.ToLowerInvariant())
.Where(f => f.Contains(@"\chara\")) .Where(f => f.Contains(@"\chara\"))
.Where(f =>
(f.EndsWith(".tex", StringComparison.OrdinalIgnoreCase)
|| f.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase)
|| f.EndsWith(".mtrl", StringComparison.OrdinalIgnoreCase)))
.Concat(Directory.EnumerateFiles(_pluginConfiguration.CacheFolder, "*.*", SearchOption.AllDirectories) .Concat(Directory.EnumerateFiles(_pluginConfiguration.CacheFolder, "*.*", SearchOption.AllDirectories)
.Select(s => s.ToLowerInvariant())) .Select(s => s.ToLowerInvariant()))
.Where(f => (f.EndsWith(".tex", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".mdl", StringComparison.OrdinalIgnoreCase) || f.EndsWith(".mtrl", StringComparison.OrdinalIgnoreCase))) .Select(p => new KeyValuePair<string, bool>(p, false)).ToList());
.Select(p => new KeyValuePair<string, bool>(p, false)));
List<FileCache> fileCaches; List<FileCache> fileCaches;
await using (var db = new FileCacheContext()) await using (var db = new FileCacheContext())
fileCaches = db.FileCaches.ToList(); fileCaches = db.FileCaches.ToList();

View File

@@ -16,9 +16,6 @@ namespace MareSynchronos.Models
public bool IsReady => FileReplacements.All(f => f.Computed); public bool IsReady => FileReplacements.All(f => f.Computed);
[JsonProperty]
public int JobId { get; set; } = 0;
public string ManipulationString { get; set; } = string.Empty; public string ManipulationString { get; set; } = string.Empty;
public void AddFileReplacement(FileReplacement fileReplacement) public void AddFileReplacement(FileReplacement fileReplacement)
@@ -49,7 +46,6 @@ namespace MareSynchronos.Models
}; };
}).ToList(), }).ToList(),
GlamourerData = GlamourerString, GlamourerData = GlamourerString,
JobId = JobId,
ManipulationData = ManipulationString ManipulationData = ManipulationString
}; };
} }

View File

@@ -344,6 +344,13 @@ namespace MareSynchronos.UI
{ {
ColorTextWrapped("The folder you selected does not exist or cannot be written to. Please provide a valid path.", ImGuiColors.DalamudRed); ColorTextWrapped("The folder you selected does not exist or cannot be written to. Please provide a valid path.", ImGuiColors.DalamudRed);
} }
int maxCacheSize = _pluginConfiguration.MaxLocalCacheInGiB;
if (ImGui.SliderInt("Maximum Cache Size in GB", ref maxCacheSize, 1, 50, "%d GB"))
{
_pluginConfiguration.MaxLocalCacheInGiB = maxCacheSize;
_pluginConfiguration.Save();
}
} }
private bool _isDirectoryWritable = false; private bool _isDirectoryWritable = false;

View File

@@ -23,7 +23,6 @@ namespace MareSynchronos.Utils
private readonly ClientState _clientState; private readonly ClientState _clientState;
private readonly ObjectTable _objectTable; private readonly ObjectTable _objectTable;
private readonly Framework _framework; private readonly Framework _framework;
public event PlayerChange? PlayerChanged;
public event LogIn? LogIn; public event LogIn? LogIn;
public event LogOut? LogOut; public event LogOut? LogOut;
public event FrameworkUpdate? FrameworkUpdate; public event FrameworkUpdate? FrameworkUpdate;
@@ -59,43 +58,16 @@ namespace MareSynchronos.Utils
public bool IsLoggedIn => _clientState.IsLoggedIn; public bool IsLoggedIn => _clientState.IsLoggedIn;
private void WatcherOnPlayerChanged(Character actor)
{
PlayerChanged?.Invoke(actor);
}
public bool IsPlayerPresent => _clientState.LocalPlayer != null; public bool IsPlayerPresent => _clientState.LocalPlayer != null;
public string PlayerName => _clientState.LocalPlayer?.Name.ToString() ?? "--"; public string PlayerName => _clientState.LocalPlayer?.Name.ToString() ?? "--";
public int PlayerJobId => (int)_clientState.LocalPlayer!.ClassJob.Id;
public IntPtr PlayerPointer => _clientState.LocalPlayer!.Address; public IntPtr PlayerPointer => _clientState.LocalPlayer!.Address;
public PlayerCharacter PlayerCharacter => _clientState.LocalPlayer!; public PlayerCharacter PlayerCharacter => _clientState.LocalPlayer!;
public string PlayerNameHashed => Crypto.GetHash256(PlayerName + _clientState.LocalPlayer!.HomeWorld.Id); public string PlayerNameHashed => Crypto.GetHash256(PlayerName + _clientState.LocalPlayer!.HomeWorld.Id);
public Dictionary<string, PlayerCharacter> GetLocalPlayers()
{
if (!_clientState.IsLoggedIn)
{
return new Dictionary<string, PlayerCharacter>();
}
Dictionary<string, PlayerCharacter> allLocalPlayers = new();
foreach (var obj in _objectTable)
{
if (obj.ObjectKind != Dalamud.Game.ClientState.Objects.Enums.ObjectKind.Player) continue;
string playerName = obj.Name.ToString();
if (playerName == PlayerName) continue;
var playerObject = (PlayerCharacter)obj;
allLocalPlayers[Crypto.GetHash256(playerObject.Name.ToString() + playerObject.HomeWorld.Id.ToString())] = playerObject;
}
return allLocalPlayers;
}
public List<PlayerCharacter> GetPlayerCharacters() public List<PlayerCharacter> GetPlayerCharacters()
{ {
return _objectTable.Where(obj => return _objectTable.Where(obj =>