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

react/cache

ReactPHP async cache component with Promise-based CacheInterface and an in-memory ArrayCache. Inspired by PSR-16 but designed for non-blocking apps. Supports get/set/delete, bulk operations, clear, has, and common fallback patterns.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer (composer require react/cache:^3@dev) and using the provided ArrayCache implementation for simple in-memory caching. The core concept is the CacheInterface, which defines async, promise-returning methods like get(), set(), delete(), and their batch variants (getMultiple(), setMultiple(), deleteMultiple()). Begin with basic usage—fetching cached data with fallback logic (e.g., from DB on cache miss)—using promise chaining or async/await (in PHP 8+ with async libraries like amphp/parallel-functions). The README’s "Common usage" section provides immediate patterns: fallback get and fallback get-and-set are the most common first integrations.

Implementation Patterns

  • Fallback Cache Pattern: Use get() followed by conditional fallback to a data source. Chain promises to populate cache automatically on cache miss (set() after get() yields null).
  • Batch Caching: Prefer getMultiple() and setMultiple() for efficient retrieval/persistence of grouped keys—especially useful when hydrating DTOs, user preferences, or configuration slices.
  • LRU In-Memory Caching: Use ArrayCache($limit) for short-lived, per-request or per-worker caches (e.g., within CLI workers or long-running ReactPHP servers), with automatic eviction of least-recently-used items.
  • Integration with Async I/O: When building ReactPHP-based services (e.g., HTTP servers, TCP proxies), leverage react/cache to wrap database or external API calls, avoiding blocking while still supporting caching semantics.
  • Type Hinting Against Interface: Inject CacheInterface into services to decouple cache storage—swap in-memory for Redis/ Memcached later (via community implementations like reactphp/redis-cache or custom wrappers).

Gotchas and Tips

  • Monotonic vs Wall-Clock Time: Before PHP 7.3, ArrayCache uses wall-clock time for TTLs, making it prone to premature expiration if the system clock jumps (e.g., via NTP). Prefer PHP ≥ 7.3 for reliable TTLs, or avoid relying on sub-second precision TTLs.
  • has() is Not Safe for Conditional Logic: As noted in the docs, has() can return true just before another process deletes the key—use get() + set() patterns instead for atomic operations.
  • Promise-Based Error Handling: Handle resolution failures explicitly—get() resolves with the $default value on cache miss or error, but network-backed implementations may reject promises (e.g., Redis connection down). Always add ->otherwise() or try/catch if wrapping with async utilities.
  • Batch Method Argument Types: Ensure getMultiple() receives iterable<string>, and setMultiple() receives iterable<string,mixed>—PHP 8+ union types help, but legacy code may need careful normalization.
  • No TTL in delete()/deleteMultiple(): Unlike set(), deletion doesn’t accept a TTL (because deletion is immediate).
  • Maturity Warning: While mature and stable for v1, v3 is still @dev (last release Nov 2022). Consider v1 (^1.2) for production stability until v3 stabilizes. No direct dependents means ecosystem adoption is niche (ReactPHP-focused).
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
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
twbs/bootstrap4
php-http/client-implementation
phpcr/phpcr-implementation
cucumber/gherkin-monorepo
haydenpierce/class-finder
psr/simple-cache-implementation