Ejemplo n.º 1
0
        public bool GetKeyExist(CacheKeyInfo keyInfo)
        {
            lock (this)
            {
                string realKey = string.Empty;
                if (keyInfo.CacheKey.IsNativeFile)
                {
                    realKey = keyInfo.GetRealKey();
                }
                else
                {
                    realKey = keyInfo.GetRealKey() + ".cache";
                }

                bool saveDisk = keyInfo.CacheKey.SaveDiskPath != null;
                if (saveDisk)
                {
                    CacheIndex cacheIndex = cacheIndexManager.GetCacheIndex(realKey);
                    if (cacheIndex == null)
                    {
                        return(false);
                    }

                    string filePath = baseCacheDir + cacheIndex.CacheFilePath;
                    if (cacheIndex.ExpireTime > 0)
                    {
                        int now = (int)(TimeManager.Instance.Now / 1000);
                        //过期
                        if (now - cacheIndex.WriteTime >= cacheIndex.ExpireTime)
                        {
                            FileUtil.DeleteFile(filePath);
                            cacheIndexManager.RemoveCacheIndex(realKey);
                            return(false);
                        }
                    }

                    if (fileCache.ContainsKey(realKey))
                    {
                        return(true);
                    }

                    if (!File.Exists(filePath))
                    {
                        FileUtil.DeleteFile(filePath);
                        cacheIndexManager.RemoveCacheIndex(realKey);
                        return(false);
                    }
                    return(true);
                }
                else
                {
                    return(localCache.ContainsKey(realKey));
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 加载索引文件
 /// </summary>
 public void LoadCacheIndex()
 {
     ByteBuffer buffer = FileUtil.ReadFileToByteArray(cacheIndexFilePath);
     if(buffer != null)
     {
         while(buffer.ReadableBytes() > 0)
         {
             CacheIndex cacheIndex = new CacheIndex();
             cacheIndex.Key = buffer.ReadString();
             cacheIndex.WriteTime = buffer.ReadInt();
             cacheIndex.ExpireTime = buffer.ReadInt();
             cacheIndex.CacheFilePath = buffer.ReadString();
             cacheIndexMap.Add(cacheIndex.Key, cacheIndex);
         }
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 保存索引对象到磁盘
 /// </summary>
 public void Flush()
 {
     lock (this)
     {
         ByteBuffer buffer = ByteBuffer.Allocate(cacheIndexMap.Count() * 30);
         foreach (KeyValuePair <string, CacheIndex> kv in cacheIndexMap)
         {
             CacheIndex cacheIndex = kv.Value;
             buffer.WriteString(cacheIndex.Key);
             buffer.WriteInt(cacheIndex.WriteTime);
             buffer.WriteInt(cacheIndex.ExpireTime);
             buffer.WriteString(cacheIndex.CacheFilePath);
         }
         FileUtil.WriteFile(cacheIndexFilePath, buffer.ToArray());
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 加载索引文件
        /// </summary>
        public void LoadCacheIndex()
        {
            ByteBuffer buffer = FileUtil.ReadFileToByteArray(cacheIndexFilePath);

            if (buffer != null)
            {
                while (buffer.ReadableBytes() > 0)
                {
                    CacheIndex cacheIndex = new CacheIndex();
                    cacheIndex.Key           = buffer.ReadString();
                    cacheIndex.WriteTime     = buffer.ReadInt();
                    cacheIndex.ExpireTime    = buffer.ReadInt();
                    cacheIndex.CacheFilePath = buffer.ReadString();
                    cacheIndexMap.Add(cacheIndex.Key, cacheIndex);
                }
            }
        }
Ejemplo n.º 5
0
        public void AddCacheIndex(string key, string cacheFilePath, int expireTime)
        {
            lock (this)
            {
                if (cacheIndexMap.ContainsKey(key))
                {
                    CacheIndex cacheIndex = cacheIndexMap[key];
                    cacheIndex.ExpireTime = expireTime;
                    cacheIndex.WriteTime = (int)(TimeManager.Instance.Now / 1000);
                }
                else
                {
                    CacheIndex cacheIndex = new CacheIndex();
                    cacheIndex.Key = key;
                    cacheIndex.WriteTime = (int)(TimeManager.Instance.Now / 1000);
                    cacheIndex.ExpireTime = expireTime;
                    cacheIndex.CacheFilePath = cacheFilePath;
                    cacheIndexMap.Add(key, cacheIndex);
                }

                Flush();
            }
        }
Ejemplo n.º 6
0
        public void AddCacheIndex(string key, string cacheFilePath, int expireTime)
        {
            lock (this)
            {
                if (cacheIndexMap.ContainsKey(key))
                {
                    CacheIndex cacheIndex = cacheIndexMap[key];
                    cacheIndex.ExpireTime = expireTime;
                    cacheIndex.WriteTime  = (int)(TimeManager.Instance.Now / 1000);
                }
                else
                {
                    CacheIndex cacheIndex = new CacheIndex();
                    cacheIndex.Key           = key;
                    cacheIndex.WriteTime     = (int)(TimeManager.Instance.Now / 1000);
                    cacheIndex.ExpireTime    = expireTime;
                    cacheIndex.CacheFilePath = cacheFilePath;
                    cacheIndexMap.Add(key, cacheIndex);
                }

                Flush();
            }
        }
Ejemplo n.º 7
0
        public object Get(CacheKeyInfo keyInfo)
        {
            lock (this)
            {
                string realKey = string.Empty;
                if (keyInfo.CacheKey.IsNativeFile)
                {
                    realKey = keyInfo.GetRealKey();
                }
                else
                {
                    realKey = keyInfo.GetRealKey() + ".cache";
                }

                bool saveDisk = keyInfo.CacheKey.SaveDiskPath != null;
                if (saveDisk)
                {
                    CacheIndex cacheIndex = cacheIndexManager.GetCacheIndex(realKey);
                    if (cacheIndex == null)
                    {
                        return(null);
                    }

                    string filePath = baseCacheDir + cacheIndex.CacheFilePath;
                    if (cacheIndex.ExpireTime > 0)
                    {
                        int now = (int)(TimeManager.Instance.Now / 1000);
                        //过期
                        if (now - cacheIndex.WriteTime >= cacheIndex.ExpireTime)
                        {
                            FileUtil.DeleteFile(filePath);
                            cacheIndexManager.RemoveCacheIndex(realKey);
                            return(null);
                        }
                    }

                    if (!fileCache.ContainsKey(realKey))
                    {
                        if (!File.Exists(filePath))
                        {
                            FileUtil.DeleteFile(filePath);
                            cacheIndexManager.RemoveCacheIndex(realKey);
                            return(null);
                        }

                        ByteBuffer buffer = FileUtil.ReadFileToByteArray(filePath);
                        if (buffer == null)
                        {
                            FileUtil.DeleteFile(filePath);
                            cacheIndexManager.RemoveCacheIndex(realKey);
                            return(null);
                        }
                        fileCache.Add(realKey, buffer.ToArray());
                        buffer.ResetReaderIndex();
                    }
                    return(TranscoderManager.Instance.Decode(keyInfo.CacheKey.Transcoder, fileCache[realKey], keyInfo.CacheKey.IsNativeFile));
                }
                else
                {
                    if (!localCache.ContainsKey(realKey))
                    {
                        return(null);
                    }
                    return(localCache[realKey]);
                }
            }
        }