Beispiel #1
0
        /// <summary>
        /// Cập nhật Cache Hashtable cho các bảng dữ liệu danh mục
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="actionLoad"></param>
        public void LoadCacheHastable(CacheHashtable obj, Func <Hashtable> actionLoad)
        {
            try
            {
                // Check cache version (cacheVersion = 0: Refresh Cache)
                long cacheVersion = this.GetCacheValue(obj.KeyVersion, (long)0);

                if (obj.CacheVersion < cacheVersion || cacheVersion == 0)
                {
                    Hashtable h = null;

                    if (cacheVersion != 0)
                    {
                        h = h.GetCache(obj.Key); // load hashtable from cache
                    }

                    if (h == null)
                    {
                        // load from db
                        h = actionLoad();

                        cacheVersion = DateTime.Now.Ticks;
                        h.MemoryCache(obj.Key, CacheExpiration.OneMonth);
                        cacheVersion.MemoryCache(obj.KeyVersion, CacheExpiration.OneMonth);
                    }
                    obj.CacheVersion = cacheVersion;

                    System.Threading.Interlocked.Exchange(ref obj.CacheClient, Hashtable.Synchronized(h));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }