aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.BL/MemoryCacheManager.cs
blob: 6a53e302fbd11b183bf3b789bf67edbc5e863578 (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.FSE.BL
{
    /// <summary>
    /// Represents an in-memory global cache manager.
    /// </summary>
    public class MemoryCacheManager
    {
        private Dictionary<String, IMemoryCacheDictionary> _cacheDictionaries;

        private static MemoryCacheManager _instance;
        /// <summary>
        /// Gets the default singleton instance.
        /// </summary>
        public static MemoryCacheManager Default
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new MemoryCacheManager();
                }

                return _instance;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="MemoryCacheManager"/> class.
        /// </summary>
        public MemoryCacheManager()
        {
            _cacheDictionaries = new Dictionary<String, IMemoryCacheDictionary>();
        }

        /// <summary>
        /// Gets the specified <see cref="MemoryCacheDictionary{T}"/> or creates and return a new one.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public MemoryCacheDictionary<TKey, TValue> GetOrCreateCache<TKey, TValue>(String name) where TValue : class
        {
            if (_cacheDictionaries.ContainsKey(name))
            {
                return _cacheDictionaries[name] as MemoryCacheDictionary<TKey, TValue>;
            }
            else
            {
                var cacheDictionary = new MemoryCacheDictionary<TKey, TValue>(name);
                _cacheDictionaries[name] = cacheDictionary;
                return cacheDictionary;
            }
        }

        /// <summary>
        /// Gets the specified <see cref="MemoryCacheDoubleKeyDictionary{T}"/> or creates and return a new one.
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public MemoryCacheDoubleKeyDictionary<TKey1, TKey2, TValue> GetOrCreateCache<TKey1, TKey2, TValue>(String name) where TValue : class
        {
            if (_cacheDictionaries.ContainsKey(name))
            {
                return _cacheDictionaries[name] as MemoryCacheDoubleKeyDictionary<TKey1, TKey2, TValue>;
            }
            else
            {
                var cacheDictionary = new MemoryCacheDoubleKeyDictionary<TKey1, TKey2, TValue>(name);
                _cacheDictionaries[name] = cacheDictionary;
                return cacheDictionary;
            }
        }

        /// <summary>
        /// Clears all <see cref="IMemoryCacheDictionary"/> stored by this manager.
        /// </summary>
        public void ClearAll()
        {
            foreach (var cacheDictionary in _cacheDictionaries)
            {
                cacheDictionary.Value.Clear();
            }
        }
    }
}