PHP code example of phillarmonic / staccache-bundle
1. Go to this page and download the library: Download phillarmonic/staccache-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Phillarmonic\StaccacheBundle\Attribute\Staccacheable;
#[ORM\Entity]
#[Staccacheable(ttl: 1800)] // Cache for 30 minutes
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
// Getters and setters...
}
namespace App\Repository;
use App\Entity\Product;
use Doctrine\Persistence\ManagerRegistry;
use Phillarmonic\StaccacheBundle\Cache\EntityCacheManager;
use Phillarmonic\StaccacheBundle\Cache\QueryCacheManager;
use Phillarmonic\StaccacheBundle\Repository\CachedServiceEntityRepository;
class ProductRepository extends CachedServiceEntityRepository
{
public function __construct(
ManagerRegistry $registry,
EntityCacheManager $cacheManager,
QueryCacheManager $queryCacheManager
) {
parent::__construct($registry, Product::class, $cacheManager, $queryCacheManager);
}
/**
* Find products by category with cache
*/
public function findByCategoryWithCache(string $category): array
{
// Create a query
$queryBuilder = $this->createQueryBuilder('p')
->where('p.category = :category')
->setParameter('category', $category);
// Execute with cache (key must be unique for this query)
return $this->executeQueryWithCache(
$queryBuilder,
'products_by_category_' . $category,
3600 // Cache for 1 hour
);
}
/**
* Example of bypassing cache for a specific operation
*/
public function findFreshProduct(int $id): ?Product
{
return $this->withoutCache()->find($id);
}
}
namespace App\Controller;
use App\Entity\Product;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class ProductController extends AbstractController
{
#[Route('/product/{id}', name: 'app_product_show')]
public function show(Product $product): Response
{
// The product is loaded from cache if available
return $this->render('product/show.html.twig', [
'product' => $product,
]);
}
}
// Bypass entity cache for a single operation
$repository->withoutCache()->find($id);
// Bypass collection cache
$repository->withoutCollectionCache()->findAll();
// Bypass query cache
$repository->withoutQueryCache()->executeQueryWithCache($query, 'cache_key');
namespace App\Service;
use App\Entity\Product;
use Phillarmonic\StaccacheBundle\Cache\EntityCacheManager;
use Phillarmonic\StaccacheBundle\Cache\QueryCacheManager;
class ProductService
{
private EntityCacheManager $entityCacheManager;
private QueryCacheManager $queryCacheManager;
public function __construct(
EntityCacheManager $entityCacheManager,
QueryCacheManager $queryCacheManager
) {
$this->entityCacheManager = $entityCacheManager;
$this->queryCacheManager = $queryCacheManager;
}
public function updateProduct(Product $product): void
{
// Update product...
// Manually update the cache
$this->entityCacheManager->cacheEntity($product);
// Invalidate related collection caches
$this->entityCacheManager->invalidateCollectionCaches(Product::class);
// Invalidate specific query cache
$this->queryCacheManager->invalidateQueryCache('featured_products', Product::class);
}
}
public function update(Request $request, Product $product, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(ProductType::class, $product);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
// The cache is automatically updated by the FormSubmitCacheListener
return $this->redirectToRoute('app_product_show', ['id' => $product->getId()]);
}
// ...
}
$cacheManager->invalidateCache($entity);
// Track cache hits/misses
$entity = $cacheManager->getFromCache($entityClass, $id);
if ($entity !== null) {
// Cache hit
$metrics->increment('cache.hit');
} else {
// Cache miss
$metrics->increment('cache.miss');
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.