tangxu
2024-04-30 26d8996e55c33186800031f7c305688035bb3182
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Threading;
 
namespace IStation.ChEr
{
    internal sealed partial class SharedMemoryCache
    {
        /// <summary>
        /// Set a cache item by key, function and optional eviction
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry to insert</param>
        /// <param name="valueFunction">A function to execute to get the data for the cache entry</param>
        /// <param name="policy">(Optional) An object that contains eviction details for the cache entry</param>
        public void Set(string key, Func<object> valueFunction, int? seconds)
        {
            if (key == null || valueFunction == null) { return; }
 
            Exception ex_func = null;
 
            this._cacheKeysBeingHandled.TryAdd(key, new CacheKeyBeingHandled());
 
            lock (this._cacheKeysBeingHandled[key].SetOperation.CounterLock)
            {
                this._cacheKeysBeingHandled[key].SetOperation.Counter++;
            }
 
            lock (this._cacheKeysBeingHandled[key].SetOperation.Lock)
            {
                if (this._cacheKeysBeingHandled[key].SetOperation.Thread != null)
                {
                    try
                    {
                        this._cacheKeysBeingHandled[key].SetOperation.Thread.Interrupt();
                    }
                    finally
                    {
                        this._cacheKeysBeingHandled[key].SetOperation.Thread = null;
                    }
                }
            }
 
            lock (_cacheKeysBeingHandled[key].SetOperation.CounterLock)
            {
                if (this._cacheKeysBeingHandled[key].SetOperation.Counter > 1)
                {
                    this._cacheKeysBeingHandled[key].SetOperation.Counter--;
                }
            }
 
            if (this._cacheKeysBeingHandled[key].SetOperation.Counter > 1)
            {
                return; // it's not the most recent thread, so ignore it
            }
 
            lock (_cacheKeysBeingHandled[key].SetOperation.Lock)
            {
                if (this._cacheKeysBeingHandled[key].SetOperation.Thread == null)
                {
                    this._cacheKeysBeingHandled[key].SetOperation.Thread = new Thread(() =>
                    {
                        try
                        {
                            object value = valueFunction();
 
                            ((IMemoryCacheDirect)this).Set(key, value, seconds);
                        }
                        catch (ThreadAbortException)
                        {
                        }
                        catch (Exception ex)
                        {
                            ex_func = ex;
                        }
                        finally
                        {
                        }
                    });
 
                    this._cacheKeysBeingHandled[key].SetOperation.Thread.Start();
                    this._cacheKeysBeingHandled[key].SetOperation.Thread.Join();
                }
            }
            if (ex_func != null)
                throw ex_func;
        }
 
        /// <summary>
        /// Set a cache item by key, value and optional eviction
        /// </summary>
        /// <param name="key">A unique identifier for the cache entry to insert</param>
        /// <param name="value">The data for the cache entry</param>
        /// <param name="policy">(Optional) An object that contains eviction details for the cache item</param>
        public void Set(string key, object value, int? seconds)
        {
            if (key == null) { return; }
 
            if (value != null)
            {
                ((IMemoryCacheDirect)this).Set(key, value, seconds);
 
                // if there's currently a function opterating on this key
                if (this._cacheKeysBeingHandled.TryGetValue(key, out CacheKeyBeingHandled cacheKeyBeingHandled))
                {
                    cacheKeyBeingHandled.SetOperation.Thread?.Interrupt();
 
                    // if it wants the same type, then cancel it and give it this value
                    if (cacheKeyBeingHandled.GetSetOperation.Type == value.GetType())
                    {
                        cacheKeyBeingHandled.GetSetOperation.Value = value;
                        cacheKeyBeingHandled.GetSetOperation.Thread.Interrupt();
                    }
                }
            }
            else
            {
                if (this._isWiping) { return; }
 
                this.Remove(key);
            }
        }
 
        /// <summary>
        /// The core method that directly sets a value in the wrapped memory cache
        /// </summary>
        /// <param name="key">key of cache item to set</param>
        /// <param name="value">The data for the cache entry</param>
        /// <param name="policy">(Optional) An object that contains eviction details for the cache entry</param>
        void IMemoryCacheDirect.Set(string key, object value, int? seconds)
        {
            var set = new Action(() =>
            {
 
                _isSetting = true;
 
                try
                {
                    if (seconds != null)
                    {
                        _memoryCache.Set(key, value, TimeSpan.FromSeconds(seconds.Value));
                    }
                    else
                    {
                        _memoryCache.Set(key, value);
                    }
                }
                finally
                {
                    _isSetting = false;
                }
 
            });
 
            if (!_isWiping)
            {
                set();
            }
            else // hold the set until wiping is complete
            {
                lock (_setLock)
                {
                    SpinWait.SpinUntil(() => !_isWiping); // one thread watching until wipe complete
 
                    set();
                }
            }
        }
    }
}