Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2498 | - | 1 | |
2 | namespace GMap.NET.CacheProviders |
||
3 | { |
||
4 | using System.Diagnostics; |
||
5 | using GMap.NET.Internals; |
||
6 | using System; |
||
7 | |||
8 | public class MemoryCache : IDisposable |
||
9 | { |
||
10 | readonly KiberTileCache TilesInMemory = new KiberTileCache(); |
||
11 | |||
12 | FastReaderWriterLock kiberCacheLock = new FastReaderWriterLock(); |
||
13 | |||
14 | /// <summary> |
||
15 | /// the amount of tiles in MB to keep in memmory, default: 22MB, if each ~100Kb it's ~222 tiles |
||
16 | /// </summary> |
||
17 | public int Capacity |
||
18 | { |
||
19 | get |
||
20 | { |
||
21 | kiberCacheLock.AcquireReaderLock(); |
||
22 | try |
||
23 | { |
||
24 | return TilesInMemory.MemoryCacheCapacity; |
||
25 | } |
||
26 | finally |
||
27 | { |
||
28 | kiberCacheLock.ReleaseReaderLock(); |
||
29 | } |
||
30 | } |
||
31 | set |
||
32 | { |
||
33 | kiberCacheLock.AcquireWriterLock(); |
||
34 | try |
||
35 | { |
||
36 | TilesInMemory.MemoryCacheCapacity = value; |
||
37 | } |
||
38 | finally |
||
39 | { |
||
40 | kiberCacheLock.ReleaseWriterLock(); |
||
41 | } |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /// <summary> |
||
46 | /// current memmory cache size in MB |
||
47 | /// </summary> |
||
48 | public double Size |
||
49 | { |
||
50 | get |
||
51 | { |
||
52 | kiberCacheLock.AcquireReaderLock(); |
||
53 | try |
||
54 | { |
||
55 | return TilesInMemory.MemoryCacheSize; |
||
56 | } |
||
57 | finally |
||
58 | { |
||
59 | kiberCacheLock.ReleaseReaderLock(); |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | public void Clear() |
||
65 | { |
||
66 | kiberCacheLock.AcquireWriterLock(); |
||
67 | try |
||
68 | { |
||
69 | TilesInMemory.Clear(); |
||
70 | } |
||
71 | finally |
||
72 | { |
||
73 | kiberCacheLock.ReleaseWriterLock(); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | // ... |
||
78 | |||
79 | internal byte[] GetTileFromMemoryCache(RawTile tile) |
||
80 | { |
||
81 | kiberCacheLock.AcquireReaderLock(); |
||
82 | try |
||
83 | { |
||
84 | byte[] ret = null; |
||
85 | if(TilesInMemory.TryGetValue(tile, out ret)) |
||
86 | { |
||
87 | return ret; |
||
88 | } |
||
89 | } |
||
90 | finally |
||
91 | { |
||
92 | kiberCacheLock.ReleaseReaderLock(); |
||
93 | } |
||
94 | return null; |
||
95 | } |
||
96 | |||
97 | internal void AddTileToMemoryCache(RawTile tile, byte[] data) |
||
98 | { |
||
99 | if(data != null) |
||
100 | { |
||
101 | kiberCacheLock.AcquireWriterLock(); |
||
102 | try |
||
103 | { |
||
104 | if(!TilesInMemory.ContainsKey(tile)) |
||
105 | { |
||
106 | TilesInMemory.Add(tile, data); |
||
107 | } |
||
108 | } |
||
109 | finally |
||
110 | { |
||
111 | kiberCacheLock.ReleaseWriterLock(); |
||
112 | } |
||
113 | } |
||
114 | #if DEBUG |
||
115 | else |
||
116 | { |
||
117 | Debug.WriteLine("adding empty data to MemoryCache ;} "); |
||
118 | if(Debugger.IsAttached) |
||
119 | { |
||
120 | Debugger.Break(); |
||
121 | } |
||
122 | } |
||
123 | #endif |
||
124 | } |
||
125 | |||
126 | internal void RemoveOverload() |
||
127 | { |
||
128 | kiberCacheLock.AcquireWriterLock(); |
||
129 | try |
||
130 | { |
||
131 | TilesInMemory.RemoveMemoryOverload(); |
||
132 | } |
||
133 | finally |
||
134 | { |
||
135 | kiberCacheLock.ReleaseWriterLock(); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | #region IDisposable Members |
||
140 | |||
141 | ~MemoryCache() |
||
142 | { |
||
143 | Dispose(false); |
||
144 | } |
||
145 | |||
146 | void Dispose(bool disposing) |
||
147 | { |
||
148 | if(kiberCacheLock != null) |
||
149 | { |
||
150 | if(disposing) |
||
151 | { |
||
152 | Clear(); |
||
153 | } |
||
154 | |||
155 | kiberCacheLock.Dispose(); |
||
156 | kiberCacheLock = null; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | public void Dispose() |
||
161 | { |
||
162 | this.Dispose(true); |
||
163 | GC.SuppressFinalize(this); |
||
164 | } |
||
165 | |||
166 | #endregion |
||
167 | } |
||
168 | } |