Skip to content

Commit ab51e3c

Browse files
committed
Part of b1221dc.
1 parent 3838a71 commit ab51e3c

File tree

1 file changed

+40
-57
lines changed

1 file changed

+40
-57
lines changed

src/SilDev/Investment/CacheInvestor.cs

Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
// ==============================================
66
//
77
// Filename: CacheInvestor.cs
8-
// Version: 2020-01-28 20:16
8+
// Version: 2023-12-18 23:47
99
//
10-
// Copyright (c) 2020, Si13n7 Developments(tm)
10+
// Copyright (c) 2023, Si13n7 Developments(tm)
1111
// All rights reserved.
1212
// ______________________________________________
1313

@@ -16,28 +16,14 @@
1616
namespace SilDev.Investment
1717
{
1818
using System;
19-
using System.Collections.Generic;
19+
using System.Collections.Concurrent;
2020
using System.Threading;
2121

2222
/// <summary>
2323
/// Provides simple global caching of elements.
2424
/// </summary>
2525
public static class CacheInvestor
2626
{
27-
private static volatile object _syncObject;
28-
29-
private static object SyncObject
30-
{
31-
get
32-
{
33-
if (_syncObject != null)
34-
return _syncObject;
35-
var obj = new object();
36-
Interlocked.CompareExchange<object>(ref _syncObject, obj, null);
37-
return _syncObject;
38-
}
39-
}
40-
4127
/// <summary>
4228
/// Gets the default reference identifier of the provided type.
4329
/// </summary>
@@ -60,15 +46,12 @@ public static int GetId<TElement>()
6046
/// <param name="id">
6147
/// The reference identifier.
6248
/// </param>
63-
public static void AddDefault<TElement>(int id) where TElement : new()
49+
public static bool AddDefault<TElement>(int id) where TElement : new()
6450
{
6551
if (CacheProvider<TElement>.Storage.ContainsKey(id))
66-
return;
67-
lock (SyncObject)
68-
{
69-
var item = new TElement();
70-
CacheProvider<TElement>.Storage.Add(id, item);
71-
}
52+
return true;
53+
var item = new TElement();
54+
return CacheProvider<TElement>.Storage.TryAdd(id, item);
7255
}
7356

7457
/// <summary>
@@ -77,7 +60,7 @@ public static int GetId<TElement>()
7760
/// <typeparam name="TElement">
7861
/// The type of the element.
7962
/// </typeparam>
80-
public static void AddDefault<TElement>() where TElement : new() =>
63+
public static bool AddDefault<TElement>() where TElement : new() =>
8164
AddDefault<TElement>(GetId<TElement>());
8265

8366
/// <summary>
@@ -92,13 +75,8 @@ public static int GetId<TElement>()
9275
/// <param name="element">
9376
/// The element to be added.
9477
/// </param>
95-
public static void AddItem<TElement>(int id, TElement element)
96-
{
97-
if (CacheProvider<TElement>.Storage.ContainsKey(id))
98-
return;
99-
lock (SyncObject)
100-
CacheProvider<TElement>.Storage.Add(id, element);
101-
}
78+
public static bool AddItem<TElement>(int id, TElement element) =>
79+
CacheProvider<TElement>.Storage.ContainsKey(id) || CacheProvider<TElement>.Storage.TryAdd(id, element);
10280

10381
/// <summary>
10482
/// Adds the specified element under the default identifier of the provided
@@ -110,7 +88,7 @@ public static void AddItem<TElement>(int id, TElement element)
11088
/// <param name="element">
11189
/// The value to be added.
11290
/// </param>
113-
public static void AddItem<TElement>(TElement element) =>
91+
public static bool AddItem<TElement>(TElement element) =>
11492
AddItem(GetId<TElement>(), element);
11593

11694
/// <summary>
@@ -125,13 +103,10 @@ public static void AddItem<TElement>(TElement element) =>
125103
/// <param name="element">
126104
/// The element to be added or updated.
127105
/// </param>
128-
public static void AddOrUpdate<TElement>(int id, TElement element)
106+
public static bool AddOrUpdate<TElement>(int id, TElement element)
129107
{
130-
lock (SyncObject)
131-
{
132-
RemoveItem<TElement>(id);
133-
CacheProvider<TElement>.Storage.Add(id, element);
134-
}
108+
RemoveItem<TElement>(id);
109+
return CacheProvider<TElement>.Storage.TryAdd(id, element);
135110
}
136111

137112
/// <summary>
@@ -148,7 +123,7 @@ public static void AddOrUpdate<TElement>(TElement element) =>
148123
AddOrUpdate(GetId<TElement>(), element);
149124

150125
/// <summary>
151-
/// Updates an element under the specified identifier.
126+
/// Updates an existing element under the specified identifier.
152127
/// </summary>
153128
/// <typeparam name="TElement">
154129
/// The type of the element.
@@ -159,28 +134,27 @@ public static void AddOrUpdate<TElement>(TElement element) =>
159134
/// <param name="element">
160135
/// The element to be updated.
161136
/// </param>
162-
public static void Update<TElement>(int id, TElement element)
137+
public static bool Update<TElement>(int id, TElement element)
163138
{
164139
if (!CacheProvider<TElement>.Storage.ContainsKey(id))
165-
return;
166-
lock (SyncObject)
167-
{
168-
if (CacheProvider<TElement>.Storage[id] is IDisposable disposable)
169-
disposable.Dispose();
170-
CacheProvider<TElement>.Storage[id] = element;
171-
}
140+
return false;
141+
if (CacheProvider<TElement>.Storage[id] is IDisposable disposable)
142+
disposable.Dispose();
143+
CacheProvider<TElement>.Storage[id] = element;
144+
return true;
172145
}
173146

174147
/// <summary>
175-
/// Updates an element under the default identifier of the provided type.
148+
/// Updates an existing element under the default identifier of the provided
149+
/// type.
176150
/// </summary>
177151
/// <typeparam name="TElement">
178152
/// The type of the element.
179153
/// </typeparam>
180154
/// <param name="element">
181155
/// The element to be updated.
182156
/// </param>
183-
public static void Update<TElement>(TElement element) where TElement : new() =>
157+
public static bool Update<TElement>(TElement element) where TElement : new() =>
184158
Update(GetId<TElement>(), element);
185159

186160
/// <summary>
@@ -268,12 +242,9 @@ public static void RemoveItem<TElement>(int id)
268242
{
269243
if (!CacheProvider<TElement>.Storage.ContainsKey(id))
270244
return;
271-
lock (SyncObject)
272-
{
273-
var item = (object)CacheProvider<TElement>.Storage[id];
274-
CacheProvider<TElement>.Storage.Remove(id);
275-
CurrentMemory.Destroy(ref item);
276-
}
245+
var item = (object)CacheProvider<TElement>.Storage[id];
246+
CacheProvider<TElement>.Storage.TryRemove(id, out _);
247+
CurrentMemory.Destroy(ref item);
277248
}
278249

279250
/// <summary>
@@ -288,7 +259,19 @@ public static void RemoveItem<TElement>() =>
288259

289260
private static class CacheProvider<T>
290261
{
291-
internal static volatile IDictionary<int, T> Storage = new Dictionary<int, T>();
262+
private static ConcurrentDictionary<int, T> _storage;
263+
264+
internal static ConcurrentDictionary<int, T> Storage
265+
{
266+
get
267+
{
268+
if (_storage != null)
269+
return _storage;
270+
var storage = new ConcurrentDictionary<int, T>();
271+
Interlocked.CompareExchange(ref _storage, storage, null);
272+
return _storage;
273+
}
274+
}
292275
}
293276
}
294277
}

0 commit comments

Comments
 (0)