Exemplo n.º 1
0
 public static void AddItem(string key, object value)
 {
     if (CacheInstance != null)
     {
         CacheInstance.Add(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(20), CacheItemPriority.Normal, null);
     }
 }
Exemplo n.º 2
0
        private void NotFoundInCache(int address)
        {
            if (cache.Count < _cacheSize)
            {
                // Cache not full
                WriteLog("Додано дані до кеш");
                cache.Add(new CacheInstance(address)
                {
                    Value = mainMemory[address]
                });
                FoundInCache(cache.Count - 1);
                return;
            }

            // Cache full
            var cacheN = FindLeastPopular();

            if (cache[cacheN].Changed)
            {
                mainMemory[cache[cacheN].Address] = cache[cacheN].Value;
                WriteLog("Дані записанo в пам'ять");
            }

            WriteLog("Заміна даних в кеш");
            cache[cacheN] = new CacheInstance(address)
            {
                Value = mainMemory[address]
            };
            FoundInCache(cacheN);
            return;
        }
Exemplo n.º 3
0
        public CacheInstance GetCache(Data.Guid guid)
        {
            CacheInstance table = caches.GetValue(guid);
            if (table == null)
            {
                lock (caches)
                {
                    if (table == null)
                    {
                        table = new CacheInstance(guid);
                        caches.Add(guid, table);
                    }
                }
            }

            return table;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the value stored in the cache item.
        /// </summary>
        /// <remarks>
        /// This value must be serializable, otherwise <see cref="ArgumentException"/>
        /// is thrown when you will try to add or insert the CacheItem.
        /// </remarks>
        /// <typeparam name="T">Specifies the type of value obtained from the cache item.</typeparam>
        /// <returns>Value of the cache item with the type defined.</returns>
        /// <example>
        /// Example gets value of a cache item
        /// <code>
        /// CacheItem item = cache.GetCacheItem("Product0");
        ///
        /// Product product = item.GetValue&lt;Product&gt;();
        /// </code>
        /// </example>
        public T GetValue <T>()
        {
            if (_value is ValueEmissary)
            {
                if (_currentValue != default(object) && _currentValue is T)
                {
                    return((T)_currentValue);
                }

                var emissary = _value as ValueEmissary;

                if (CacheInstance == default(Cache))
                {
                    return((T)emissary.Data);
                }
                _currentValue = CacheInstance.SafeDeserialize <T>(emissary.Data, CacheInstance.SerializationContext, FlagMap, UserObjectType.CacheItem);

                return((T)_currentValue);
            }
            return((T)_value);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Deletes the specified key.
 /// </summary>
 /// <param name="key">The key value to be deleted.</param>
 public void Delete(string key)
 {
     CacheInstance.Remove(key);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Clears the cache by deleting all the items.
 /// </summary>
 public void Clear()
 {
     CacheInstance.Clear();
 }
Exemplo n.º 7
0
 /// <summary>
 /// Determines whether the specified key exists or not.
 /// </summary>
 /// <param name="key">The key value to be checked.</param>
 /// <returns>
 ///   <c>true</c> if the key exists; otherwise <c>false</c>.
 /// </returns>
 public bool Exists(string key)
 {
     return(CacheInstance.ContainsKey(key));
 }