logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class LruCache<TKey, TValue>

    Least Recently Used (LRU) cache implementation with memory cost tracking.
    Provides efficient O(1) access and eviction based on usage patterns. Not thread-safe.

    Inheritance
    object
    LruCache<TKey, TValue>
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    Namespace: Serenity.AssetPrefetcher.Domain.Services
    Assembly: Serenity.AssetPrefetcher.Domain.dll
    Syntax
    public class LruCache<TKey, TValue>
    Type Parameters
    Name Description
    TKey

    Type of cache keys

    TValue

    Type of cached values

    Constructors

    LruCache()

    Declaration
    public LruCache()

    Properties

    TotalCost

    Total memory cost of all cached items in bytes.

    Declaration
    public int TotalCost { get; }
    Property Value
    Type Description
    int

    The sum of costs of all items currently in the cache

    Methods

    Clear()

    Removes all entries from the cache and resets the total cost to zero.

    Declaration
    public void Clear()

    EnumerateFromOldest()

    Enumerates cache entries from oldest (least recently used) to newest.
    Useful for implementing eviction policies based on usage patterns.

    Declaration
    public IEnumerable<(TKey key, TValue val, int cost)> EnumerateFromOldest()
    Returns
    Type Description
    IEnumerable<(TKey key, TValue val, int cost)>

    Enumerable of cache entries ordered by least recent access

    Put(TKey, TValue, int)

    Adds or updates a value in the cache with the specified memory cost.
    If the key already exists, updates the value and cost, moving it to most recently used.

    Declaration
    public void Put(TKey key, TValue value, int cost)
    Parameters
    Type Name Description
    TKey key

    The cache key

    TValue value

    The value to cache

    int cost

    Memory cost of the cached item in bytes

    Remove(TKey)

    Removes a key-value pair from the cache and updates the total cost.

    Declaration
    public bool Remove(TKey key)
    Parameters
    Type Name Description
    TKey key

    The cache key to remove

    Returns
    Type Description
    bool

    True if the key was found and removed, false otherwise

    TryGet(TKey, out TValue)

    Attempts to retrieve a value from the cache and updates its position to most recently used.

    Declaration
    public bool TryGet(TKey key, out TValue value)
    Parameters
    Type Name Description
    TKey key

    The cache key to look up

    TValue value

    The retrieved value if found, default otherwise

    Returns
    Type Description
    bool

    True if the key was found in cache, false otherwise

    In this article
    © 2026 Serenity. All Rights Reserved