Fix VRCX.json being slow to save

This commit is contained in:
Natsumi
2025-10-16 13:19:40 +11:00
parent 23d9850a25
commit 617d2e161d
6 changed files with 37 additions and 107 deletions

View File

@@ -204,7 +204,7 @@ namespace VRCX
VRCXStorage.Instance.Set("VRCX_SizeWidth", LastSizeWidth.ToString()); VRCXStorage.Instance.Set("VRCX_SizeWidth", LastSizeWidth.ToString());
VRCXStorage.Instance.Set("VRCX_SizeHeight", LastSizeHeight.ToString()); VRCXStorage.Instance.Set("VRCX_SizeHeight", LastSizeHeight.ToString());
VRCXStorage.Instance.Set("VRCX_WindowState", ((int)LastWindowStateToRestore).ToString()); VRCXStorage.Instance.Set("VRCX_WindowState", ((int)LastWindowStateToRestore).ToString());
VRCXStorage.Instance.Flush(); VRCXStorage.Instance.Save();
} }
private void MainForm_FormClosed(object sender, FormClosedEventArgs e) private void MainForm_FormClosed(object sender, FormClosedEventArgs e)

View File

@@ -1,10 +1,5 @@
// Copyright(c) 2019-2025 pypy, Natsumi and individual contributors.
// All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text.Json; using System.Text.Json;
@@ -15,127 +10,72 @@ namespace VRCX
public class VRCXStorage public class VRCXStorage
{ {
public static readonly VRCXStorage Instance; public static readonly VRCXStorage Instance;
private static readonly ReaderWriterLockSlim m_Lock = new ReaderWriterLockSlim();
private static Dictionary<string, string> m_Storage = new Dictionary<string, string>(); private static ConcurrentDictionary<string, string> _storage = new ConcurrentDictionary<string, string>();
private static readonly string m_JsonPath = Path.Join(Program.AppDataDirectory, "VRCX.json"); private static readonly string JsonPath = Path.Join(Program.AppDataDirectory, "VRCX.json");
private static bool m_Dirty;
private static readonly TimeSpan SaveDebounce = TimeSpan.FromMilliseconds(500);
private static readonly Timer SaveTimer;
private static readonly Lock SaveLock = new Lock();
static VRCXStorage() static VRCXStorage()
{ {
Instance = new VRCXStorage(); Instance = new VRCXStorage();
SaveTimer = new Timer(_ => Instance.Save(), null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
} }
public void Load() public void Load()
{ {
m_Lock.EnterWriteLock(); var tmp = new Dictionary<string, string>();
try JsonFileSerializer.Deserialize(JsonPath, ref tmp);
{ _storage = new ConcurrentDictionary<string, string>(tmp);
JsonFileSerializer.Deserialize(m_JsonPath, ref m_Storage);
m_Dirty = false;
}
finally
{
m_Lock.ExitWriteLock();
}
} }
public void Save() public void Save()
{ {
m_Lock.EnterReadLock(); lock (SaveLock)
try
{ {
if (m_Dirty) var snapshot = new Dictionary<string, string>(_storage);
{ JsonFileSerializer.Serialize(JsonPath, snapshot);
JsonFileSerializer.Serialize(m_JsonPath, m_Storage);
m_Dirty = false;
}
} }
finally
{
m_Lock.ExitReadLock();
}
}
public void Flush()
{
Save();
} }
public void Clear() public void Clear()
{ {
m_Lock.EnterWriteLock(); if (!_storage.IsEmpty)
try
{ {
if (m_Storage.Count > 0) _storage.Clear();
{ ScheduleSave();
m_Storage.Clear();
m_Dirty = true;
}
}
finally
{
m_Lock.ExitWriteLock();
} }
} }
public bool Remove(string key) public bool Remove(string key)
{ {
m_Lock.EnterWriteLock(); var result = _storage.TryRemove(key, out _);
try if (result)
{ ScheduleSave();
var result = m_Storage.Remove(key); return result;
if (result)
{
m_Dirty = true;
}
return result;
}
finally
{
m_Lock.ExitWriteLock();
}
} }
public string Get(string key) public string Get(string key)
{ {
m_Lock.EnterReadLock(); return _storage.TryGetValue(key, out var value) ? value : string.Empty;
try
{
return m_Storage.TryGetValue(key, out string value)
? value
: string.Empty;
}
finally
{
m_Lock.ExitReadLock();
}
} }
public void Set(string key, string value) public void Set(string key, string value)
{ {
m_Lock.EnterWriteLock(); _storage[key] = value;
try ScheduleSave();
{
m_Storage[key] = value;
m_Dirty = true;
}
finally
{
m_Lock.ExitWriteLock();
}
} }
public string GetAll() public string GetAll()
{ {
m_Lock.EnterReadLock(); return JsonSerializer.Serialize(new Dictionary<string, string>(_storage));
try }
{
return JsonSerializer.Serialize(m_Storage); private static void ScheduleSave()
} {
finally SaveTimer.Change(SaveDebounce, Timeout.InfiniteTimeSpan);
{
m_Lock.ExitReadLock();
}
} }
} }
} }

View File

@@ -94,7 +94,7 @@ namespace VRCX
catch (UriFormatException) catch (UriFormatException)
{ {
VRCXStorage.Instance.Set("VRCX_ProxyServer", string.Empty); VRCXStorage.Instance.Set("VRCX_ProxyServer", string.Empty);
VRCXStorage.Instance.Flush(); VRCXStorage.Instance.Save();
const string message = "The proxy server URI you used is invalid.\nVRCX will close, please correct the proxy URI."; const string message = "The proxy server URI you used is invalid.\nVRCX will close, please correct the proxy URI.";
#if !LINUX #if !LINUX
System.Windows.Forms.MessageBox.Show(message, "Invalid Proxy URI", MessageBoxButtons.OK, MessageBoxIcon.Error); System.Windows.Forms.MessageBox.Show(message, "Invalid Proxy URI", MessageBoxButtons.OK, MessageBoxIcon.Error);

View File

@@ -1,5 +1,3 @@
import * as workerTimers from 'worker-timers';
let VRCXStorage = {}; let VRCXStorage = {};
export default class { export default class {
@@ -40,12 +38,5 @@ export default class {
VRCXStorage.SetObject = function (key, value) { VRCXStorage.SetObject = function (key, value) {
this.Set(key, JSON.stringify(value)); this.Set(key, JSON.stringify(value));
}; };
workerTimers.setInterval(
() => {
VRCXStorage.Flush();
},
5 * 60 * 1000
);
} }
} }

View File

@@ -248,7 +248,7 @@ export const useGeneralSettingsStore = defineStore('GeneralSettings', () => {
'VRCX_ProxyServer', 'VRCX_ProxyServer',
vrcxStore.proxyServer vrcxStore.proxyServer
); );
await VRCXStorage.Flush(); await VRCXStorage.Save();
await new Promise((resolve) => { await new Promise((resolve) => {
workerTimers.setTimeout(resolve, 100); workerTimers.setTimeout(resolve, 100);
}); });
@@ -263,7 +263,7 @@ export const useGeneralSettingsStore = defineStore('GeneralSettings', () => {
'VRCX_ProxyServer', 'VRCX_ProxyServer',
vrcxStore.proxyServer vrcxStore.proxyServer
); );
await VRCXStorage.Flush(); await VRCXStorage.Save();
await new Promise((resolve) => { await new Promise((resolve) => {
workerTimers.setTimeout(resolve, 100); workerTimers.setTimeout(resolve, 100);
}); });

View File

@@ -90,7 +90,7 @@ export const useVrcxStore = defineStore('Vrcx', () => {
}); });
window.electron.onWindowStateChange((event, newState) => { window.electron.onWindowStateChange((event, newState) => {
state.windowState = newState.windowState; state.windowState = newState.toString();
debounce(saveVRCXWindowOption, 300)(); debounce(saveVRCXWindowOption, 300)();
}); });
@@ -364,7 +364,6 @@ export const useVrcxStore = defineStore('Vrcx', () => {
VRCXStorage.Set('VRCX_SizeWidth', state.sizeWidth.toString()); VRCXStorage.Set('VRCX_SizeWidth', state.sizeWidth.toString());
VRCXStorage.Set('VRCX_SizeHeight', state.sizeHeight.toString()); VRCXStorage.Set('VRCX_SizeHeight', state.sizeHeight.toString());
VRCXStorage.Set('VRCX_WindowState', state.windowState); VRCXStorage.Set('VRCX_WindowState', state.windowState);
VRCXStorage.Flush();
} }
} }