Appearance
Cache
JASE.Cache is the primary entry point for JASE’s in-process memory caching. It wraps a single internal cache instance and exposes a small, consistent API for:
- Permanent entries (no expiration)
- Timebound entries (absolute or sliding expiration)
- File-dependent entries (auto-invalidated when a file changes)
- Optional size limit + eviction tracking
This cache is process-local. App restart clears everything.
Architecture
Cache is a static facade over an internal cache instance:
- Backing implementation:
JASE.Caching.InMemoryCache - Interface:
JASE.Interfaces.ICache - Default expiration: 5 minutes (
DefaultTimeSpan)
Key behaviors
Copy-on-read (default)
Most read methods default doCopy = true.
That means values are copied on return via ReflectionTools.CopyObject() to reduce accidental shared-mutable-state bugs.
Use doCopy: false if:
- you’re caching immutable types, or
- you want maximum performance, or
- your type can’t be safely copied.
csharp
MyType value = Cache.Get<MyType>(key, doCopy: false);Size limit and Statistics
If JASE is configured with a memory cache size limit (via CoreConfig.ReservedMemoryCacheBytes), the underlying cache enables size tracking and eviction statistics.
Use:
- HasSizeLimit
- Stats
- CapacityEvictions
Common patterns
Generate deterministic cache keys
GenerateCacheKey(prefix, parts...) hashes inputs into a stable key.
csharp
string key = Cache.GenerateCacheKey("user-profile", userId, includeInactive);If a part has a ToJson() method returning string, it will be used; otherwise ToString() is used (and Type uses FullName).
Read-through caching (sync)
csharp
UserProfile profile = Cache.GetOrInsert(
key,
factory: () => LoadUserProfile(userId)
);Read-through caching with expiration
csharp
UserProfile profile = Cache.GetOrInsert(
key,
factory: () => LoadUserProfile(userId),
expiration: TimeSpan.FromMinutes(10),
sliding: true
);- sliding: true refreshes the expiration window on access
- sliding: false is absolute expiration
Async factory + cancellation
csharp
UserProfile profile = await Cache.GetOrInsertAsync(
key,
factory: async ct => await LoadUserProfileAsync(userId, ct),
expiration: TimeSpan.FromMinutes(10),
sliding: true,
ct: cancellationToken
);File-dependent caching (auto-invalidate)
Cache an item that should be invalidated when a file changes (write/delete).
csharp
Config cfg = Cache.GetOrInsert(
key: "app-config",
filename: @"C:\app\config.json",
factory: () => LoadConfig()
);When the file changes, the cache key is removed automatically.
API
Properties
Attempts– total cache lookup attemptsHits– successful hitsEvictions– evictions (expired + capacity)CapacityEvictions– evicted due to size limit pressureCount– current item countHasSizeLimit– whether size limiting is enabledDefaultTimeSpan– default expiration (5 minutes)
Methods
GenerateCacheKey(prefix, parts...)Get(key, doCopy)Get<T>(key, doCopy)TryGetValue(key, out value, doCopy)TryGetValue<T>(key, out value, doCopy)Insert(key, item, size)Insert(key, item, expiration, sliding, size)Insert(key, item, filename, size)GetOrInsert(...)overloadsGetOrInsertAsync(...)overloadsRemove(key)GetListKeys()GetVersion()
When not to use this cache
- This cache is not distributed and not persistent. Don’t use it for:
- Cross-instance caching (load-balanced services)
- Long-lived persistence needs
- Sharing data across machines/processes