コード例 #1
0
        /// <summary>
        /// 返回所有脱敏词汇
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <string> > GetWordsAsync()
        {
            // 读取缓存数据
            var wordsCached = await _distributedCache.GetStringAsync(DISTRIBUTED_KEY);

            if (wordsCached != null)
            {
                return(_jsonSerializerProvider.Deserialize <IEnumerable <string> >(wordsCached));
            }

            // 读取文件内容
            byte[] buffer;
            using (var readStream = _fileProvider.GetFileInfo("sensitive-words.txt").CreateReadStream()) // 暂时不提供配置文件名称
            {
                buffer = new byte[readStream.Length];
                await readStream.ReadAsync(buffer.AsMemory(0, buffer.Length));
            }

            var content = Encoding.UTF8.GetString(buffer);

            // 取换行符分割字符串
            var words = content.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            // 缓存数据
            await _distributedCache.SetStringAsync(DISTRIBUTED_KEY, _jsonSerializerProvider.Serialize(words));

            return(words);
        }
コード例 #2
0
 public static T Deserialize <T>(string jsonString)
 {
     CheckJsonProviderNotNull();
     return(staticJsonSerializerProvider.Deserialize <T>(jsonString));
 }