CodeIgniter v4.7 API

Cache
in package
implements RouteAttributeInterface

Cache Attribute

Caches the response of a controller method at the server level for a specified duration. This is server-side caching to avoid expensive operations, not browser-level caching.

Usage:

#[Cache(for: 3600)] // Cache for 1 hour
#[Cache(for: 300, key: 'custom_key')] // Cache with custom key

Limitations:

  • Only caches GET requests; POST, PUT, DELETE, and other methods are ignored
  • Streaming responses or file downloads may not cache properly
  • Cache key includes HTTP method, path, query string, and possibly user_id(), but not request headers
  • Does not automatically invalidate related cache entries
  • Cookies set in the response are cached and reused for all subsequent requests
  • Large responses may impact cache storage performance
  • Browser Cache-Control headers do not affect server-side caching behavior

Security Considerations:

  • Ensure cache backend is properly secured and not accessible publicly
  • Be aware that authorization checks happen before cache lookup
Attributes
#[Attribute]
\Attribute::TARGET_METHOD

Table of Contents

Interfaces

RouteAttributeInterface

Properties

$for  : int
$key  : string|null

Methods

__construct()  : mixed
after()  : ResponseInterface|null
Process the attribute after the controller is executed.
before()  : RequestInterface|ResponseInterface|null
Process the attribute before the controller is executed.
generateCacheKey()  : string

Properties

$key

public string|null $key = null

Methods

__construct()

public __construct([int $for = 3600 ][, string|null $key = null ]) : mixed
Parameters
$for : int = 3600
$key : string|null = null

        
On this page

Search results