XyzLruCache<T>¶
Namespace: ThinkGeo.Core
Represents an in-memory, tile-indexed least-recently-used cache. Thread-safe via a single lock protecting both map and LRU list.
public class XyzLruCache<T>
Type Parameters¶
T
Inheritance object → XyzLruCache<T>
Properties¶
Capacity¶
Gets or sets the maximum number of items that can be stored in the cache. Setting it trims the cache immediately if needed.
public int Capacity { get; set; }
Property Value¶
PreferredRetentionCapacity¶
Maximum number of entries the Preferred bucket holds before its excess starts competing with the Normal bucket for eviction. Entries within this count are evicted only after the Normal bucket is empty and the Preferred over-quota tail has been drained.
public int PreferredRetentionCapacity { get; set; }
Property Value¶
Exceptions¶
ArgumentOutOfRangeException
Thrown when set to a negative value.
Remarks:
Defaults to int.MaxValue, which protects the entire Preferred bucket as long as the Normal bucket is non-empty. Setting this property immediately trims the cache if the new value is exceeded. Thread-safe. Priority assignment itself is controlled via XyzLruCache<T>.UpdatePriorities(IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>); changing this property only adjusts the protection budget.
The sum of XyzLruCache<T>.PreferredRetentionCapacity, XyzLruCache<T>.AnchorRetentionCapacity, and XyzLruCache<T>.CriticalRetentionCapacity should typically be kept below XyzLruCache<T>.Capacity; otherwise the Normal bucket can be evicted faster than it fills.
AnchorRetentionCapacity¶
Maximum number of entries the Anchor bucket holds before its excess starts competing with the Normal bucket for eviction. Anchor is stickier than Preferred but less sticky than Critical: it is evicted only after the Normal bucket is empty and the Preferred bucket has been drained down to its own retention capacity.
public int AnchorRetentionCapacity { get; set; }
Property Value¶
Exceptions¶
ArgumentOutOfRangeException
Thrown when set to a negative value.
Remarks:
Defaults to int.MaxValue, which protects the entire Anchor bucket as long as Normal and over-quota Preferred entries exist. Setting this property immediately trims the cache if the new value is exceeded. Thread-safe. Priority assignment itself is controlled via XyzLruCache<T>.UpdatePriorities(IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>); changing this property only adjusts the protection budget.
CriticalRetentionCapacity¶
Maximum number of entries the Critical bucket holds before its excess starts competing with the Normal bucket for eviction. Critical is the stickiest tier: it is evicted only after Normal, Preferred, and Anchor over-quota tails have been drained.
public int CriticalRetentionCapacity { get; set; }
Property Value¶
Exceptions¶
ArgumentOutOfRangeException
Thrown when set to a negative value.
Remarks:
Defaults to int.MaxValue, which protects the entire Critical bucket as long as lower-priority over-quota entries exist. Setting this property immediately trims the cache if the new value is exceeded. Thread-safe. Priority assignment itself is controlled via XyzLruCache<T>.UpdatePriorities(IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>); changing this property only adjusts the protection budget.
The sum of XyzLruCache<T>.PreferredRetentionCapacity, XyzLruCache<T>.AnchorRetentionCapacity, and XyzLruCache<T>.CriticalRetentionCapacity should typically be kept below XyzLruCache<T>.Capacity; otherwise the Normal bucket can be evicted faster than it fills.
Constructors¶
XyzLruCache(int)¶
Initializes a new instance of the XyzLruCache<T> class.
public XyzLruCache(int capacity)
Parameters¶
capacity int
The maximum number of cached items.
Methods¶
TryGet(int, long, long, T&)¶
Tries to get a tile from the cache. If found, updates LRU order (MRU).
public bool TryGet(int zoom, long x, long y, T& value)
Parameters¶
zoom int
x long
y long
value T&
Returns¶
Get(int, long, long)¶
Gets a tile from the cache, returning default(T) if it does not exist.
public T Get(int zoom, long x, long y)
Parameters¶
zoom int
x long
y long
Returns¶
T
Add(int, long, long, T)¶
Adds or updates the given content in the cache, moving it to MRU. If capacity is exceeded, evicts LRU items.
public void Add(int zoom, long x, long y, T content)
Parameters¶
zoom int
x long
y long
content T
Returns¶
Remove(int, long, long)¶
Removes the specified tile from the cache, if it exists.
public bool Remove(int zoom, long x, long y)
Parameters¶
zoom int
x long
y long
Returns¶
UpdatePriorities(IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>, IEnumerable<ValueTuple<int, long, long>>)¶
Replaces the set of keys whose entries are protected from eviction. Existing entries are reclassified into the matching bucket; entries whose keys are not present in any collection are returned to the Normal bucket. The provided collections are also retained internally, so any future XyzLruCache<T>.Add(int, long, long, T) with a matching key is placed into the corresponding bucket automatically.
public void UpdatePriorities(IEnumerable<ValueTuple<int, long, long>> preferredKeys, IEnumerable<ValueTuple<int, long, long>> criticalKeys, IEnumerable<ValueTuple<int, long, long>> anchorKeys)
Parameters¶
preferredKeys IEnumerable<ValueTuple<int, long, long>>
Keys to mark as Preferred, or null to clear the Preferred set.
criticalKeys IEnumerable<ValueTuple<int, long, long>>
Keys to mark as Critical, or null to clear the Critical set.
anchorKeys IEnumerable<ValueTuple<int, long, long>>
Keys to mark as Anchor, or null to clear the Anchor set. Defaults to null for backward compatibility with two-tier callers.
Returns¶
Remarks:
This is a full replacement, not a merge: pass null or an empty collection to clear that bucket. Keys appearing in multiple collections follow the priority order Critical > Anchor > Preferred (the stickier classification wins). After reclassification, the cache is trimmed back to XyzLruCache<T>.Capacity using the configured retention budgets.
Thread-safe. Cost is linear in the cache size plus, for each transitioning entry, linear in the destination bucket size — large all-cache reclassifications are not free. Callers typically invoke this once per viewport change rather than per tile.
Clear()¶
Removes every cached entry, resets the LRU order, clears the Preferred, Anchor, and Critical key sets, and restores XyzLruCache<T>.PreferredRetentionCapacity, XyzLruCache<T>.AnchorRetentionCapacity, and XyzLruCache<T>.CriticalRetentionCapacity to their defaults (int.MaxValue).
public void Clear()