)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » (Ia.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

Cache implementation of IMemoryCache using a dictionary to store its entries

    1: using Microsoft.Extensions.Caching.Memory;
    2: using Microsoft.Extensions.Configuration;
    3:  
    4: namespace Ia.Cl.Models
    5: {
    6:     ////////////////////////////////////////////////////////////////////////////
    7:  
    8:     /// <summary publish="true">
    9:     /// Cache implementation of IMemoryCache using a dictionary to store its entries
   10:     /// </summary>
   11:     /// <remarks> 
   12:     /// Copyright � 2019-2024 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   13:     ///
   14:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   15:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   16:     ///
   17:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   18:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   19:     /// 
   20:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   21:     /// 
   22:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   23:     /// </remarks> 
   24:     public sealed class Cache
   25:     {
   26:         private static volatile Cache instance;
   27:         private static MemoryCache memoryCache;
   28:  
   29:         private static readonly object objectLock = new object();
   30:  
   31:         ////////////////////////////////////////////////////////////////////////////
   32:  
   33:         /// <summary>
   34:         ///
   35:         /// </summary>
   36:         private Cache() { }
   37:  
   38:         ////////////////////////////////////////////////////////////////////////////
   39:  
   40:         /// <summary>
   41:         ///
   42:         /// </summary>
   43:         public static Cache Instance
   44:         {
   45:             get
   46:             {
   47:                 if (instance == null)
   48:                 {
   49:                     lock (objectLock)
   50:                     {
   51:                         if (instance == null)
   52:                         {
   53:                             instance = new Cache();
   54:  
   55:                             memoryCache = new MemoryCache(new MemoryCacheOptions());
   56:                         }
   57:                     }
   58:                 }
   59:  
   60:                 return instance;
   61:             }
   62:         }
   63:  
   64:         ////////////////////////////////////////////////////////////////////////////
   65:  
   66:         /// <summary>
   67:         ///
   68:         /// </summary>
   69:         public void Set(string key, object value)
   70:         {
   71:             memoryCache.Set(key, value);
   72:         }
   73:  
   74:         ////////////////////////////////////////////////////////////////////////////
   75:  
   76:         /// <summary>
   77:         ///
   78:         /// </summary>
   79:         public void Set<T>(string key, T value)
   80:         {
   81:             memoryCache.Set<T>(key, value);
   82:         }
   83:  
   84:         ////////////////////////////////////////////////////////////////////////////
   85:  
   86:         /// <summary>
   87:         ///
   88:         /// </summary>
   89:         public object Get(string key)
   90:         {
   91:             return memoryCache.Get(key);
   92:         }
   93:  
   94:         ////////////////////////////////////////////////////////////////////////////
   95:  
   96:         /// <summary>
   97:         ///
   98:         /// </summary>
   99:         public T Get<T>(string key)
  100:         {
  101:             return memoryCache.Get<T>(key);
  102:         }
  103:  
  104:         ////////////////////////////////////////////////////////////////////////////
  105:  
  106:         /// <summary>
  107:         ///
  108:         /// </summary>
  109:         public int Count
  110:         {
  111:             get
  112:             {
  113:                 return memoryCache.Count;
  114:             }
  115:         }
  116:  
  117:         ////////////////////////////////////////////////////////////////////////////
  118:  
  119:         /// <summary>
  120:         ///
  121:         /// </summary>
  122:         public void Clear()
  123:         {
  124:             memoryCache.Clear();
  125:         }
  126:  
  127:         ////////////////////////////////////////////////////////////////////////////
  128:  
  129:         /// <summary>
  130:         ///
  131:         /// </summary>
  132:         public ICacheEntry CreateEntry(object key)
  133:         {
  134:             return memoryCache.CreateEntry(key);
  135:         }
  136:  
  137:         ////////////////////////////////////////////////////////////////////////////
  138:  
  139:         /// <summary>
  140:         ///
  141:         /// </summary>
  142:         public void Remove(object key)
  143:         {
  144:             memoryCache.Remove(key);
  145:         }
  146:  
  147:         ////////////////////////////////////////////////////////////////////////////
  148:  
  149:         /// <summary>
  150:         ///
  151:         /// </summary>
  152:         public MemoryCacheStatistics GetCurrentStatistics()
  153:         {
  154:             return memoryCache.GetCurrentStatistics();
  155:         }
  156:  
  157:         ////////////////////////////////////////////////////////////////////////////
  158:  
  159:         /// <summary>
  160:         ///
  161:         /// </summary>
  162:         public bool Contains(string key)
  163:         {
  164:             bool b;
  165:  
  166:             if (memoryCache.TryGetValue(key, out object result))
  167:             {
  168:                 b = true;
  169:             }
  170:             else b = false;
  171:  
  172:             return b;
  173:         }
  174:  
  175:         ////////////////////////////////////////////////////////////////////////////
  176:  
  177:         /// <summary>
  178:         ///
  179:         /// </summary>
  180:         public bool TryGetValue(string key, out object result)
  181:         {
  182:             return memoryCache.TryGetValue(key, out result);
  183:         }
  184:  
  185:         ////////////////////////////////////////////////////////////////////////////
  186:  
  187:         /// <summary>
  188:         ///
  189:         /// </summary>
  190:         public bool TryGetValue<T>(string key, out T result)
  191:         {
  192:             return memoryCache.TryGetValue<T>(key, out result);
  193:         }
  194:  
  195:         ////////////////////////////////////////////////////////////////////////////
  196:         ////////////////////////////////////////////////////////////////////////////
  197:     }
  198:  
  199:     ////////////////////////////////////////////////////////////////////////////
  200:     ////////////////////////////////////////////////////////////////////////////
  201: }