public virtual void Put(Job key, ITileBitmap bitmap) { lock (this) { // no-op } }
public virtual bool ContainsKey(Job key) { lock (this) { return(this.FindFile(key) != null); } }
protected internal virtual IFile FindFile(Job key) { // Search for file in directory structure zoomLevel/tileX/tileY+suffix // slow descent at the moment, better for debugging. var zoomLevelFolder = this.rootDirectory.GetFolderAsync(Convert.ToString(key.tile.ZoomLevel)).Result; if (zoomLevelFolder == null) { LOGGER.Info("Failed to find directory " + PCLStorage.PortablePath.Combine(new string[] { this.rootDirectory.Path, Convert.ToString(key.tile.ZoomLevel) })); return(null); } var tileXFolder = zoomLevelFolder.GetFolderAsync(Convert.ToString(key.tile.TileX)).Result; if (tileXFolder == null) { LOGGER.Info("Failed to find directory " + PCLStorage.PortablePath.Combine(new string[] { this.rootDirectory.Path, Convert.ToString(key.tile.ZoomLevel), Convert.ToString(key.tile.TileX) })); return(null); } var file = tileXFolder.GetFileAsync(Convert.ToString(key.tile.TileY) + this.suffix).Result; if (file == null) { LOGGER.Info("Failed to find file " + PCLStorage.PortablePath.Combine(new string[] { this.rootDirectory.Path, Convert.ToString(key.tile.ZoomLevel), Convert.ToString(key.tile.TileX), Convert.ToString(key.tile.TileY) + this.suffix })); return(null); } LOGGER.Info("Found file " + PCLStorage.PortablePath.Combine(new string[] { this.rootDirectory.Path, Convert.ToString(key.tile.ZoomLevel), Convert.ToString(key.tile.TileX), Convert.ToString(key.tile.TileY) + this.suffix })); return(file); }
public virtual void Put(Job key, ITileBitmap bitmap) { lock (this) { if (key == null) { throw new System.ArgumentException("key must not be null"); } else if (bitmap == null) { throw new System.ArgumentException("bitmap must not be null"); } ITileBitmap old = this.lruCache.Get(key); if (old != null) { old.DecrementRefCount(); } if (this.lruCache.Add(key, bitmap) != null) { LOGGER.Warn("overwriting cached entry: " + key); } bitmap.IncrementRefCount(); this.observable.NotifyObservers(); } }
public virtual ITileBitmap Get(Job key) { lock (this) { IFile file = this.FindFile(key); if (file == null) { return(null); } try { using (System.IO.Stream inputStream = file.OpenAsync(FileAccess.Read).Result) { return(this.graphicFactory.CreateTileBitmap(inputStream, key.tile.TileSize, key.hasAlpha)); } } catch (CorruptedInputStreamException) { // this can happen, at least on Android, when the input stream // is somehow corrupted, returning null ensures it will be loaded // from another source return(null); } catch (IOException) { return(null); } } }
public virtual bool ContainsKey(Job key) { lock (this) { return(this.lruCache.ContainsKey(key)); } }
public virtual void Put(Job key, ITileBitmap bitmap) { if (this.workingSet.Contains(key)) { this.firstLevelTileCache.Put(key, bitmap); } this.secondLevelTileCache.Put(key, bitmap); }
private IFile GetOutputFile(Job job) { string file = PCLStorage.PortablePath.Combine(new string[] { this.cacheDirectory.Path, job.Key }); string dir = file.Substring(0, file.LastIndexOf(PCLStorage.PortablePath.DirectorySeparatorChar)); if (IsValidCacheDirectory(FileSystem.Current.GetFolderFromPathAsync(dir).Result)) { return(FileSystem.Current.GetFolderFromPathAsync(dir).Result.CreateFileAsync(file + FILE_EXTENSION, CreationCollisionOption.ReplaceExisting).Result); } return(null); }
private void Remove(Job key) { try { //@lock.writeLock().@lock(); this.lruCache.Remove(key.Key); } finally { //@lock.writeLock().unlock(); } }
public virtual ITileBitmap Get(Job key) { lock (this) { ITileBitmap bitmap = this.lruCache.Get(key); if (bitmap != null) { bitmap.IncrementRefCount(); } return(bitmap); } }
public virtual bool ContainsKey(Job key) { try { //@lock.readLock().@lock(); // if we are using a threaded cache we return true if the tile is still in the // queue to reduce double rendering return(this.lruCache.ContainsKey(key.Key)); } finally { //@lock.readLock().unlock(); } }
/// <summary> /// stores the bitmap data on disk with filename key /// </summary> /// <param name="key"> /// filename </param> /// <param name="bitmap"> /// tile image </param> private void StoreData(Job key, ITileBitmap bitmap) { try { IFile file = GetOutputFile(key); if (file == null) { // if the file cannot be written, silently return return; } using (System.IO.Stream outputStream = file.OpenAsync(FileAccess.ReadAndWrite).Result) { bitmap.Compress(outputStream); try { //@lock.writeLock().@lock(); if (this.lruCache.Add(key.Key, file) != null) { LOGGER.Warn("overwriting cached entry: " + key.Key); } } finally { //@lock.writeLock().unlock(); } } } catch (Exception e) { // we are catching now any exception and then disable the file cache // this should ensure that no exception in the storage thread will // ever crash the main app. If there is a runtime exception, the thread // will exit (via destroy). LOGGER.Fatal("Disabling filesystem cache", e); // most likely cause is that the disk is full, just disable the // cache otherwise // more and more exceptions will be thrown. this.Destroy(); try { //@lock.writeLock().@lock(); this.lruCache = new FileWorkingSetCache <string>(0); } finally { //@lock.writeLock().unlock(); } } }
public virtual ITileBitmap Get(Job key) { ITileBitmap returnBitmap = this.firstLevelTileCache.Get(key); if (returnBitmap != null) { return(returnBitmap); } returnBitmap = this.secondLevelTileCache.Get(key); if (returnBitmap != null) { this.firstLevelTileCache.Put(key, returnBitmap); return(returnBitmap); } return(null); }
public virtual ITileBitmap Get(Job key) { IFile file; try { //@lock.readLock().@lock(); file = this.lruCache.Get(key.Key); } finally { //@lock.readLock().unlock(); } if (file == null) { return(null); } try { using (System.IO.Stream inputStream = file.OpenAsync(FileAccess.Read).Result) { ITileBitmap result = this.graphicFactory.CreateTileBitmap(inputStream, key.tile.TileSize, key.hasAlpha); // TODO //result.Timestamp = file.lastModified(); return(result); } } catch (CorruptedInputStreamException e) { // this can happen, at least on Android, when the input stream // is somehow corrupted, returning null ensures it will be loaded // from another source Remove(key); LOGGER.Warn("input stream from file system cache invalid " + key.Key, e); return(null); } catch (IOException e) { Remove(key); LOGGER.Fatal(e.Message, e); return(null); } }
public virtual void Put(Job key, ITileBitmap bitmap) { if (key == null) { throw new System.ArgumentException("key must not be null"); } else if (bitmap == null) { throw new System.ArgumentException("bitmap must not be null"); } if (Capacity == 0) { return; } StoreData(key, bitmap); this.observable.NotifyObservers(); }
public void Run() { var zFolders = outerInstance.cacheDirectory.GetFoldersAsync().Result; if (zFolders != null) { foreach (IFolder z in zFolders) { var xFolders = z.GetFoldersAsync().Result; if (xFolders != null) { foreach (IFolder x in xFolders) { var yFiles = x.GetFilesAsync().Result; if (yFiles != null) { foreach (IFile y in yFiles) { if (IsValidFile(y) && y.Name.EndsWith(FILE_EXTENSION)) { int index = y.Name.LastIndexOf(FILE_EXTENSION); string key = Job.ComposeKey(z.Name, x.Name, y.Name.Substring(0, index)); try { //[email protected]().@lock(); if (outerInstance.lruCache.Add(key, y) != null) { LOGGER.Warn("overwriting cached entry: " + key); } } finally { //[email protected]().unlock(); } } } } } } } } }
public virtual ITileBitmap GetImmediately(Job key) { return(firstLevelTileCache.Get(key)); }
public virtual ITileBitmap GetImmediately(Job key) { return(Get(key)); }
public virtual bool ContainsKey(Job key) { return(this.firstLevelTileCache.ContainsKey(key) || this.secondLevelTileCache.ContainsKey(key)); }