Skip to content

Commit

Permalink
Tile Cache Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
BiologyTools committed Apr 20, 2024
1 parent 9e7e687 commit b074bdc
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions Source/Bio/ISlideSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@
using AForge;
namespace Bio
{
public class LruCache<TKey, TValue>
public class LruCache<TileInformation, TValue>
{
public class Info
{
public ZCT Coordinate { get; set; }
public TileIndex Index { get; set; }
}
private readonly int capacity;
private Dictionary<TKey, LinkedListNode<(TKey key, TValue value)>> cacheMap = new Dictionary<TKey, LinkedListNode<(TKey key, TValue value)>>();
private LinkedList<(TKey key, TValue value)> lruList = new LinkedList<(TKey key, TValue value)>();
private Dictionary<Info, LinkedListNode<(Info key, TValue value)>> cacheMap = new Dictionary<Info, LinkedListNode<(Info key, TValue value)>>();
private LinkedList<(Info key, TValue value)> lruList = new LinkedList<(Info key, TValue value)>();

public LruCache(int capacity)
{
this.capacity = capacity;
}

public TValue Get(TKey key)
public TValue Get(Info key)
{
if (cacheMap.TryGetValue(key, out var node))
{
lruList.Remove(node);
lruList.AddLast(node);
return node.Value.value;
Info tf = (Info)node.Value.key;
}

return default(TValue);
}

public void Add(TKey key, TValue value)
public void Add(Info key, TValue value)
{
if (cacheMap.Count >= capacity)
{
Expand All @@ -51,25 +55,26 @@ public void Add(TKey key, TValue value)
lruList.Remove(cacheMap[key]);
}

var newNode = new LinkedListNode<(TKey key, TValue value)>((key, value));
var newNode = new LinkedListNode<(Info key, TValue value)>((key, value));
lruList.AddLast(newNode);
cacheMap[key] = newNode;
}
}
public class TileCache
{
private LruCache<TileInfo, byte[]> cache;
private LruCache<TileInformation, byte[]> cache;
private int capacity;
SlideSourceBase source = null;
public TileCache(SlideSourceBase source, int capacity = 10000)
public TileCache(SlideSourceBase source, int capacity = 500)
{
this.source = source;
this.capacity = capacity;
this.cache = new LruCache<TileInfo, byte[]>(capacity);
this.cache = new LruCache<TileInformation, byte[]>(capacity);
}

public async Task<byte[]> GetTile(TileInfo info)
public async Task<byte[]> GetTile(TileInformation info)
{

byte[] data = cache.Get(info);
if (data != null)
{
Expand All @@ -80,12 +85,12 @@ public async Task<byte[]> GetTile(TileInfo info)
return tile;
}

private void AddTile(TileInfo tileId, byte[] tile)
private void AddTile(TileInformation tileId, byte[] tile)
{
cache.Add(tileId, tile);
}

private async Task<byte[]> LoadTile(TileInfo tileId)
private async Task<byte[]> LoadTile(TileInformation tileId)
{
try
{
Expand All @@ -98,7 +103,7 @@ private async Task<byte[]> LoadTile(TileInfo tileId)
}
}

public class TileInfo
public class TileInformation
{
public TileIndex Index { get; set; }
public Extent Extent { get; set; }
Expand Down Expand Up @@ -162,7 +167,7 @@ public async Task<byte[]> GetSlice(SliceInfo sliceInfo)
List<Tuple<Extent, byte[]>> tiles = new List<Tuple<Extent, byte[]>>();
foreach (BruTile.TileInfo t in tileInfos)
{
TileInfo tf = new TileInfo();
TileInformation tf = new TileInformation();
tf.Extent = t.Extent;
tf.Coordinate = App.viewer.GetCoordinate();
tf.Index = t.Index;
Expand Down Expand Up @@ -265,7 +270,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public async Task<byte[]> GetTileAsync(TileInfo tileInfo)
public async Task<byte[]> GetTileAsync(TileInformation tileInfo)
{
if (tileInfo == null)
return null;
Expand Down

0 comments on commit b074bdc

Please sign in to comment.