예제 #1
0
        public async Task <string> getCachedMetadataAsync(Uri target, string id, CalcMetadataCallback computer)
        {
            // ensure the object is up to date
            var data = await getCachedObjectAsync(target);

            var metafn = data.Name + ".meta." + id;

            string result;

            if (finished_metadata.TryGetValue(metafn, out result))
            {
                return(result);
            }

            Task <string> pending;

            if (!pending_metadata.TryGetValue(metafn, out pending))
            {
                pending = getMetadataToMemory(data, metafn, computer);

                if (!pending.IsCompleted)
                {
                    pending_metadata.Add(metafn, pending);
                }
            }

            return(await pending);
        }
예제 #2
0
        private async Task <string> getMetadataToMemory(StorageFile data, string metafn, CalcMetadataCallback computer)
        {
            try
            {
                string result;

                try
                {
                    var file = await cache_directory.GetFileAsync(metafn);

                    result = await FileIO.ReadTextAsync(file, Windows.Storage.Streams.UnicodeEncoding.Utf8);

                    return(finished_metadata[metafn] = result);
                }
                catch (FileNotFoundException) { }

                result = finished_metadata[metafn] = await computer(data);

                StorageFile newfile = await cache_directory.CreateFileAsync(metafn, CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(newfile, result, Windows.Storage.Streams.UnicodeEncoding.Utf8);

                return(result);
            }
            finally
            {
                pending_metadata.Remove(metafn);
            }
        }