ProjectCacheService.SimpleMRUCache.cs 3.9 KB
Newer Older
H
Heejae Chang 已提交
1 2 3 4 5 6 7 8 9
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Roslyn.Utilities;

10
namespace Microsoft.CodeAnalysis.Host
H
Heejae Chang 已提交
11 12 13 14 15 16 17
{
    internal partial class ProjectCacheService : IProjectCacheHostService
    {
        private class SimpleMRUCache
        {
            private const int CacheSize = 3;

J
Jared Parsons 已提交
18
            private readonly Node[] _nodes = new Node[CacheSize];
H
Heejae Chang 已提交
19

20
            public bool IsEmpty
H
Heejae Chang 已提交
21 22 23
            {
                get
                {
J
Jared Parsons 已提交
24
                    for (var i = 0; i < _nodes.Length; i++)
H
Heejae Chang 已提交
25
                    {
J
Jared Parsons 已提交
26
                        if (_nodes[i].Data != null)
H
Heejae Chang 已提交
27 28 29 30 31 32 33 34 35 36 37 38
                        {
                            return false;
                        }
                    }

                    return true;
                }
            }

            public void Touch(object instance)
            {
                var oldIndex = -1;
39
                var oldTime = DateTime.MaxValue;
H
Heejae Chang 已提交
40

J
Jared Parsons 已提交
41
                for (var i = 0; i < _nodes.Length; i++)
H
Heejae Chang 已提交
42
                {
J
Jared Parsons 已提交
43
                    if (instance == _nodes[i].Data)
H
Heejae Chang 已提交
44
                    {
J
Jared Parsons 已提交
45
                        _nodes[i].LastTouched = DateTime.UtcNow;
H
Heejae Chang 已提交
46 47 48
                        return;
                    }

J
Jared Parsons 已提交
49
                    if (oldTime >= _nodes[i].LastTouched)
H
Heejae Chang 已提交
50
                    {
J
Jared Parsons 已提交
51
                        oldTime = _nodes[i].LastTouched;
H
Heejae Chang 已提交
52 53 54 55 56
                        oldIndex = i;
                    }
                }

                Contract.Requires(oldIndex >= 0);
J
Jared Parsons 已提交
57
                _nodes[oldIndex] = new Node(instance, DateTime.UtcNow);
H
Heejae Chang 已提交
58 59 60 61
            }

            public void ClearExpiredItems(DateTime expirationTime)
            {
J
Jared Parsons 已提交
62
                for (var i = 0; i < _nodes.Length; i++)
H
Heejae Chang 已提交
63
                {
J
Jared Parsons 已提交
64
                    if (_nodes[i].Data != null && _nodes[i].LastTouched < expirationTime)
H
Heejae Chang 已提交
65
                    {
J
Jared Parsons 已提交
66
                        _nodes[i] = default(Node);
H
Heejae Chang 已提交
67 68 69 70 71 72
                    }
                }
            }

            public void Clear()
            {
J
Jared Parsons 已提交
73
                Array.Clear(_nodes, 0, _nodes.Length);
H
Heejae Chang 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            }

            private struct Node
            {
                public readonly object Data;
                public DateTime LastTouched;

                public Node(object data, DateTime lastTouched)
                {
                    Data = data;
                    LastTouched = lastTouched;
                }
            }
        }

        private class ImplicitCacheMonitor : IdleProcessor
        {
            private readonly ProjectCacheService _owner;
            private readonly SemaphoreSlim _gate;

            public ImplicitCacheMonitor(ProjectCacheService owner, int backOffTimeSpanInMS) :
                base(AggregateAsynchronousOperationListener.CreateEmptyListener(),
                     backOffTimeSpanInMS,
                     CancellationToken.None)
            {
                _owner = owner;
                _gate = new SemaphoreSlim(0);

                Start();
            }

            protected override Task ExecuteAsync()
            {
                _owner.ClearExpiredImplicitCache(DateTime.UtcNow - TimeSpan.FromMilliseconds(BackOffTimeSpanInMS));

                return SpecializedTasks.EmptyTask;
            }

            public void Touch()
            {
                UpdateLastAccessTime();

                if (_gate.CurrentCount == 0)
                {
                    _gate.Release();
                }
            }

            protected override Task WaitAsync(CancellationToken cancellationToken)
            {
                if (_owner.IsImplicitCacheEmpty)
                {
                    return _gate.WaitAsync(cancellationToken);
                }

                return SpecializedTasks.EmptyTask;
            }
        }
    }
}