Skip to content

Commit cd695f9

Browse files
committed
Pinyin caching- Fix unsupported serialization of ConcurrentDictionary
Use Dictionary with lock instead
1 parent 879e467 commit cd695f9

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Wox.Infrastructure/Alphabet.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Alphabet : IAlphabet
2121
{
2222
private readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
2323
private ConcurrentDictionary<string, string[][]> PinyinCache;
24-
private BinaryStorage<ConcurrentDictionary<string, string[][]>> _pinyinStorage;
24+
private BinaryStorage<Dictionary<string, string[][]>> _pinyinStorage;
2525
private Settings _settings;
2626

2727
public void Initialize([NotNull] Settings settings)
@@ -36,8 +36,14 @@ private void InitializePinyinHelpers()
3636

3737
Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
3838
{
39-
_pinyinStorage = new BinaryStorage<ConcurrentDictionary<string, string[][]>>("Pinyin");
40-
PinyinCache = _pinyinStorage.TryLoad(new ConcurrentDictionary<string, string[][]>());
39+
_pinyinStorage = new BinaryStorage<Dictionary<string, string[][]>>("Pinyin");
40+
41+
lock(_pinyinStorage)
42+
{
43+
var loaded = _pinyinStorage.TryLoad(new Dictionary<string, string[][]>());
44+
45+
PinyinCache = new ConcurrentDictionary<string, string[][]>(loaded);
46+
}
4147

4248
// force pinyin library static constructor initialize
4349
PinyinHelper.toHanyuPinyinStringArray('T', Format);
@@ -79,7 +85,11 @@ public void Save()
7985
{
8086
return;
8187
}
82-
_pinyinStorage.Save(PinyinCache);
88+
89+
lock(_pinyinStorage)
90+
{
91+
_pinyinStorage.Save(PinyinCache.ToDictionary(i => i.Key, i => i.Value));
92+
}
8393
}
8494

8595
private static string[] EmptyStringArray = new string[0];

0 commit comments

Comments
 (0)