|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +// You may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System.Collections.Concurrent; |
| 16 | + |
| 17 | +namespace AwsWrapperDataProvider.Plugin.BlueGreenConnection.BlueGreenConnection; |
| 18 | + |
| 19 | +public class BlueGreenConnectionCache |
| 20 | +{ |
| 21 | + private static readonly Lazy<BlueGreenConnectionCache> InstanceValue = |
| 22 | + new Lazy<BlueGreenConnectionCache>(() => new BlueGreenConnectionCache()); |
| 23 | + |
| 24 | + private readonly ConcurrentDictionary<string, object> cache = new(); |
| 25 | + |
| 26 | + private BlueGreenConnectionCache() { } |
| 27 | + |
| 28 | + public static BlueGreenConnectionCache Instance => InstanceValue.Value; |
| 29 | + |
| 30 | + public T? Get<T>(string key) where T : class |
| 31 | + { |
| 32 | + return cache.TryGetValue(key, out var value) ? value as T : null; |
| 33 | + } |
| 34 | + |
| 35 | + public void Set<T>(string key, T value) where T : class? |
| 36 | + { |
| 37 | + cache[key] = value; |
| 38 | + } |
| 39 | + |
| 40 | + public bool TryRemove(string key) |
| 41 | + { |
| 42 | + return cache.TryRemove(key, out _); |
| 43 | + } |
| 44 | + |
| 45 | + public void Clear() |
| 46 | + { |
| 47 | + cache.Clear(); |
| 48 | + } |
| 49 | +} |
0 commit comments