Пример #1
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 rawTile = new RawTile(provider.DbId, pos, zoom);

                // let't check memory first
                var m = MemoryCache.GetTileFromMemoryCache(rawTile);
                if (m != null && TileImageProxy != null)
                {
                    ret = TileImageProxy.FromArray(m);
                }

                if (ret == null)
                {
                    if (ImageCache != null)
                    {
                        // hold writer for 5s
                        if (fCacheOnIdleRead)
                        {
                            Interlocked.Exchange(ref fReadingCache, 5);
                        }

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

                    ret = provider.GetTileImage(pos, zoom);
                    // Enqueue Cache
                    if (ret != null)
                    {
                        MemoryCache.AddTileToMemoryCache(rawTile, ret.Data.GetBuffer());

                        EnqueueCacheTask(new CacheQueueItem(rawTile, ret.Data.GetBuffer()));
                    }
                }
            } catch (Exception ex) {
                result = ex;
                ret    = null;
                Debug.WriteLine("GetImageFrom: " + ex);
            }

            return(ret);
        }
Пример #2
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);
        }