Table of Contents

SlimMemoryCache Class

Definition

Namespace
Cuemon.Runtime.Caching
Assemblies
Cuemon.Runtime.Caching.dll
Source
src/Cuemon.Runtime.Caching/SlimMemoryCache.cs

Represents the type that implements an in-memory cache for an application.

public class SlimMemoryCache : Disposable, IDisposable, ICacheEnumerable<long>, IEnumerable<KeyValuePair<long, CacheEntry>>, IEnumerable
Inheritance
SlimMemoryCache
Implements
Inherited Members

Examples

SlimMemoryCache provides in-memory caching with absolute and sliding expiration, namespace grouping, and automatic cleanup. This example creates a cache with cleanup enabled (FirstSweep = 30s, SucceedingSweep = 10s), adds a config entry with absolute expiration, a session entry with sliding expiration under the "sessions" namespace, and updates the config entry via the indexer. It demonstrates safe retrieval with TryGet, key-based removal with Remove, namespace-based counting with Count("data"), and bulk namespace removal with RemoveAll("data"). Console output confirms the cached values, update status, removal success, and namespace counts before and after removal.

using System;
using Cuemon.Runtime.Caching;

namespace MyApp.Examples;

public class Example
{
    public void Run()
    {

        // Create a cache with automatic cleanup
        var cache = new SlimMemoryCache(o =>
        {
            o.EnableCleanup = true;
            o.FirstSweep = TimeSpan.FromSeconds(30);
            o.SucceedingSweep = TimeSpan.FromSeconds(10);
        });

        // Add an entry with absolute expiration
        cache.Add("config", new { Theme = "Dark", Locale = "en-US" }, DateTime.UtcNow.AddMinutes(5));

        // Add an entry with sliding expiration (resets on each access)
        cache.Add("session", new { UserId = 42, Role = "Admin" }, TimeSpan.FromMinutes(20), "sessions");

        Console.WriteLine($"Config: {cache["config"]}");

        // Use TryGet for safe retrieval
        if (cache.TryGet("session", "sessions", out var session))
        {
            Console.WriteLine($"Session: {session}");

        // Update an existing entry using the indexer
        cache["config"] = new { Theme = "Light", Locale = "en-US" };
        Console.WriteLine($"Updated config: {cache["config"]}");

        // Check if entry exists and remove it
        if (cache.Contains("config"))
        {
            cache.Remove("config");
            Console.WriteLine("Config removed.");

        // Count entries within a namespace
        cache.Add("item1", 100, DateTime.MaxValue, "data");
        cache.Add("item2", 200, DateTime.MaxValue, "data");
        Console.WriteLine($"Items in 'data' namespace: {cache.Count("data")}");

        // Remove all entries from a namespace
        cache.RemoveAll("data");
        Console.WriteLine($"After removal: {cache.Count("data")}");

        cache.Dispose();

}}}
}

Constructors

Name Description
SlimMemoryCache(Action<SlimMemoryCacheOptions>)

Initializes a new instance of the SlimMemoryCache class.

Properties

Name Description
this[string, string]

Gets or sets a value in the cache by using the default indexer property for an instance of the SlimMemoryCache class.

KeyProvider

Gets the function delegate that is responsible for providing a unique identifier for the cache entry.

Methods

Name Description
Add(CacheEntry, CacheInvalidation)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

Add(string, object, IDependency, string)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

Add(string, object, IEnumerable<IDependency>, string)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

Add(string, object, DateTime, string)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

Add(string, object, TimeSpan, string)

Inserts a cache entry into the cache and adds details about how the entry should be evicted.

Contains(string, string)

Determines whether a cache entry exists in the cache.

Count(string)

Gets the number of entries associated with the ns contained in the cache.

Get(string, string)

Returns an entry from the cache.

GetCacheEntry(string, string)

Returns an entry from the cache as a CacheEntry instance.

GetEnumerator()

Returns an enumerator that iterates through the collection.

OnDisposeManagedResources()

Called when this object is being disposed by either Dispose() or Dispose(bool) having disposing set to true and Disposed is false.

Remove(string, string)

Removes a cache entry from the cache.

RemoveAll(string)

Removes all entries associated with the ns contained in the cache.

Set(string, object, CacheInvalidation, string)

Inserts a cache entry into the cache.

TryGet(string, out object)

Attempts to get the value associated with the specified key from the cache.

TryGet(string, string, out object)

Attempts to get the value associated with the specified key and ns from the cache.

TryGetCacheEntry(string, out CacheEntry)

Attempts to get the CacheEntry associated with the specified key from the cache.

TryGetCacheEntry(string, string, out CacheEntry)

Attempts to get the CacheEntry associated with the specified key and ns from the cache.

See Also