public static bool HasExpired(CachedDataTableIdentifier id)
 {
     if (_updateHist.ContainsKey(id))
     {
         CacheDataTableTimeoutSpec tmSpec = _updateHist[id];
         TimeSpan ts = DateTime.Now.Subtract(tmSpec.RetrievedOn);
         return(ts.Minutes > tmSpec.Timeout);
     }
     else
     {
         return(false);
     }
 }
        public static void ApplyTimeoutChange(int newTimeout)
        {
            lock (_padlock)
            {
                CacheDataTableTimeoutSpec         tmSpec;
                IList <CachedDataTableIdentifier> removeList = new List <CachedDataTableIdentifier>();
                IList <CachedDataTableIdentifier> updateList = new List <CachedDataTableIdentifier>();
                foreach (CachedDataTableIdentifier id in _updateHist.Keys)
                {
                    if (newTimeout <= 0)
                    {
                        removeList.Add(id);
                    }
                    else
                    {
                        tmSpec = _updateHist[id];
                        TimeSpan ts = DateTime.Now.Subtract(tmSpec.RetrievedOn);
                        if (tmSpec.Timeout - ts.TotalSeconds > newTimeout)
                        {
                            removeList.Add(id);
                            updateList.Add(id);
                        }
                    }
                }

                foreach (CachedDataTableIdentifier id in removeList)
                {
                    if (_updateHist.ContainsKey(id))
                    {
                        _updateHist.Remove(id);
                    }

                    if (updateList.Contains(id))
                    {
                        CachedDataTableIdentifier newId = new CachedDataTableIdentifier();
                        newId.CopyFrom(id);
                        newId.Timeout      = newTimeout;
                        tmSpec             = new CacheDataTableTimeoutSpec();
                        tmSpec.RetrievedOn = DateTime.Now;
                        tmSpec.Timeout     = newTimeout;
                        _updateHist.Add(newId, tmSpec);
                    }
                }
            }
        }
        public static DataTable GetData(SqlConnection conn, CachedDataTableIdentifier id)
        {
            lock (_padlock)
            {
                bool isOutdated = false;
                if (_updateHist.ContainsKey(id))
                {
                    CacheDataTableTimeoutSpec tmSpec = _updateHist[id];
                    TimeSpan ts = DateTime.Now.Subtract(tmSpec.RetrievedOn);

                    isOutdated = ts.TotalSeconds > tmSpec.Timeout;
                }

                if (!_cache.ContainsKey(id) || isOutdated)
                {
                    StoreData(conn, id);
                }

                DataTable result = _cache[id];
                return(result != null?result.Copy() : null);
            }
        }
        public static void StoreData(SqlConnection conn, CachedDataTableIdentifier id)
        {
            lock (_padlock)
            {
                if (id.DataType == CachedDataType.None)
                {
                    throw new Exception("CachedDataType is None!");
                }

                if (_cache.ContainsKey(id))
                {
                    DataTable rm = _cache[id];
                    _cache.Remove(id);
                    if (rm != null)
                    {
                        rm.Clear();
                        rm.Dispose();
                    }
                }

                if (_updateHist.ContainsKey(id))
                {
                    _updateHist.Remove(id);
                }

                string script = String.Empty;
                switch (id.DataType)
                {
                case CachedDataType.Databases:
                    script = ResManager.GetDBScript("Script_GetDatabases");
                    break;

                case CachedDataType.Users:
                    script = ResManager.GetDBScript("Script_GetUsers");
                    break;

                case CachedDataType.Objects:
                    script = ResManager.GetDBScript("Script_CodeCompletionProposalWithoutProcParams");
                    break;

                default:
                    break;
                }

                SqlCommand     cmd     = null;
                SqlDataAdapter adapter = null;
                DataTable      tbl     = null;

                try
                {
                    cmd = new SqlCommand(script, conn);
                    cmd.CommandTimeout    = 0;
                    adapter               = new SqlDataAdapter();
                    adapter.SelectCommand = cmd;
                    tbl = new DataTable();
                    adapter.Fill(tbl);
                }
                finally
                {
                    if (cmd != null)
                    {
                        cmd.Dispose();
                    }
                    if (adapter != null)
                    {
                        adapter.Dispose();
                    }
                }

                CachedDataTableIdentifier key = new CachedDataTableIdentifier();
                key.CopyFrom(id);
                if (key.Timeout > 0)
                {
                    CacheDataTableTimeoutSpec tmSpec = new CacheDataTableTimeoutSpec();
                    tmSpec.Timeout     = key.Timeout;
                    tmSpec.RetrievedOn = DateTime.Now;
                    _updateHist.Add(key, tmSpec);
                }
                _cache.Add(key, tbl);
            }
        }