Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cache Laravel Package

doctrine/cache

Doctrine Cache is a legacy cache component extracted from Doctrine Common, offering multiple cache drivers and interfaces used across Doctrine projects. Note: the library is deprecated and no longer maintained; prefer a PSR-6 or PSR-16 cache instead.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require doctrine/cache. Then, choose a cache driver and instantiate it — for example, ArrayCache for development or RedisCache for production. Wrap your expensive operations with fetch() or save() to cache results. A minimal example:

use Doctrine\Common\Cache\FilesystemCache;

$cache = new FilesystemCache('/path/to/cache/dir');

$result = $cache->fetch('my_key') ?: $cache->save('my_key', expensiveComputation(), 3600);

First place to look: the CacheProvider abstract class and concrete driver classes (e.g., ArrayCache, RedisCache) in the Doctrine\Common\Cache namespace. Use fetch() for safe retrieval (returns null on miss) and save() with TTL — avoid contains() before fetch() unless needed for logic branching.

Implementation Patterns

  • Caching service-layer logic: Wrap repository calls or service methods that query databases or call external APIs. Use descriptive cache keys (e.g., UserRepository::findCachedBy($id)'user:' . $id).
  • Namespacing: Use setNamespace() (e.g., setNamespace('myapp_v1_')) to isolate cache entries per environment or version — prevents stale data when upgrading.
  • Tagging via prefixing: Since doctrine/cache doesn’t support native tags, use consistent key prefixes ('products:by_category:' . $categoryId) and clear entries via deleteMultiple() or key iteration (if driver supports it).
  • Framework integration: Laravel uses this internally for its Cache::store('file'), redis, etc., via illuminate/cache adapters. You can plug it into Symfony or standalone apps using PSR-6 bridge adapters if needed.
  • Debugging hot paths: Wrap frequently used computations (e.g., report generation, config parsing) and log cache hits/misses via a custom Logger decorator.

Gotchas and Tips

  • No built-in tagging or atomic multi-key operations: Simulate tags with prefixes, and clear caches manually (e.g., deleteMultiple(glob($cacheDir . 'products:*')) for filesystem).
  • Filesystem driver pitfalls: On high-concurrency writes, file locks can cause contention. Prefer Redis or APCu for heavy production workloads.
  • delete() vs deleteMultiple(): delete() on many keys one-by-one is slow — batch deletes where possible (e.g., store key list in a master entry and delete both).
  • Serialization matters: Non-serializable objects (e.g., closures, PDO instances) will fail on save. Ensure cached values are scalar or serializable.
  • TTL isn’t enforced strictly: Values may persist beyond expiration until evicted — don’t rely on exact expiry times; use versioned keys for stronger invalidation control.
  • PSR-6/16 compatibility: Doctrine Cache predates PSR standards. Use the DoctrineCommonCachePSR16Adapter (if available) or modern packages like psr/cache directly for new code — Doctrine Cache is in maintenance mode (last release 2022).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport