Files
roi-theme/shared/Infrastructure/Services/WordPressCacheService.php
FrankZamora 0846a3bf03 Migración completa a Clean Architecture con componentes funcionales
- Reorganización de estructura: Admin/, Public/, Shared/, Schemas/
- 12 componentes migrados: TopNotificationBar, Navbar, CtaLetsTalk, Hero,
  FeaturedImage, TableOfContents, CtaBoxSidebar, SocialShare, CtaPost,
  RelatedPost, ContactForm, Footer
- Panel de administración con tabs Bootstrap 5 funcionales
- Schemas JSON para configuración de componentes
- Renderers dinámicos con CSSGeneratorService (cero CSS hardcodeado)
- FormBuilders para UI admin con Design System consistente
- Fix: Bootstrap JS cargado en header para tabs funcionales
- Fix: buildTextInput maneja valores mixed (bool/string)
- Eliminación de estructura legacy (src/, admin/, assets/css/componente-*)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-25 21:20:06 -06:00

125 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace ROITheme\Shared\Infrastructure\Services;
use ROITheme\Shared\Domain\Contracts\CacheServiceInterface;
/**
* WordPressCacheService - Cache con Transients API
*
* RESPONSABILIDAD: Gestionar cache de componentes
*
* IMPLEMENTACIÓN: WordPress Transients
* - get_transient()
* - set_transient()
* - delete_transient()
*
* VENTAJAS:
* - Compatible con object cache (Redis, Memcached)
* - Expiración automática
* - API simple
*
* @package ROITheme\Infrastructure\Services
*/
final class WordPressCacheService implements CacheServiceInterface
{
private const PREFIX = 'roi_theme_';
public function __construct(
private \wpdb $wpdb
) {}
/**
* Obtener valor del cache
*
* @param string $key Clave del cache
* @return mixed|null Valor o null si no existe/expiró
*/
public function get(string $key): mixed
{
$transient = get_transient($this->getFullKey($key));
// WordPress devuelve false si no existe
return $transient === false ? null : $transient;
}
/**
* Guardar valor en cache
*
* @param string $key Clave del cache
* @param mixed $value Valor a guardar
* @param int $expiration Tiempo de vida en segundos (default 1 hora)
* @return bool True si guardó exitosamente
*/
public function set(string $key, mixed $value, int $expiration = 3600): bool
{
return set_transient($this->getFullKey($key), $value, $expiration);
}
/**
* Eliminar entrada de cache
*
* @param string $key Clave del cache
* @return bool True si eliminó exitosamente
*/
public function delete(string $key): bool
{
return $this->invalidate($key);
}
/**
* Limpiar todo el cache
*
* @return bool
*/
public function flush(): bool
{
return $this->invalidateAll();
}
/**
* Invalidar (eliminar) entrada de cache
*
* @param string $key Clave del cache
* @return bool True si eliminó exitosamente
*/
public function invalidate(string $key): bool
{
return delete_transient($this->getFullKey($key));
}
/**
* Invalidar todo el cache de componentes
*
* @return bool
*/
public function invalidateAll(): bool
{
// Obtener todos los componentes
$components = $this->wpdb->get_col(
"SELECT DISTINCT component_name FROM {$this->wpdb->prefix}roi_theme_components"
);
$success = true;
foreach ($components as $componentName) {
$result = $this->invalidate("component_{$componentName}");
$success = $success && $result;
}
return $success;
}
/**
* Obtener clave completa con prefijo
*
* @param string $key
* @return string
*/
private function getFullKey(string $key): string
{
return self::PREFIX . $key;
}
}