diff options
Diffstat (limited to 'OpenSim/Framework/Cache.cs')
-rw-r--r-- | OpenSim/Framework/Cache.cs | 83 |
1 files changed, 30 insertions, 53 deletions
diff --git a/OpenSim/Framework/Cache.cs b/OpenSim/Framework/Cache.cs index 31cab4a..79e20fc 100644 --- a/OpenSim/Framework/Cache.cs +++ b/OpenSim/Framework/Cache.cs | |||
@@ -199,14 +199,7 @@ namespace OpenSim.Framework | |||
199 | // | 199 | // |
200 | public class Cache | 200 | public class Cache |
201 | { | 201 | { |
202 | /// <summary> | ||
203 | /// Must only be accessed under lock. | ||
204 | /// </summary> | ||
205 | private List<CacheItemBase> m_Index = new List<CacheItemBase>(); | 202 | private List<CacheItemBase> m_Index = new List<CacheItemBase>(); |
206 | |||
207 | /// <summary> | ||
208 | /// Must only be accessed under m_Index lock. | ||
209 | /// </summary> | ||
210 | private Dictionary<string, CacheItemBase> m_Lookup = | 203 | private Dictionary<string, CacheItemBase> m_Lookup = |
211 | new Dictionary<string, CacheItemBase>(); | 204 | new Dictionary<string, CacheItemBase>(); |
212 | 205 | ||
@@ -327,19 +320,19 @@ namespace OpenSim.Framework | |||
327 | { | 320 | { |
328 | if (m_Lookup.ContainsKey(index)) | 321 | if (m_Lookup.ContainsKey(index)) |
329 | item = m_Lookup[index]; | 322 | item = m_Lookup[index]; |
323 | } | ||
330 | 324 | ||
331 | if (item == null) | 325 | if (item == null) |
332 | { | 326 | { |
333 | Expire(true); | ||
334 | return null; | ||
335 | } | ||
336 | |||
337 | item.hits++; | ||
338 | item.lastUsed = DateTime.Now; | ||
339 | |||
340 | Expire(true); | 327 | Expire(true); |
328 | return null; | ||
341 | } | 329 | } |
342 | 330 | ||
331 | item.hits++; | ||
332 | item.lastUsed = DateTime.Now; | ||
333 | |||
334 | Expire(true); | ||
335 | |||
343 | return item; | 336 | return item; |
344 | } | 337 | } |
345 | 338 | ||
@@ -392,10 +385,7 @@ namespace OpenSim.Framework | |||
392 | // | 385 | // |
393 | public Object Find(Predicate<CacheItemBase> d) | 386 | public Object Find(Predicate<CacheItemBase> d) |
394 | { | 387 | { |
395 | CacheItemBase item; | 388 | CacheItemBase item = m_Index.Find(d); |
396 | |||
397 | lock (m_Index) | ||
398 | item = m_Index.Find(d); | ||
399 | 389 | ||
400 | if (item == null) | 390 | if (item == null) |
401 | return null; | 391 | return null; |
@@ -429,12 +419,12 @@ namespace OpenSim.Framework | |||
429 | public virtual void Store(string index, Object data, Type container, | 419 | public virtual void Store(string index, Object data, Type container, |
430 | Object[] parameters) | 420 | Object[] parameters) |
431 | { | 421 | { |
422 | Expire(false); | ||
423 | |||
432 | CacheItemBase item; | 424 | CacheItemBase item; |
433 | 425 | ||
434 | lock (m_Index) | 426 | lock (m_Index) |
435 | { | 427 | { |
436 | Expire(false); | ||
437 | |||
438 | if (m_Index.Contains(new CacheItemBase(index))) | 428 | if (m_Index.Contains(new CacheItemBase(index))) |
439 | { | 429 | { |
440 | if ((m_Flags & CacheFlags.AllowUpdate) != 0) | 430 | if ((m_Flags & CacheFlags.AllowUpdate) != 0) |
@@ -460,17 +450,9 @@ namespace OpenSim.Framework | |||
460 | m_Index.Add(item); | 450 | m_Index.Add(item); |
461 | m_Lookup[index] = item; | 451 | m_Lookup[index] = item; |
462 | } | 452 | } |
463 | |||
464 | item.Store(data); | 453 | item.Store(data); |
465 | } | 454 | } |
466 | 455 | ||
467 | /// <summary> | ||
468 | /// Expire items as appropriate. | ||
469 | /// </summary> | ||
470 | /// <remarks> | ||
471 | /// Callers must lock m_Index. | ||
472 | /// </remarks> | ||
473 | /// <param name='getting'></param> | ||
474 | protected virtual void Expire(bool getting) | 456 | protected virtual void Expire(bool getting) |
475 | { | 457 | { |
476 | if (getting && (m_Strategy == CacheStrategy.Aggressive)) | 458 | if (getting && (m_Strategy == CacheStrategy.Aggressive)) |
@@ -493,10 +475,12 @@ namespace OpenSim.Framework | |||
493 | 475 | ||
494 | switch (m_Strategy) | 476 | switch (m_Strategy) |
495 | { | 477 | { |
496 | case CacheStrategy.Aggressive: | 478 | case CacheStrategy.Aggressive: |
497 | if (Count < Size) | 479 | if (Count < Size) |
498 | return; | 480 | return; |
499 | 481 | ||
482 | lock (m_Index) | ||
483 | { | ||
500 | m_Index.Sort(new SortLRU()); | 484 | m_Index.Sort(new SortLRU()); |
501 | m_Index.Reverse(); | 485 | m_Index.Reverse(); |
502 | 486 | ||
@@ -506,7 +490,7 @@ namespace OpenSim.Framework | |||
506 | 490 | ||
507 | ExpireDelegate doExpire = OnExpire; | 491 | ExpireDelegate doExpire = OnExpire; |
508 | 492 | ||
509 | if (doExpire != null) | 493 | if (doExpire != null) |
510 | { | 494 | { |
511 | List<CacheItemBase> candidates = | 495 | List<CacheItemBase> candidates = |
512 | m_Index.GetRange(target, Count - target); | 496 | m_Index.GetRange(target, Count - target); |
@@ -529,34 +513,27 @@ namespace OpenSim.Framework | |||
529 | foreach (CacheItemBase item in m_Index) | 513 | foreach (CacheItemBase item in m_Index) |
530 | m_Lookup[item.uuid] = item; | 514 | m_Lookup[item.uuid] = item; |
531 | } | 515 | } |
532 | 516 | } | |
533 | break; | 517 | break; |
534 | 518 | default: | |
535 | default: | 519 | break; |
536 | break; | ||
537 | } | 520 | } |
538 | } | 521 | } |
539 | 522 | ||
540 | public void Invalidate(string uuid) | 523 | public void Invalidate(string uuid) |
541 | { | 524 | { |
542 | lock (m_Index) | 525 | if (!m_Lookup.ContainsKey(uuid)) |
543 | { | 526 | return; |
544 | if (!m_Lookup.ContainsKey(uuid)) | ||
545 | return; | ||
546 | 527 | ||
547 | CacheItemBase item = m_Lookup[uuid]; | 528 | CacheItemBase item = m_Lookup[uuid]; |
548 | m_Lookup.Remove(uuid); | 529 | m_Lookup.Remove(uuid); |
549 | m_Index.Remove(item); | 530 | m_Index.Remove(item); |
550 | } | ||
551 | } | 531 | } |
552 | 532 | ||
553 | public void Clear() | 533 | public void Clear() |
554 | { | 534 | { |
555 | lock (m_Index) | 535 | m_Index.Clear(); |
556 | { | 536 | m_Lookup.Clear(); |
557 | m_Index.Clear(); | ||
558 | m_Lookup.Clear(); | ||
559 | } | ||
560 | } | 537 | } |
561 | } | 538 | } |
562 | } \ No newline at end of file | 539 | } |