public void Set(CacheKeyInfo keyInfo, object value)
        {
            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)
                {
                    if(!Directory.Exists(baseCacheDir + keyInfo.CacheKey.SaveDiskPath))
                    {
                        Directory.CreateDirectory(baseCacheDir + keyInfo.CacheKey.SaveDiskPath);
                    }
                    byte[] bytes = null;
                    if (!keyInfo.CacheKey.IsNativeFile)
                    {
                        bytes = TranscoderManager.Instance.Encode(keyInfo.CacheKey.Transcoder, keyInfo.CacheKey.CanCompress, value);
                    }
                    else
                    {
                        bytes = (value) as byte[];
                    }
                    string cacheFile = baseCacheDir + keyInfo.CacheKey.SaveDiskPath + "/" + realKey;
                    using (FileStream fs = new FileStream(cacheFile, FileMode.Create))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
                    cacheIndexManager.AddCacheIndex(realKey, keyInfo.CacheKey.SaveDiskPath + "/" + realKey, keyInfo.CacheKey.ExpireTime);

                    if (fileCache.ContainsKey(realKey))
                    {
                        fileCache[realKey] = bytes;
                    }
                    else
                    {
                        fileCache.Add(realKey, bytes);
                    }

                }
                else
                {
                    if(localCache.ContainsKey(realKey))
                    {
                        localCache[realKey] = value;
                    }
                    else
                    {
                        localCache.Add(realKey, value);
                    }
                }
            }
        }
Exemple #2
0
        public void Set(CacheKeyInfo keyInfo, object value)
        {
            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)
                {
                    if (!Directory.Exists(baseCacheDir + keyInfo.CacheKey.SaveDiskPath))
                    {
                        Directory.CreateDirectory(baseCacheDir + keyInfo.CacheKey.SaveDiskPath);
                    }
                    byte[] bytes = null;
                    if (!keyInfo.CacheKey.IsNativeFile)
                    {
                        bytes = TranscoderManager.Instance.Encode(keyInfo.CacheKey.Transcoder, keyInfo.CacheKey.CanCompress, value);
                    }
                    else
                    {
                        bytes = (value) as byte[];
                    }
                    string cacheFile = baseCacheDir + keyInfo.CacheKey.SaveDiskPath + "/" + realKey;
                    using (FileStream fs = new FileStream(cacheFile, FileMode.Create))
                    {
                        fs.Write(bytes, 0, bytes.Length);
                    }
                    cacheIndexManager.AddCacheIndex(realKey, keyInfo.CacheKey.SaveDiskPath + "/" + realKey, keyInfo.CacheKey.ExpireTime);

                    if (fileCache.ContainsKey(realKey))
                    {
                        fileCache[realKey] = bytes;
                    }
                    else
                    {
                        fileCache.Add(realKey, bytes);
                    }
                }
                else
                {
                    if (localCache.ContainsKey(realKey))
                    {
                        localCache[realKey] = value;
                    }
                    else
                    {
                        localCache.Add(realKey, value);
                    }
                }
            }
        }
Exemple #3
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));
                }
            }
        }
Exemple #4
0
 public void Del(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)
         {
             string cacheFile = baseCacheDir + keyInfo.CacheKey.SaveDiskPath + "/" + realKey;
             if (File.Exists(cacheFile))
             {
                 File.Delete(cacheFile);
             }
             cacheIndexManager.RemoveCacheIndex(cacheFile);
             if (fileCache.ContainsKey(realKey))
             {
                 fileCache.Remove(realKey);
             }
         }
         else
         {
             if (localCache.ContainsKey(realKey))
             {
                 localCache.Remove(realKey);
             }
         }
     }
 }
 public void Del(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)
         {
             string cacheFile = baseCacheDir + keyInfo.CacheKey.SaveDiskPath + "/" + realKey;
             if (File.Exists(cacheFile))
             {
                 File.Delete(cacheFile);
             }
             cacheIndexManager.RemoveCacheIndex(cacheFile);
             if (fileCache.ContainsKey(realKey))
             {
                 fileCache.Remove(realKey);
             }
         }
         else
         {
             if (localCache.ContainsKey(realKey))
             {
                 localCache.Remove(realKey);
             }
         }
     }
 }
        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);
                }
            }
        }
        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];
                }
            }
        }
Exemple #8
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]);
                }
            }
        }