diff options
author | onefang | 2019-05-19 21:24:15 +1000 |
---|---|---|
committer | onefang | 2019-05-19 21:24:15 +1000 |
commit | 5e4d6cab00cb29cd088ab7b62ab13aff103b64cb (patch) | |
tree | a9fbc62df9eb2d1d9ba2698d8552eae71eca20d8 /OpenSim/Framework/Cache.cs | |
parent | Add a build script. (diff) | |
download | opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.zip opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.gz opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.bz2 opensim-SC-5e4d6cab00cb29cd088ab7b62ab13aff103b64cb.tar.xz |
Dump OpenSim 0.9.0.1 into it's own branch.
Diffstat (limited to 'OpenSim/Framework/Cache.cs')
-rw-r--r-- | OpenSim/Framework/Cache.cs | 83 |
1 files changed, 54 insertions, 29 deletions
diff --git a/OpenSim/Framework/Cache.cs b/OpenSim/Framework/Cache.cs index 31cab4a..3ca85d7 100644 --- a/OpenSim/Framework/Cache.cs +++ b/OpenSim/Framework/Cache.cs | |||
@@ -89,14 +89,14 @@ namespace OpenSim.Framework | |||
89 | public CacheItemBase(string index) | 89 | public CacheItemBase(string index) |
90 | { | 90 | { |
91 | uuid = index; | 91 | uuid = index; |
92 | entered = DateTime.Now; | 92 | entered = DateTime.UtcNow; |
93 | lastUsed = entered; | 93 | lastUsed = entered; |
94 | } | 94 | } |
95 | 95 | ||
96 | public CacheItemBase(string index, DateTime ttl) | 96 | public CacheItemBase(string index, DateTime ttl) |
97 | { | 97 | { |
98 | uuid = index; | 98 | uuid = index; |
99 | entered = DateTime.Now; | 99 | entered = DateTime.UtcNow; |
100 | lastUsed = entered; | 100 | lastUsed = entered; |
101 | expires = ttl; | 101 | expires = ttl; |
102 | } | 102 | } |
@@ -215,6 +215,8 @@ namespace OpenSim.Framework | |||
215 | private CacheFlags m_Flags = 0; | 215 | private CacheFlags m_Flags = 0; |
216 | private int m_Size = 1024; | 216 | private int m_Size = 1024; |
217 | private TimeSpan m_DefaultTTL = new TimeSpan(0); | 217 | private TimeSpan m_DefaultTTL = new TimeSpan(0); |
218 | private DateTime m_nextExpire; | ||
219 | private TimeSpan m_expiresTime = new TimeSpan(0,0,30); | ||
218 | public ExpireDelegate OnExpire; | 220 | public ExpireDelegate OnExpire; |
219 | 221 | ||
220 | // Comparison interfaces | 222 | // Comparison interfaces |
@@ -233,6 +235,21 @@ namespace OpenSim.Framework | |||
233 | return(a.lastUsed.CompareTo(b.lastUsed)); | 235 | return(a.lastUsed.CompareTo(b.lastUsed)); |
234 | } | 236 | } |
235 | } | 237 | } |
238 | // same as above, reverse order | ||
239 | private class SortLRUrev : IComparer<CacheItemBase> | ||
240 | { | ||
241 | public int Compare(CacheItemBase a, CacheItemBase b) | ||
242 | { | ||
243 | if (a == null && b == null) | ||
244 | return 0; | ||
245 | if (a == null) | ||
246 | return -1; | ||
247 | if (b == null) | ||
248 | return 1; | ||
249 | |||
250 | return(b.lastUsed.CompareTo(a.lastUsed)); | ||
251 | } | ||
252 | } | ||
236 | 253 | ||
237 | // Convenience constructors | 254 | // Convenience constructors |
238 | // | 255 | // |
@@ -241,6 +258,8 @@ namespace OpenSim.Framework | |||
241 | m_Strategy = CacheStrategy.Balanced; | 258 | m_Strategy = CacheStrategy.Balanced; |
242 | m_Medium = CacheMedium.Memory; | 259 | m_Medium = CacheMedium.Memory; |
243 | m_Flags = 0; | 260 | m_Flags = 0; |
261 | m_nextExpire = DateTime.UtcNow + m_expiresTime; | ||
262 | m_Strategy = CacheStrategy.Aggressive; | ||
244 | } | 263 | } |
245 | 264 | ||
246 | public Cache(CacheMedium medium) : | 265 | public Cache(CacheMedium medium) : |
@@ -295,19 +314,23 @@ namespace OpenSim.Framework | |||
295 | { | 314 | { |
296 | lock (m_Index) | 315 | lock (m_Index) |
297 | { | 316 | { |
298 | if (Count <= Size) | 317 | int target = newSize; |
299 | return; | 318 | if(m_Strategy == CacheStrategy.Aggressive) |
319 | target = (int)(newSize * 0.9); | ||
300 | 320 | ||
301 | m_Index.Sort(new SortLRU()); | 321 | if(Count > target) |
302 | m_Index.Reverse(); | 322 | { |
323 | m_Index.Sort(new SortLRUrev()); | ||
303 | 324 | ||
304 | m_Index.RemoveRange(newSize, Count - newSize); | 325 | m_Index.RemoveRange(newSize, Count - target); |
305 | m_Size = newSize; | ||
306 | 326 | ||
307 | m_Lookup.Clear(); | 327 | m_Lookup.Clear(); |
328 | |||
329 | foreach (CacheItemBase item in m_Index) | ||
330 | m_Lookup[item.uuid] = item; | ||
331 | } | ||
332 | m_Size = newSize; | ||
308 | 333 | ||
309 | foreach (CacheItemBase item in m_Index) | ||
310 | m_Lookup[item.uuid] = item; | ||
311 | } | 334 | } |
312 | } | 335 | } |
313 | 336 | ||
@@ -333,10 +356,10 @@ namespace OpenSim.Framework | |||
333 | Expire(true); | 356 | Expire(true); |
334 | return null; | 357 | return null; |
335 | } | 358 | } |
336 | 359 | ||
337 | item.hits++; | 360 | item.hits++; |
338 | item.lastUsed = DateTime.Now; | 361 | item.lastUsed = DateTime.UtcNow; |
339 | 362 | ||
340 | Expire(true); | 363 | Expire(true); |
341 | } | 364 | } |
342 | 365 | ||
@@ -361,14 +384,15 @@ namespace OpenSim.Framework | |||
361 | // | 384 | // |
362 | public virtual Object Get(string index, FetchDelegate fetch) | 385 | public virtual Object Get(string index, FetchDelegate fetch) |
363 | { | 386 | { |
364 | Object item = Get(index); | 387 | CacheItemBase item = GetItem(index); |
365 | if (item != null) | 388 | if (item != null) |
366 | return item; | 389 | return item.Retrieve(); |
367 | 390 | ||
368 | Object data = fetch(index); | 391 | Object data = fetch(index); |
392 | |||
369 | if (data == null) | 393 | if (data == null) |
370 | { | 394 | { |
371 | if ((m_Flags & CacheFlags.CacheMissing) != 0) | 395 | if((m_Flags & CacheFlags.CacheMissing) != 0) |
372 | { | 396 | { |
373 | lock (m_Index) | 397 | lock (m_Index) |
374 | { | 398 | { |
@@ -384,7 +408,6 @@ namespace OpenSim.Framework | |||
384 | } | 408 | } |
385 | 409 | ||
386 | Store(index, data); | 410 | Store(index, data); |
387 | |||
388 | return data; | 411 | return data; |
389 | } | 412 | } |
390 | 413 | ||
@@ -442,9 +465,9 @@ namespace OpenSim.Framework | |||
442 | item = GetItem(index); | 465 | item = GetItem(index); |
443 | 466 | ||
444 | item.hits++; | 467 | item.hits++; |
445 | item.lastUsed = DateTime.Now; | 468 | item.lastUsed = DateTime.UtcNow; |
446 | if (m_DefaultTTL.Ticks != 0) | 469 | if (m_DefaultTTL.Ticks != 0) |
447 | item.expires = DateTime.Now + m_DefaultTTL; | 470 | item.expires = DateTime.UtcNow + m_DefaultTTL; |
448 | 471 | ||
449 | item.Store(data); | 472 | item.Store(data); |
450 | } | 473 | } |
@@ -455,7 +478,7 @@ namespace OpenSim.Framework | |||
455 | parameters); | 478 | parameters); |
456 | 479 | ||
457 | if (m_DefaultTTL.Ticks != 0) | 480 | if (m_DefaultTTL.Ticks != 0) |
458 | item.expires = DateTime.Now + m_DefaultTTL; | 481 | item.expires = DateTime.UtcNow + m_DefaultTTL; |
459 | 482 | ||
460 | m_Index.Add(item); | 483 | m_Index.Add(item); |
461 | m_Lookup[index] = item; | 484 | m_Lookup[index] = item; |
@@ -476,10 +499,14 @@ namespace OpenSim.Framework | |||
476 | if (getting && (m_Strategy == CacheStrategy.Aggressive)) | 499 | if (getting && (m_Strategy == CacheStrategy.Aggressive)) |
477 | return; | 500 | return; |
478 | 501 | ||
502 | DateTime now = DateTime.UtcNow; | ||
503 | if(now < m_nextExpire) | ||
504 | return; | ||
505 | |||
506 | m_nextExpire = now + m_expiresTime; | ||
507 | |||
479 | if (m_DefaultTTL.Ticks != 0) | 508 | if (m_DefaultTTL.Ticks != 0) |
480 | { | 509 | { |
481 | DateTime now= DateTime.Now; | ||
482 | |||
483 | foreach (CacheItemBase item in new List<CacheItemBase>(m_Index)) | 510 | foreach (CacheItemBase item in new List<CacheItemBase>(m_Index)) |
484 | { | 511 | { |
485 | if (item.expires.Ticks == 0 || | 512 | if (item.expires.Ticks == 0 || |
@@ -494,15 +521,13 @@ namespace OpenSim.Framework | |||
494 | switch (m_Strategy) | 521 | switch (m_Strategy) |
495 | { | 522 | { |
496 | case CacheStrategy.Aggressive: | 523 | case CacheStrategy.Aggressive: |
497 | if (Count < Size) | 524 | int target = (int)((float)Size * 0.9); |
525 | if (Count < target) // Cover ridiculous cache sizes | ||
498 | return; | 526 | return; |
499 | 527 | ||
500 | m_Index.Sort(new SortLRU()); | 528 | target = (int)((float)Size * 0.8); |
501 | m_Index.Reverse(); | ||
502 | 529 | ||
503 | int target = (int)((float)Size * 0.9); | 530 | m_Index.Sort(new SortLRUrev()); |
504 | if (target == Count) // Cover ridiculous cache sizes | ||
505 | return; | ||
506 | 531 | ||
507 | ExpireDelegate doExpire = OnExpire; | 532 | ExpireDelegate doExpire = OnExpire; |
508 | 533 | ||