コード例 #1
0
        public async Task <dynamic> GetAllKnownWidgetZonesAsync()
        {
            var fileName = "widgetzones.json";
            var fs       = _appContext.AppDataRoot;
            var cacheKey = CommonHelper.BuildScopedKey(fileName);

            var rawJson = await _memoryCache.GetOrCreateAsync(cacheKey, async entry =>
            {
                if (fs.FileExists(fileName))
                {
                    entry.ExpirationTokens.Add(fs.Watch(fileName));
                    return(await fs.ReadAllTextAsync(fileName));
                }
                else
                {
                    return(string.Empty);
                }
            });

            if (rawJson is string json && json.HasValue())
            {
                try
                {
                    return(JObject.Parse(json));
                }
                catch
                {
                    // Json is invalid. Don't parse again.
                    _memoryCache.Set(cacheKey, string.Empty);
                }
            }

            return(null);
        }