private void AddMemoryCacheItem(OnlineMapsTile tile, int tileHash, string tileKey)
    {
        for (int i = 0; i < countMemoryItems; i++)
        {
            if (memoryCache[i].hash == tileHash && memoryCache[i].key == tileKey)
            {
                return;
            }
        }

        if (memoryCache == null)
        {
            memoryCache = new MemoryCacheItem[maxMemoryCacheSize * 10];
        }
        else if (memoryCache.Length == countMemoryItems)
        {
            Array.Resize(ref memoryCache, memoryCache.Length + 50);
        }

        MemoryCacheItem item = new MemoryCacheItem(tileKey, tile);

        memoryCache[countMemoryItems++] = item;
        _memoryCacheSize += item.size;
        if (memoryCacheSize > maxMemoryCacheSize * 1000000)
        {
            UnloadOldMemoryCacheItems();
        }
    }
    private bool TryLoadFromMemoryCache(OnlineMapsTile tile)
    {
        if (memoryCache == null)
        {
            return(false);
        }

        string key     = GetTileKey(tile);
        int    keyHash = key.GetHashCode();

        MemoryCacheItem item = null;

        for (int itemIndex = 0; itemIndex < countMemoryItems; itemIndex++)
        {
            if (memoryCache[itemIndex].hash == keyHash && memoryCache[itemIndex].key == key)
            {
                item      = memoryCache[itemIndex];
                item.time = DateTime.Now.Ticks;
                break;
            }
        }
        if (item == null)
        {
            return(false);
        }

        LoadTile(tile, item.bytes);
        if (OnLoadedFromMemoryCache != null)
        {
            OnLoadedFromMemoryCache(tile);
        }

        return(true);
    }
    private bool TryLoadTraffic(OnlineMapsTile tile)
    {
        if (memoryCache == null)
        {
            return(false);
        }

        string key     = GetTrafficKey(tile);
        int    keyHash = key.GetHashCode();

        MemoryCacheItem item = null;

        for (int itemIndex = 0; itemIndex < countMemoryItems; itemIndex++)
        {
            if (memoryCache[itemIndex].hash == keyHash && memoryCache[itemIndex].key == key)
            {
                item      = memoryCache[itemIndex];
                item.time = DateTime.Now.Ticks;
                break;
            }
        }
        if (item == null)
        {
            return(false);
        }

        Texture2D trafficTexture = new Texture2D(256, 256, TextureFormat.RGB24, false);

        trafficTexture.LoadImage(item.bytes);
        trafficTexture.wrapMode = TextureWrapMode.Clamp;
        tile.trafficTexture     = trafficTexture;
        map.Redraw();

        return(true);
    }
Example #4
0
 public void Build()
 {
     cache = new Cache();
     cacheItemUUID = UUID.Random();
     MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(),DateTime.Now + TimeSpan.FromDays(1));
     byte[] foo = new byte[1];
     foo[0] = 255;
     cachedItem.Store(foo);
     cache.Store(cacheItemUUID.ToString(), cachedItem);
 }
Example #5
0
 public void Build()
 {
     cache = new Cache();
     cache = new Cache(CacheMedium.Memory,CacheStrategy.Aggressive,CacheFlags.AllowUpdate);
     cacheItemUUID = UUID.Random();
     MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(),DateTime.Now + TimeSpan.FromDays(1));
     byte[] foo = new byte[1];
     foo[0] = 255;
     cachedItem.Store(foo);
     cache.Store(cacheItemUUID.ToString(), cachedItem);
 }
Example #6
0
        public void Build()
        {
            cache         = new Cache();
            cacheItemUUID = UUID.Random();
            MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(), DateTime.Now + TimeSpan.FromDays(1));

            byte[] foo = new byte[1];
            foo[0] = 255;
            cachedItem.Store(foo);
            cache.Store(cacheItemUUID.ToString(), cachedItem);
        }
Example #7
0
        public void Build()
        {
            cache         = new Cache();
            cache         = new Cache(CacheMedium.Memory, CacheStrategy.Aggressive, CacheFlags.AllowUpdate);
            cacheItemUUID = UUID.Random();
            MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(), DateTime.Now + TimeSpan.FromDays(1));

            byte[] foo = new byte[1];
            foo[0] = 255;
            cachedItem.Store(foo);
            cache.Store(cacheItemUUID.ToString(), cachedItem);
        }
Example #8
0
 public void Set(string key, object value, int expire = -1)
 {
     lock (_sync)
     {
         var expirationDate = DateTime.MinValue;
         if (expire > 0)
         {
             expirationDate = DateTime.Now.AddSeconds(expire);
         }
         Items[key] = new MemoryCacheItem(value, expirationDate);
     }
 }
Example #9
0
 public void ExpireItemManually()
 {
     UUID ImmediateExpiryUUID = UUID.Random();
     MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(1));
     byte[] foo = new byte[1];
     foo[0] = 1;
     cachedItem.Store(foo);
     cache.Store(cacheItemUUID.ToString(), cachedItem);
     cache.Invalidate(cacheItemUUID.ToString());
     cache.Get(cacheItemUUID.ToString());
     object citem = cache.Get(cacheItemUUID.ToString());
     Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
 }
Example #10
0
        public void TestTTLExpiredEntry()
        {
            UUID ImmediateExpiryUUID = UUID.Random();
            MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(-1));
            byte[] foo = new byte[1];
            foo[0] = 1;
            cachedItem.Store(foo);
            cache.Store(cacheItemUUID.ToString(), cachedItem);

            cache.Get(cacheItemUUID.ToString());
            //object citem = cache.Get(cacheItemUUID.ToString());
            //Assert.That(citem == null, "Item should not be in Cache because the expiry time was before now");
        }
Example #11
0
        public void ExpireItemManually()
        {
            UUID            ImmediateExpiryUUID = UUID.Random();
            MemoryCacheItem cachedItem          = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(1));

            byte[] foo = new byte[1];
            foo[0] = 1;
            cachedItem.Store(foo);
            cache.Store(cacheItemUUID.ToString(), cachedItem);
            cache.Invalidate(ImmediateExpiryUUID.ToString());
            cache.Get(cacheItemUUID.ToString());
            //object citem = cache.Get(cacheItemUUID.ToString());
            //Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
        }
Example #12
0
        public void TestTTLExpiredEntry()
        {
            UUID            ImmediateExpiryUUID = UUID.Random();
            MemoryCacheItem cachedItem          = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(-1));

            byte[] foo = new byte[1];
            foo[0] = 1;
            cachedItem.Store(foo);
            cache.Store(cacheItemUUID.ToString(), cachedItem);

            cache.Get(cacheItemUUID.ToString());
            //object citem = cache.Get(cacheItemUUID.ToString());
            //Assert.That(citem == null, "Item should not be in Cache because the expiry time was before now");
        }
        /// <summary>
        /// 设置缓存项
        /// </summary>
        /// <typeparam name="T">缓存数据类型</typeparam>
        /// <param name="key">Key</param>
        /// <param name="item">缓存数据</param>
        /// <param name="policy">缓存策略</param>
        public void Set <T>(string key, T item, CachePolicy?policy = null)
        {
            var cacheItem = new MemoryCacheItem <T>
            {
                Value = item
            };
            MemoryCacheEntryOptions opt = new MemoryCacheEntryOptions();

            var entry = _cache.CreateEntry(key);

            entry.Value = item;
            if (policy != null)
            {
                opt.AbsoluteExpirationRelativeToNow = policy.Value.AbsoluteExpiration;
                opt.SlidingExpiration = policy.Value.SlidingExpiration;
            }

            _cache.Set(key, cacheItem, opt);
        }
Example #14
0
        private static void StoreToMemoryCache(string key, BitmapImage image)
        {
            lock (_memoryCache)
            {
                var mci = new MemoryCacheItem()
                {
                    LastUpdated = DateTime.Now,
                    Image       = image
                };
                if (_memoryCache.Contains(key))
                {
                    _memoryCache[key] = mci;
                }
                else
                {
                    _memoryCache.Add(key, mci);
                }

                FixMemoryCacheSize();
            }
        }
    private void UnloadOldMemoryCacheItems()
    {
        int countUnload = Mathf.RoundToInt(countMemoryItems * memoryCacheUnloadRate);

        if (countUnload == 0)
        {
            return;
        }
        if (countUnload < 0)
        {
            throw new Exception("Can not unload a negative number of items. Check memoryCacheUnloadRate.");
        }
        if (countMemoryItems < countUnload)
        {
            countUnload = countMemoryItems;
        }

        long[] unloadTimes   = new long[countUnload];
        int[]  unloadIndices = new int[countUnload];
        int    c             = 0;

        for (int i = 0; i < countMemoryItems; i++)
        {
            long t = memoryCache[i].time;
            if (c == 0)
            {
                unloadIndices[0] = 0;
                unloadTimes[0]   = t;
                c++;
            }
            else
            {
                int index  = c;
                int index2 = index - 1;

                while (index2 >= 0)
                {
                    if (unloadTimes[index2] < t)
                    {
                        break;
                    }

                    index2--;
                    index--;
                }

                if (index < countUnload)
                {
                    for (int j = countUnload - 1; j > index; j--)
                    {
                        unloadIndices[j] = unloadIndices[j - 1];
                        unloadTimes[j]   = unloadTimes[j - 1];
                    }
                    unloadIndices[index] = i;
                    unloadTimes[index]   = t;
                    if (c < countUnload)
                    {
                        c++;
                    }
                }
            }
        }

        for (int i = 0; i < countUnload; i++)
        {
            int             index = unloadIndices[i];
            MemoryCacheItem mci   = memoryCache[index];
            if (mci.tileUsed)
            {
                continue;
            }

            _memoryCacheSize -= mci.size;
            mci.Dispose();
            memoryCache[index] = null;
        }

        int offset = 0;

        for (int i = 0; i < countMemoryItems; i++)
        {
            if (memoryCache[i] == null)
            {
                offset++;
            }
            else if (offset > 0)
            {
                memoryCache[i - offset] = memoryCache[i];
            }
        }

        countMemoryItems -= countUnload;
    }
 /// <summary>
 /// ake current cache item most recently used
 /// </summary>
 /// <param name="cacheItem">cache item</param>
 /// <returns>cache item wrapped into node of type <see cref="LinkedListNode"/></returns>
 /// <remarks>Both operation in the method have O(1) complexity</remarks>
 private LinkedListNode <MemoryCacheItem <TKey, TValue> > UpdateAccessChain(MemoryCacheItem <TKey, TValue> cacheItem)
 {
     return(_accessChain.AddFirst(cacheItem));
 }