Ejemplo n.º 1
0
        public override void Set(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
        {
            PrimaryCache.Set(key, value, absoluteExpiration, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.Set(key, PrimaryCache.Get(key), absoluteExpiration, regionName));
        }
Ejemplo n.º 2
0
        public override void Set(CacheItem item, CacheItemPolicy policy)
        {
            PrimaryCache.Set(item, policy);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.Set(PrimaryCache.GetCacheItem(item.Key, item.RegionName), policy));
        }
Ejemplo n.º 3
0
        public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            PrimaryCache.Set(key, value, policy, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.Set(key, PrimaryCache.Get(key), policy, regionName));
        }
Ejemplo n.º 4
0
        public override object Remove(string key, string regionName = null)
        {
            var item = PrimaryCache.Remove(key, regionName);

            SecondaryCache.Remove(key, regionName);

            return(item);
        }
Ejemplo n.º 5
0
        public override object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
        {
            var item = PrimaryCache.AddOrGetExisting(key, value, absoluteExpiration, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.AddOrGetExisting(key, PrimaryCache.Get(key), absoluteExpiration, regionName));

            return(item);
        }
Ejemplo n.º 6
0
        public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)
        {
            var item = PrimaryCache.AddOrGetExisting(value, policy);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.AddOrGetExisting(PrimaryCache.GetCacheItem(value.Key), policy));

            return(item);
        }
Ejemplo n.º 7
0
        public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            var item = PrimaryCache.AddOrGetExisting(key, value, policy, regionName);

            //queue up writing to secondary cache on another thread
            ThreadPool.QueueUserWorkItem(_ => SecondaryCache.AddOrGetExisting(key, PrimaryCache.Get(key), policy, regionName));

            return(item);
        }
Ejemplo n.º 8
0
        public override object Get(string key, string regionName = null)
        {
            var item = PrimaryCache.Get(key, regionName);

            if (item == null)
            {
                item = SecondaryCache.Get(key, regionName);

                if (item != null)
                {
                    PrimaryCache.Set(key, item, new CacheItemPolicy(), regionName);
                }
            }

            return(item);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// gets image from tile server
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="pos"></param>
        /// <param name="zoom"></param>
        /// <returns></returns>
        public PureImage GetImageFrom(GMapProvider provider, GPoint pos, int zoom, out Exception result)
        {
            PureImage ret = null;

            result = null;

            try
            {
                var rtile = new RawTile(provider.DbId, pos, zoom);

                // let't check memmory first
                if (UseMemoryCache)
                {
                    var m = MemoryCache.GetTileFromMemoryCache(rtile);
                    if (m != null)
                    {
                        if (GMapProvider.TileImageProxy != null)
                        {
                            ret = GMapProvider.TileImageProxy.FromArray(m);
                            if (ret == null)
                            {
#if DEBUG
                                Debug.WriteLine("Image disposed in MemoryCache o.O, should never happen ;} " + new RawTile(provider.DbId, pos, zoom));
                                if (Debugger.IsAttached)
                                {
                                    Debugger.Break();
                                }
#endif
                                m = null;
                            }
                        }
                    }
                }

                if (ret == null)
                {
                    if (Mode != AccessMode.ServerOnly && !provider.BypassCache)
                    {
                        if (PrimaryCache != null)
                        {
                            // hold writer for 5s
                            if (cacheOnIdleRead)
                            {
                                Interlocked.Exchange(ref readingCache, 5);
                            }

                            ret = PrimaryCache.GetImageFromCache(provider.DbId, pos, zoom);
                            if (ret != null)
                            {
                                if (UseMemoryCache)
                                {
                                    MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                                }
                                return(ret);
                            }
                        }

                        if (SecondaryCache != null)
                        {
                            // hold writer for 5s
                            if (cacheOnIdleRead)
                            {
                                Interlocked.Exchange(ref readingCache, 5);
                            }

                            ret = SecondaryCache.GetImageFromCache(provider.DbId, pos, zoom);
                            if (ret != null)
                            {
                                if (UseMemoryCache)
                                {
                                    MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                                }
                                EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.First));
                                return(ret);
                            }
                        }
                    }

                    if (Mode != AccessMode.CacheOnly)
                    {
                        ret = provider.GetTileImage(pos, zoom);
                        {
                            // Enqueue Cache
                            if (ret != null)
                            {
                                if (UseMemoryCache)
                                {
                                    MemoryCache.AddTileToMemoryCache(rtile, ret.Data.GetBuffer());
                                }

                                if (Mode != AccessMode.ServerOnly && !provider.BypassCache)
                                {
                                    EnqueueCacheTask(new CacheQueueItem(rtile, ret.Data.GetBuffer(), CacheUsage.Both));
                                }
                            }
                        }
                    }
                    else
                    {
                        result = noDataException;
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex;
                ret    = null;
                Debug.WriteLine("GetImageFrom: " + ex.ToString());
            }

            return(ret);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// live for cache ;}
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CacheEngineLoop()
        {
            Debug.WriteLine("CacheEngine: start");
            int left = 0;

            OnTileCacheStart?.Invoke();

            bool startEvent = false;

            while (!abortCacheLoop)
            {
                try
                {
                    CacheQueueItem?task = null;

                    lock (tileCacheQueue)
                    {
                        left = tileCacheQueue.Count;
                        if (left > 0)
                        {
                            task = tileCacheQueue.Dequeue();
                        }
                    }

                    if (task.HasValue)
                    {
                        if (startEvent)
                        {
                            startEvent = false;

                            OnTileCacheStart?.Invoke();
                        }

                        OnTileCacheProgress?.Invoke(left);

                        #region -- save --
                        // check if stream wasn't disposed somehow
                        if (task.Value.Img != null)
                        {
                            Debug.WriteLine("CacheEngine[" + left + "]: storing tile " + task.Value + ", " + task.Value.Img.Length / 1024 + "kB...");

                            if ((task.Value.CacheType & CacheUsage.First) == CacheUsage.First && PrimaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                PrimaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            if ((task.Value.CacheType & CacheUsage.Second) == CacheUsage.Second && SecondaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                SecondaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            task.Value.Clear();

                            if (!boostCacheEngine)
                            {
#if PocketPC
                                Thread.Sleep(3333);
#else
                                Thread.Sleep(333);
#endif
                            }
                        }
                        else
                        {
                            Debug.WriteLine("CacheEngineLoop: skip, tile disposed to early -> " + task.Value);
                        }
                        task = null;
                        #endregion
                    }
                    else
                    {
                        if (!startEvent)
                        {
                            startEvent = true;

                            OnTileCacheComplete?.Invoke();
                        }

                        if (abortCacheLoop || noMapInstances || !WaitForCache.WaitOne(33333, false) || noMapInstances)
                        {
                            break;
                        }
                    }
                }
#if !PocketPC
                catch (AbandonedMutexException)
                {
                    break;
                }
#endif
                catch (Exception ex)
                {
                    Debug.WriteLine("CacheEngineLoop: " + ex.ToString());
                }
            }
            Debug.WriteLine("CacheEngine: stop");

            if (!startEvent)
            {
                OnTileCacheComplete?.Invoke();
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// live for cache ;}
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CacheEngineLoop()
        {
            int left = 0;

            if (OnTileCacheStart != null)
            {
                OnTileCacheStart();
            }

            bool startEvent = false;

            while (!abortCacheLoop)
            {
                try
                {
                    CacheQueueItem?task = null;

                    lock (tileCacheQueue)
                    {
                        left = tileCacheQueue.Count;
                        if (left > 0)
                        {
                            task = tileCacheQueue.Dequeue();
                        }
                    }

                    if (task.HasValue)
                    {
                        if (startEvent)
                        {
                            startEvent = false;

                            if (OnTileCacheStart != null)
                            {
                                OnTileCacheStart();
                            }
                        }

                        if (OnTileCacheProgress != null)
                        {
                            OnTileCacheProgress(left);
                        }

                        #region -- save --
                        // check if stream wasn't disposed somehow
                        if (task.Value.Img != null)
                        {
                            if ((task.Value.CacheType & CacheUsage.First) == CacheUsage.First && PrimaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                PrimaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            if ((task.Value.CacheType & CacheUsage.Second) == CacheUsage.Second && SecondaryCache != null)
                            {
                                if (cacheOnIdleRead)
                                {
                                    while (Interlocked.Decrement(ref readingCache) > 0)
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                SecondaryCache.PutImageToCache(task.Value.Img, task.Value.Tile.Type, task.Value.Tile.Pos, task.Value.Tile.Zoom);
                            }

                            task.Value.Clear();

                            if (!boostCacheEngine)
                            {
#if PocketPC
                                Thread.Sleep(3333);
#else
                                Thread.Sleep(333);
#endif
                            }
                        }
                        else
                        {
                        }
                        task = null;
                        #endregion
                    }
                    else
                    {
                        if (!startEvent)
                        {
                            startEvent = true;

                            if (OnTileCacheComplete != null)
                            {
                                OnTileCacheComplete();
                            }
                        }

                        if (abortCacheLoop || noMapInstances || !WaitForCache.WaitOne(33333, false) || noMapInstances)
                        {
                            break;
                        }
                    }
                }
#if !PocketPC
                catch (AbandonedMutexException)
                {
                    break;
                }
#endif
                catch
                {
                }
            }

            if (!startEvent)
            {
                if (OnTileCacheComplete != null)
                {
                    OnTileCacheComplete();
                }
            }
        }
Ejemplo n.º 12
0
 public override bool Contains(string key, string regionName = null)
 {
     return(PrimaryCache.Contains(key, regionName) || SecondaryCache.Contains(key, regionName));
 }