private static ExternalAnnotationsCache TryGetCacheFromDisk()
        {
            try
            {
                if (File.Exists(CachePath))
                {
                    MessagePackSerializer <ExternalAnnotationsCache> serializer =
                        SerializationContext.Default.GetSerializer <ExternalAnnotationsCache>();
                    using (FileStream stream = File.OpenRead(CachePath))
                    {
                        using (new CodeTimer("ExternalAnnotationsCache:Read"))
                        {
                            ExternalAnnotationsCache result = serializer.Unpack(stream);

                            if (result.ExternalAnnotations.Any())
                            {
                                return(result);
                            }
                        }
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
            }
            catch (IOException)
            {
            }

            return(null);
        }
        private static void SaveToDisk([NotNull] ExternalAnnotationsCache cache)
        {
            EnsureDirectoryExists();

            MessagePackSerializer <ExternalAnnotationsCache> serializer =
                SerializationContext.Default.GetSerializer <ExternalAnnotationsCache>();

            using (FileStream stream = File.Create(CachePath))
            {
                using (new CodeTimer("ExternalAnnotationsCache:Write"))
                {
                    serializer.Pack(stream, cache);
                }
            }
        }
        private static ExternalAnnotationsMap GetCached()
        {
            ExternalAnnotationsCache cached        = TryGetCacheFromDisk();
            DateTime highestLastWriteTimeUtcOnDisk = cached != null?GetHighestLastWriteTimeUtc() : DateTime.MinValue;

            if (cached == null || cached.LastWriteTimeUtc != highestLastWriteTimeUtcOnDisk)
            {
                using (new CodeTimer("ExternalAnnotationsCache:Create"))
                {
                    cached = ScanForMemberExternalAnnotations();
                    TrySaveToDisk(cached);
                }
            }

            return(cached.ExternalAnnotations);
        }
示例#4
0
        private static void TrySaveToDisk([NotNull] ExternalAnnotationsCache cache)
        {
            try
            {
                EnsureDirectoryExists();

                using (FileStream stream = File.Create(CachePath))
                {
                    using (new CodeTimer("ExternalAnnotationsCache:Write"))
                    {
                        var service = new MsgPackSerializationService();
                        service.WriteObject(cache, stream);
                    }
                }
            }
            catch (IOException)
            {
                // When MSBuild runs in parallel, another process may be writing the cache file at the same time.
            }
        }
        private static void TrySaveToDisk([NotNull] ExternalAnnotationsCache cache)
        {
            try
            {
                EnsureDirectoryExists();

                MessagePackSerializer <ExternalAnnotationsCache> serializer =
                    SerializationContext.Default.GetSerializer <ExternalAnnotationsCache>();
                using (FileStream stream = File.Create(CachePath))
                {
                    using (new CodeTimer("ExternalAnnotationsCache:Write"))
                    {
                        serializer.Pack(stream, cache);
                    }
                }
            }
            catch (IOException)
            {
                // When MSBuild runs in parallel, another process may be writing the cache file at the same time.
            }
        }
示例#6
0
        private static ExternalAnnotationsMap GetCached()
        {
            // The lock prevents IOException (process cannot access file) when host executes analyzers in parallel.
            lock (LockObject)
            {
                ExternalAnnotationsCache cached        = TryGetCacheFromDisk();
                DateTime highestLastWriteTimeUtcOnDisk = cached != null
                    ? GetHighestLastWriteTimeUtc()
                    : DateTime.MinValue;

                if (cached == null || cached.LastWriteTimeUtc != highestLastWriteTimeUtcOnDisk)
                {
                    using (new CodeTimer("ExternalAnnotationsCache:Create"))
                    {
                        cached = ScanForMemberExternalAnnotations();
                        TrySaveToDisk(cached);
                    }
                }

                return(cached.ExternalAnnotations);
            }
        }