genuine cache for program plugin

This commit is contained in:
bao-qian
2017-01-13 01:21:00 +00:00
parent 6b0ba94288
commit 2cf20f4b6c
3 changed files with 45 additions and 24 deletions

View File

@@ -15,6 +15,7 @@ namespace Wox.Plugin.Program
{ {
public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable
{ {
private static readonly object IndexLock = new object();
private static Win32[] _win32s = { }; private static Win32[] _win32s = { };
private static UWP.Application[] _uwps = { }; private static UWP.Application[] _uwps = { };
@@ -34,25 +35,38 @@ namespace Wox.Plugin.Program
{ {
_cacheStorage = new BinaryStorage<ProgramIndexCache>(); _cacheStorage = new BinaryStorage<ProgramIndexCache>();
_cache = _cacheStorage.Load(); _cache = _cacheStorage.Load();
_win32s = _cache.Programs;
var w = _cache.Win32s;
_win32s = w ?? new Win32[] {};
var u = _cache.UWPs;
_uwps = u ?? new UWP.Application[] { };
});
Log.Info($"Preload {_win32s.Length} win32 programs from cache");
Log.Info($"Preload {_uwps.Length} uwps from cache");
Task.Run(() =>
{
Stopwatch.Normal("Program Index", IndexPrograms);
}); });
Log.Info($"Preload {_win32s.Length} programs from cache");
Stopwatch.Normal("Program Index", IndexPrograms);
} }
public void Save() public void Save()
{ {
_settingsStorage.Save(); _settingsStorage.Save();
_cache.Programs = _win32s; _cache.Win32s = _win32s;
_cache.UWPs = _uwps;
_cacheStorage.Save(); _cacheStorage.Save();
} }
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
var results1 = _win32s.AsParallel().Select(p => p.Result(query.Search, _context.API)); lock (IndexLock)
var results2 = _uwps.AsParallel().Select(p => p.Result(query.Search, _context.API)); {
var result = results1.Concat(results2).Where(r => r.Score > 0).ToList(); var results1 = _win32s.AsParallel().Select(p => p.Result(query.Search, _context.API));
return result; var results2 = _uwps.AsParallel().Select(p => p.Result(query.Search, _context.API));
var result = results1.Concat(results2).Where(r => r.Score > 0).ToList();
return result;
}
} }
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
@@ -62,20 +76,22 @@ namespace Wox.Plugin.Program
public static void IndexPrograms() public static void IndexPrograms()
{ {
Win32[] w = {} ;
UWP.Application[] u = {};
var t1 = Task.Run(() => var t1 = Task.Run(() =>
{ {
_win32s = Win32.All(_settings); w = Win32.All(_settings);
}); });
var t2 = Task.Run(() => var t2 = Task.Run(() =>
{ {
_uwps = UWP.All(); u = UWP.All();
}); });
Task.WaitAll(t1, t2); Task.WaitAll(t1, t2);
var characters = _win32s.Select(p => p.Name) var characters = w.Select(p => p.Name)
.Concat(_win32s.Select(p => p.Description)) .Concat(w.Select(p => p.Description))
.Concat(_uwps.Select(p => p.DisplayName)) .Concat(u.Select(p => p.DisplayName))
.Concat(_uwps.Select(p => p.Description)); .Concat(u.Select(p => p.Description));
Parallel.ForEach(characters, c => Parallel.ForEach(characters, c =>
{ {
@@ -84,6 +100,12 @@ namespace Wox.Plugin.Program
Alphabet.PinyinComination(c); Alphabet.PinyinComination(c);
} }
}); });
lock (IndexLock)
{
_win32s = w;
_uwps = u;
}
} }
public Control CreateSettingPanel() public Control CreateSettingPanel()

View File

@@ -7,6 +7,7 @@ namespace Wox.Plugin.Program
[Serializable] [Serializable]
public class ProgramIndexCache public class ProgramIndexCache
{ {
public Win32[] Programs = { }; public Win32[] Win32s = { };
public UWP.Application[] UWPs = { };
} }
} }

View File

@@ -21,6 +21,7 @@ using Rect = System.Windows.Rect;
namespace Wox.Plugin.Program.Programs namespace Wox.Plugin.Program.Programs
{ {
[Serializable]
public class UWP public class UWP
{ {
public string Name { get; } public string Name { get; }
@@ -29,17 +30,16 @@ namespace Wox.Plugin.Program.Programs
public string Location { get; set; } public string Location { get; set; }
public Application[] Apps { get; set; } public Application[] Apps { get; set; }
public Package Package { get; }
public PackageVersion Version { get; set; } public PackageVersion Version { get; set; }
public UWP(Package package) public UWP(Package package)
{ {
Package = package;
Location = Package.InstalledLocation.Path; Location = package.InstalledLocation.Path;
Name = Package.Id.Name; Name = package.Id.Name;
FullName = Package.Id.FullName; FullName = package.Id.FullName;
FamilyName = Package.Id.FamilyName; FamilyName = package.Id.FamilyName;
InitializeAppInfo(); InitializeAppInfo();
Apps = Apps.Where(a => Apps = Apps.Where(a =>
{ {
@@ -143,8 +143,6 @@ namespace Wox.Plugin.Program.Programs
public static Application[] All() public static Application[] All()
{ {
var windows10 = new Version(10, 0); var windows10 = new Version(10, 0);
var support = Environment.OSVersion.Version.Major >= windows10.Major; var support = Environment.OSVersion.Version.Major >= windows10.Major;
if (support) if (support)
@@ -211,7 +209,7 @@ namespace Wox.Plugin.Program.Programs
return FamilyName.GetHashCode(); return FamilyName.GetHashCode();
} }
[Serializable]
public class Application : IProgram public class Application : IProgram
{ {
public string AppListEntry { get; set; } public string AppListEntry { get; set; }