Overview
TheCachingController demonstrates in-memory caching using IMemoryCache for personalized caching and ResponseCache attribute for distributed caching.
Constructor
CachingController
Initializes a new instance of the controller with memory cache support.Memory cache service for storing and retrieving cached data
/workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:10
Methods
Index
Displays the main caching view.Returns the Index view
/workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:14
MemoriaPersonalizada
Stores or retrieves a timestamp in memory cache with customizable expiration time.The cache expiration time in seconds. Defaults to 60 seconds if not provided
Returns the MemoriaPersonalizada view with ViewData containing:
MENSAJE: Indicates whether the date was stored or retrievedFECHA: The cached timestamp in long date and time format
- If “FECHA” is not in cache:
- Creates a new timestamp
- Stores it in cache with absolute expiration based on
tiempoparameter - Sets ViewData with “Fecha almacenada correctamente” message
- If “FECHA” exists in cache:
- Retrieves the cached timestamp
- Sets ViewData with “Fecha recuperada correctamente” message
/workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:19
MemoriaDistribuida
[ResponseCache(Duration = 15, Location = ResponseCacheLocation.Client)] Demonstrates client-side response caching with a 15-second duration.Returns the MemoriaDistribuida view with ViewData containing:
FECHA: Current timestamp in long date and time format
- Generates a current timestamp
- Returns the view which will be cached on the client side for 15 seconds
- The response cache is managed by the framework based on the attribute settings
/workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:42