Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1

2
namespace GMap.NET.Internals
3
{
4
   using System.Collections.Generic;
5
   using System.IO;
6
   using System;
7
   using System.Diagnostics;
8
 
9
   /// <summary>
10
   /// kiber speed memory cache for tiles with history support ;}
11
   /// </summary>
12
   internal class KiberTileCache : Dictionary<RawTile, byte[]>
13
   {
14
       public KiberTileCache() : base(new RawTileComparer())
15
       {
16
       }
17
 
18
      readonly Queue<RawTile> Queue = new Queue<RawTile>();
19
 
20
      /// <summary>
21
      /// the amount of tiles in MB to keep in memmory, default: 22MB, if each ~100Kb it's ~222 tiles
22
      /// </summary>
23
#if !PocketPC
24
      public int MemoryCacheCapacity = 22;
25
#else
26
      public int MemoryCacheCapacity = 3;
27
#endif
28
 
29
      long memoryCacheSize = 0;
30
 
31
      /// <summary>
32
      /// current memmory cache size in MB
33
      /// </summary>
34
      public double MemoryCacheSize
35
      {
36
         get
37
         {
38
            return memoryCacheSize / 1048576.0;
39
         }
40
      }
41
 
42
      public new void Add(RawTile key, byte[] value)
43
      {
44
         Queue.Enqueue(key);
45
         base.Add(key, value);
46
 
47
         memoryCacheSize += value.Length;
48
      }
49
 
50
      // do not allow directly removal of elements
51
      private new void Remove(RawTile key)
52
      {
53
 
54
      }
55
 
56
      public new void Clear()
57
      {
58
         Queue.Clear();
59
         base.Clear();
60
         memoryCacheSize = 0;
61
      }
62
 
63
      internal void RemoveMemoryOverload()
64
      {
65
         while(MemoryCacheSize > MemoryCacheCapacity)
66
         {
67
            if(Keys.Count > 0 && Queue.Count > 0)
68
            {
69
               RawTile first = Queue.Dequeue();
70
               try
71
               {
72
                  var m = base[first];
73
                  {
74
                     base.Remove(first);
75
                     memoryCacheSize -= m.Length;
76
                  }
77
                  m = null;
78
               }
79
               catch(Exception ex)
80
               {
81
                   Debug.WriteLine("RemoveMemoryOverload: " + ex);
82
               }
83
            }
84
            else
85
            {
86
               break;
87
            }
88
         }
89
      }
90
   }
91
}