Cache Migration

This sample demonstrates the CacheShim that provides Web Forms-compatible Cache["key"] access in Blazor, backed by ASP.NET Core IMemoryCache.


1. Setting & Getting Cache Values

In Web Forms, you cached data with HttpRuntime.Cache["key"] = value. The BWFC shim supports the identical pattern.

Before (Web Forms)

// Store a value
HttpRuntime.Cache["UserName"] = txtName.Text;

// Retrieve a value
var name = (string)Cache["UserName"];

After (Blazor with BWFC)

// Same pattern!
Cache["UserName"] = userName;

// Retrieve a value
var name = (string)Cache["UserName"];
Live Demo - Set & Get

Retrieved value: (empty)


2. Type-Safe Access with Get<T>()

The shim provides Cache.Get<T>(key) for strongly-typed retrieval.

Before (Web Forms)

// Requires manual cast
var count = (int)Cache["HitCount"];
Cache["HitCount"] = count + 1;

After (Blazor with BWFC)

// Type-safe access
var count = Cache.Get<int>("HitCount");
Cache["HitCount"] = count + 1;
Live Demo - Typed Counter

Hit count (stored as int): 0


3. Remove Cache Items

Call Cache.Remove(key) to evict an item, just like in Web Forms. The method returns the removed value.

Live Demo - Remove

Status: (click Store, then Remove, then Check)


4. Sliding Expiration

Cache items can be stored with a sliding expiration, just like Web Forms. Items expire if not accessed within the sliding window.

Before (Web Forms)

Cache.Insert("data", value, null,
    Cache.NoAbsoluteExpiration,
    TimeSpan.FromSeconds(30));

After (Blazor with BWFC)

// Simplified API
Cache.Insert("data", value,
    TimeSpan.FromSeconds(30));
Live Demo - Sliding Expiration

Status: (click Store, wait 5+ seconds, then Check)