Skip to main content

Overview

The CachingController 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.
memoryCache
IMemoryCache
required
Memory cache service for storing and retrieving cached data
public CachingController(IMemoryCache memoryCache)
Source: /workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:10

Methods

Index

Displays the main caching view.
public IActionResult Index()
IActionResult
IActionResult
Returns the Index view
Source: /workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:14

MemoriaPersonalizada

Stores or retrieves a timestamp in memory cache with customizable expiration time.
public IActionResult MemoriaPersonalizada(int? tiempo)
tiempo
int?
The cache expiration time in seconds. Defaults to 60 seconds if not provided
IActionResult
IActionResult
Returns the MemoriaPersonalizada view with ViewData containing:
  • MENSAJE: Indicates whether the date was stored or retrieved
  • FECHA: The cached timestamp in long date and time format
Behavior:
  • If “FECHA” is not in cache:
    • Creates a new timestamp
    • Stores it in cache with absolute expiration based on tiempo parameter
    • Sets ViewData with “Fecha almacenada correctamente” message
  • If “FECHA” exists in cache:
    • Retrieves the cached timestamp
    • Sets ViewData with “Fecha recuperada correctamente” message
Source: /workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:19

MemoriaDistribuida

[ResponseCache(Duration = 15, Location = ResponseCacheLocation.Client)] Demonstrates client-side response caching with a 15-second duration.
[ResponseCache(Duration = 15, Location = ResponseCacheLocation.Client)]
public IActionResult MemoriaDistribuida()
IActionResult
IActionResult
Returns the MemoriaDistribuida view with ViewData containing:
  • FECHA: Current timestamp in long date and time format
Behavior:
  • 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
Source: /workspace/source/MvcCoreUtilidades/Controllers/CachingController.cs:42

Build docs developers (and LLMs) love