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

amphp/cache

Non-blocking cache library for Amp-based PHP apps. Provides async cache interfaces and adapters (e.g., in-memory, filesystem, Redis) with TTL support, atomic operations, and PSR-style ergonomics for high-concurrency services.

View on GitHub
Deep Wiki
Context7

Getting Started

To begin using amphp/cache, first install it via Composer: composer require amphp/cache. Since it's built for asynchronous PHP using Amp and Revolt fibers, ensure your environment supports async execution (e.g., using Revolt’s event loop or Amp’s runner). Start by creating a cache instance:

use Amp\Cache\MemoryCache;
$cache = new MemoryCache(); // or FileCache, RedisCache, etc.

A common first use case is caching the result of an async operation:

$result = $cache->get('user:123', function () {
    return $this->fetchUserFromDatabaseAsync(123);
});

Review the README.md and the src/ directory’s interface definitions (CacheInterface, TaggedCacheInterface) to understand the core contracts.

Implementation Patterns

  • Async-native caching: Leverage fiber-awareness to cache coroutines—the cache stores lazy generators/coroutines and handles resumption transparently.
  • Tagged caching: Use TaggedCache to invalidate related entries by tag:
    $tagged = new TaggedCache($cache);
    $tagged->set('post:42', $content, ['posts', 'drafts']);
    $tagged->invalidateTags(['drafts']); // invalidates all posts with draft tag
    
  • Fallback caching pattern: Always wrap cache misses with get($key, $resolver)—the resolver (callable returning a coroutine) only executes on a miss.
  • Integrate with async HTTP clients: Cache responses for retries or offloading heavy API calls:
    $response = $cache->get('api:external', fn() => $httpClient->request('GET', $url));
    
  • Cache adapters: Swap storage backends (e.g., Amp\Cache\Redis\RedisCache) without changing client code, thanks to the unified interface.

Gotchas and Tips

  • Resolvers must be pure: Avoid side effects in the resolver closure—it may run multiple times in concurrent scenarios or during re-resolutions (e.g., after invalidation).
  • Fiber execution context: If using Amp\Promise\cache(), remember it only caches resolved values—use CacheInterface::get() for lazy async resolution.
  • Serialization matters: For persistent stores (FileCache, RedisCache), ensure your cacheable data is serializable (implements Serializable or JsonSerializable). Non-serializable resources (e.g., streams) will fail silently or throw.
  • Expiration quirks: Expiration is soft—items may persist beyond expiry until overwritten or evicted; avoid relying on exact TTL for security-sensitive data.
  • Debugging: Enable Revolt’s debug mode to trace fiber execution; cache misses may span multiple fibers, making logs confusing without context.
  • Extensibility: Implement your own Cache by extending AbstractCache or decorating with ProxyCache for metrics/logging (e.g., track hit/miss ratios).
  • Concurrency safety: While fibers isolate async control flow, cache operations themselves aren’t atomic across processes—use Redis-based caches with SETNX or file locks for distributed environments.
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