コード例 #1
0
ファイル: JsonSettingHelper.cs プロジェクト: mswlive/AQISet
        /// <summary>
        /// 同步缓存与文件
        /// File -> JObject -> Dictionary
        /// Dictionary -> JObject -> File
        /// </summary>
        public void Sync()
        {
            if (!File.Exists(jsonFile))
            {
                throw new FileNotFoundException(jsonFile);
            }

            JsonHelper newJsonHelper = new JsonHelper(File.ReadAllText(jsonFile));
            IDictionary <string, object> newSettingMap = newJsonHelper.ConvertNotNestedMap(jsonPropertyName);

            thisLock.EnterWriteLock();
            {
                // http://stackoverflow.com/questions/294138/merging-dictionaries-in-c-sharp
                // http://stackoverflow.com/questions/10559367/combine-multiple-dictionaries-into-a-single-dictionary
                cacheSettingDictionary = cacheSettingDictionary.Concat(newSettingMap)
                                         .GroupBy(d => d.Key)
                                         .ToDictionary(kv => kv.Key, kv => kv.First().Value);
                cacheJsonHelper.Concat(newJsonHelper);
            }
            thisLock.ExitWriteLock();

            Apply();
        }