-
Notifications
You must be signed in to change notification settings - Fork 421
/
Cached.cs
78 lines (65 loc) · 1.98 KB
/
Cached.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Statistics;
using System;
namespace osu.Framework.Caching
{
public class Cached<T>
{
private T value = default!;
public T Value
{
get
{
if (!IsValid)
throw new InvalidOperationException($"May not query {nameof(Value)} of an invalid {nameof(Cached<T>)}.");
return value;
}
set
{
this.value = value;
IsValid = true;
FrameStatistics.Increment(StatisticsCounterType.Refreshes);
}
}
public bool IsValid { get; private set; }
public static implicit operator T(Cached<T> value) => value.Value;
/// <summary>
/// Invalidate the cache of this object.
/// </summary>
/// <returns>True if we invalidated from a valid state.</returns>
public bool Invalidate()
{
if (IsValid)
{
IsValid = false;
FrameStatistics.Increment(StatisticsCounterType.Invalidations);
return true;
}
return false;
}
}
public class Cached
{
public bool IsValid { get; private set; }
/// <summary>
/// Invalidate the cache of this object.
/// </summary>
/// <returns>True if we invalidated from a valid state.</returns>
public bool Invalidate()
{
if (IsValid)
{
IsValid = false;
FrameStatistics.Increment(StatisticsCounterType.Invalidations);
return true;
}
return false;
}
public void Validate()
{
IsValid = true;
FrameStatistics.Increment(StatisticsCounterType.Refreshes);
}
}
}