/// <summary>
 /// Salva o estado.
 /// </summary>
 void IPersistentCacheStorage.SaveStorageState()
 {
     try
     {
         if (_stateChangeId != 0)
         {
             string path = Path.Combine(_internalStore.RootDir, "__cfs__.state");
             lock (_itemDict)
             {
                 using (Stream stream = new FileStream(path, FileMode.Create))
                 {
                     var writer = new BinaryWriter(stream);
                     writer.Write(_itemDict.Count);
                     IDictionaryEnumerator enumerator = _itemDict.GetEnumerator();
                     while (enumerator.MoveNext())
                     {
                         try
                         {
                             byte[] buffer = StoreItem.ToBinary(enumerator.Key, enumerator.Value, base.CacheContext);
                             writer.Write(buffer.Length);
                             writer.Write(buffer);
                             continue;
                         }
                         catch (Exception)
                         {
                             writer.Write(-1);
                             continue;
                         }
                     }
                     writer.Flush();
                 }
             }
         }
     }
     catch (Exception exception)
     {
         Colosoft.Logging.Trace.Error("FileSystemStorageProvider.SaveStorageState()".GetFormatter(), exception.GetFormatter());
     }
     finally
     {
         this.ResetStateChanged();
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Adiciona um novo item.
 /// </summary>
 /// <param name="key">Chave do item.</param>
 /// <param name="item">Instancia do item.</param>
 /// <returns>Resultado da operação.</returns>
 public override StoreAddResult Add(object key, object item)
 {
     try
     {
         if (_itemDict.ContainsKey(key))
         {
             return(StoreAddResult.KeyExists);
         }
         StorageProviderBase.StoreStatus status = base.HasSpace((ISizable)item);
         if (status == StorageProviderBase.StoreStatus.HasNotEnoughSpace)
         {
             return(StoreAddResult.NotEnoughSpace);
         }
         byte[] buffer = StoreItem.ToBinary(key, item, base.CacheContext);
         lock (_itemDict.SyncRoot)
         {
             MmfObjectPtr ptr = _internalStore.Add(buffer);
             if (ptr == null)
             {
                 return(StoreAddResult.NotEnoughSpace);
             }
             ptr.View.ParentStorageProvider = this;
             _itemDict.Add(key, ptr);
             base.Added(item as ISizable);
         }
         if (status == StorageProviderBase.StoreStatus.NearEviction)
         {
             return(StoreAddResult.SuccessNearEviction);
         }
     }
     catch (OutOfMemoryException exception)
     {
         Trace.Error("OutofMemoryException::MmfStorageProvider.Add()".GetFormatter(), exception.GetFormatter());
         return(StoreAddResult.NotEnoughSpace);
     }
     catch (Exception exception2)
     {
         Trace.Error("General Exception::MmfStorageProvider.Add()".GetFormatter(), exception2.GetFormatter());
         return(StoreAddResult.Failure);
     }
     return(StoreAddResult.Success);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Insere um novo item.
        /// </summary>
        /// <param name="key">Chave do item.</param>
        /// <param name="item">Instancia do item.</param>
        /// <returns>Resultado da operação.</returns>
        public override StoreInsResult Insert(object key, object item)
        {
            StoreInsResult notEnoughSpace;

            try
            {
                MmfObjectPtr info = (MmfObjectPtr)_itemDict[key];
                object       obj2 = null;
                if (info == null)
                {
                    switch (this.Add(key, item))
                    {
                    case StoreAddResult.NotEnoughSpace:
                        return(StoreInsResult.NotEnoughSpace);

                    case StoreAddResult.Failure:
                        return(StoreInsResult.Failure);
                    }
                    return(StoreInsResult.Success);
                }
                obj2 = this.Get(key);
                StorageProviderBase.StoreStatus status = base.HasSpace(obj2 as ISizable, (ISizable)item);
                if (status == StorageProviderBase.StoreStatus.HasNotEnoughSpace)
                {
                    notEnoughSpace = StoreInsResult.NotEnoughSpace;
                }
                else
                {
                    byte[] buffer = StoreItem.ToBinary(key, item, base.CacheContext);
                    lock (_itemDict.SyncRoot)
                    {
                        MmfObjectPtr ptr2 = _internalStore.Insert(info, buffer);
                        if (ptr2 == null)
                        {
                            return(StoreInsResult.NotEnoughSpace);
                        }
                        if (ptr2.Area != info.Area)
                        {
                            _itemDict[key] = ptr2;
                            _internalStore.Remove(info);
                        }
                        base.Inserted(obj2 as ISizable, item as ISizable);
                        if (status == StorageProviderBase.StoreStatus.NearEviction)
                        {
                            return((obj2 != null) ? StoreInsResult.SuccessOverwriteNearEviction : StoreInsResult.SuccessNearEviction);
                        }
                        notEnoughSpace = (ptr2 != null) ? StoreInsResult.SuccessOverwrite : StoreInsResult.Success;
                    }
                }
            }
            catch (OutOfMemoryException exception)
            {
                Trace.Error("MmfStorageProvider.Insert()".GetFormatter(), exception.GetFormatter());
                notEnoughSpace = StoreInsResult.NotEnoughSpace;
            }
            catch (Exception exception2)
            {
                Trace.Error("MmfStorageProvider.Insert()".GetFormatter(), exception2.GetFormatter());
                notEnoughSpace = StoreInsResult.Failure;
            }
            return(notEnoughSpace);
        }