fix #185. Loading index cache on startup.

This commit is contained in:
qianlifeng
2014-12-15 22:58:49 +08:00
parent 32867d3666
commit 82106c1c8b
20 changed files with 233 additions and 56 deletions

View File

@@ -2,90 +2,92 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace Wox.Infrastructure.Storage
{
public abstract class BaseStorage<T> where T : class, new()
[Serializable]
public abstract class BaseStorage<T> : IStorage where T : class,IStorage,new()
{
private string configFolder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config");
private string fileSuffix = ".json";
private static object locker = new object();
private static T storage;
private readonly string configFolder = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Config");
public event Action<T> AfterLoadConfig;
protected virtual void OnAfterLoadConfig(T obj)
protected string ConfigPath
{
Action<T> handler = AfterLoadConfig;
if (handler != null) handler(obj);
get
{
return Path.Combine(configFolder, ConfigName + FileSuffix);
}
}
protected abstract string FileSuffix { get; }
protected abstract string ConfigName { get; }
private static object locker = new object();
protected static T serializedObject;
public event Action<T> AfterLoad;
protected virtual void OnAfterLoad(T obj)
{
Action<T> handler = AfterLoad;
if (handler != null) handler(obj);
}
public static T Instance
{
get
{
if (storage == null)
if (serializedObject == null)
{
lock (locker)
{
if (storage == null)
if (serializedObject == null)
{
storage = new T();
(storage as BaseStorage<T>).Load();
serializedObject = new T();
serializedObject.Load();
}
}
}
return storage;
return serializedObject;
}
}
protected virtual T LoadDefaultConfig()
/// <summary>
/// if loading storage failed, we will try to load default
/// </summary>
/// <returns></returns>
protected virtual T LoadDefault()
{
return storage;
return serializedObject;
}
private void Load()
protected abstract void LoadInternal();
protected abstract void SaveInternal();
public void Load()
{
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
if (!File.Exists(configPath))
if (!File.Exists(ConfigPath))
{
if (!Directory.Exists(configFolder))
{
Directory.CreateDirectory(configFolder);
File.Create(configPath).Close();
}
string json = File.ReadAllText(configPath);
if (!string.IsNullOrEmpty(json))
{
try
{
storage = JsonConvert.DeserializeObject<T>(json);
}
catch (Exception)
{
storage = LoadDefaultConfig();
}
File.Create(ConfigPath).Close();
}
else
{
storage = LoadDefaultConfig();
}
OnAfterLoadConfig(storage);
LoadInternal();
OnAfterLoad(serializedObject);
}
public void Save()
{
lock (locker)
{
//json is a good choise, readable and flexiable
string configPath = Path.Combine(configFolder, ConfigName + fileSuffix);
string json = JsonConvert.SerializeObject(storage, Formatting.Indented);
File.WriteAllText(configPath, json);
SaveInternal();
}
}
}
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace Wox.Infrastructure.Storage
{
/// <summary>
/// Stroage object using binary data
/// Normally, it has better performance, but not readable
/// </summary>
[Serializable]
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
{
protected override string FileSuffix
{
get { return ".dat"; }
}
protected override void LoadInternal()
{
try
{
FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter binaryFormatter = new BinaryFormatter();
serializedObject = binaryFormatter.Deserialize(fileStream) as T;
fileStream.Close();
}
catch (Exception)
{
serializedObject = LoadDefault();
}
}
protected override void SaveInternal()
{
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create);
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(fileStream, serializedObject);
fileStream.Close();
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Wox.Infrastructure.Storage
{
public interface IStorage
{
void Load();
void Save();
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace Wox.Infrastructure.Storage
{
/// <summary>
/// Serialize object using json format.
/// </summary>
public abstract class JsonStrorage<T> : BaseStorage<T> where T : class, IStorage, new()
{
protected override string FileSuffix
{
get { return ".json"; }
}
protected override void LoadInternal()
{
string json = File.ReadAllText(ConfigPath);
if (!string.IsNullOrEmpty(json))
{
try
{
serializedObject = JsonConvert.DeserializeObject<T>(json);
}
catch (Exception)
{
serializedObject = LoadDefault();
}
}
else
{
serializedObject = LoadDefault();
}
}
protected override void SaveInternal()
{
string json = JsonConvert.SerializeObject(serializedObject, Formatting.Indented);
File.WriteAllText(ConfigPath, json);
}
}
}

View File

@@ -8,7 +8,7 @@ using Wox.Plugin;
namespace Wox.Infrastructure.Storage
{
public class UserSelectedRecordStorage : BaseStorage<UserSelectedRecordStorage>
public class UserSelectedRecordStorage : JsonStrorage<UserSelectedRecordStorage>
{
[JsonProperty]
private Dictionary<string, int> records = new Dictionary<string, int>();

View File

@@ -4,14 +4,16 @@ using System.Linq;
using System.Text;
using Newtonsoft.Json;
namespace Wox.Infrastructure.Storage.UserSettings {
public class FolderLink {
namespace Wox.Infrastructure.Storage.UserSettings
{
public class FolderLink
{
[JsonProperty]
public string Path { get; set; }
public string Path { get; set; }
public string Nickname
{
get { return Path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); }
}
public string Nickname
{
get { return Path.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.None).Last(); }
}
}
}

View File

@@ -6,7 +6,7 @@ using Newtonsoft.Json;
namespace Wox.Infrastructure.Storage.UserSettings
{
public class UserSettingStorage : BaseStorage<UserSettingStorage>
public class UserSettingStorage : JsonStrorage<UserSettingStorage>
{
[JsonProperty]
public bool DontPromptUpdateMsg { get; set; }
@@ -146,7 +146,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
get { return "config"; }
}
protected override UserSettingStorage LoadDefaultConfig()
protected override UserSettingStorage LoadDefault()
{
DontPromptUpdateMsg = false;
Theme = "Dark";
@@ -165,7 +165,7 @@ namespace Wox.Infrastructure.Storage.UserSettings
return this;
}
protected override void OnAfterLoadConfig(UserSettingStorage storage)
protected override void OnAfterLoad(UserSettingStorage storage)
{
if (storage.CustomizedPluginConfigs == null)
{