Example #1
0
        public void Insert(string key, QueryCacheDocuments item, DateTime expireTime, QueryCacheInformation cacheInfo)
        {
            if (Monitor.TryEnter(_LockObj, Timeout))
            {
                try
                {
                    base.Insert(key, item, expireTime, cacheInfo);

                    if (CacheFileEnabled)
                    {
                        WriteCacheFile(new CacheFile(key, item, cacheInfo));
                    }
                }
                finally
                {
                    Monitor.Exit(_LockObj);
                }
            }
        }
Example #2
0
        public bool TryGetValue(string key, out QueryCacheDocuments value, out DateTime expireTime, out int hitCount, out QueryCacheInformation cacheInfo)
        {
            if (Monitor.TryEnter(_LockObj, Timeout))
            {
                try
                {
                    if (CacheFileEnabled)
                    {
                        string bit16String = base.GetMD5String(key);

                        object tag;
                        bool   result = base.TryGetValue(key, out value, out expireTime, out hitCount, out tag);

                        string filePath = _CacheFileFolder + bit16String + ".xml";

                        if (!result)
                        {
                            if (System.IO.File.Exists(filePath))
                            {
                                using (System.IO.FileStream fs = new System.IO.FileStream(filePath,
                                                                                          System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))
                                {
                                    CacheFile cacheFile;

                                    try
                                    {
                                        cacheFile = Hubble.Framework.Serialization.XmlSerialization <CacheFile> .Deserialize(fs);
                                    }
                                    catch
                                    {
                                        cacheInfo = null;
                                        return(false);
                                    }

                                    value      = cacheFile.Documents;
                                    cacheInfo  = cacheFile.Info;
                                    expireTime = new DateTime();
                                    hitCount   = 1;
                                    base.Insert(key, value, expireTime, cacheInfo);
                                }

                                System.IO.File.SetLastWriteTime(filePath, DateTime.Now);
                                return(true);
                            }
                            else
                            {
                                cacheInfo = null;
                                return(false);
                            }
                        }
                        else
                        {
                            cacheInfo = tag as QueryCacheInformation;

                            try
                            {
                                if (!System.IO.File.Exists(filePath))
                                {
                                    WriteCacheFile(new CacheFile(key, value, cacheInfo));
                                }
                                else
                                {
                                    SetFileLastWriteTime(filePath);
                                }
                            }
                            catch
                            {
                            }

                            return(true);
                        }
                    }
                    else
                    {
                        object tag;
                        bool   result = base.TryGetValue(key, out value, out expireTime, out hitCount, out tag);
                        cacheInfo = tag as QueryCacheInformation;
                        return(result);
                    }
                }
                finally
                {
                    Monitor.Exit(_LockObj);
                }
            }
            else
            {
                value      = null;
                expireTime = default(DateTime);
                hitCount   = 0;
                cacheInfo  = null;
                return(false);
            }
        }
Example #3
0
 public CacheFile(string key, QueryCacheDocuments documents, QueryCacheInformation info)
 {
     Key       = key;
     Documents = documents;
     Info      = info;
 }