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

spiral/cache

Spiral Cache provides a simple caching abstraction for Spiral/PHP apps, with support for multiple storage backends and configurable cache pools. Use it to speed up repeated operations, cache computed values, and centralize cache access and invalidation.

View on GitHub
Deep Wiki
Context7

Getting Started

This package provides PSR-6 and PSR-16 interface contracts and foundational cache abstractions — not implementations. To use it, install via Composer: composer require spiral/cache. Then, implement the interfaces (e.g., Psr\SimpleCache\CacheInterface, Psr\Cache\CacheItemPoolInterface) in your own driver classes or adapters. For example, create a Redis-backed adapter by wrapping Laravel’s Cache::store('redis'). Your first real use case: type-hint CacheInterface in a service class to decouple caching logic from infrastructure.

Implementation Patterns

  • Interface-first design: Inject Psr\SimpleCache\CacheInterface into repositories or services where fast, simple key-value caching is needed (e.g., get('user_' . $id)).
  • Advanced pools: Use CacheItemPoolInterface for tag-based invalidation or batch operations, e.g., inject a TaggableCacheItemPoolInterface (custom extension) to clear all cache items tagged ['products', 'variant_42'].
  • Laravel integration: Wrap Illuminate\Cache\Repository with a thin adapter class implementing CacheInterface, enabling Spiral-style DI in Laravel apps:
    class LaravelCacheAdapter implements \Psr\SimpleCache\CacheInterface { 
        public function __construct(protected \Illuminate\Cache\Repository $laravelCache) {}
        // map get/set/delete to $this->laravelCache->get()/put()/forget()
    }
    
  • Testability: In tests, mock CacheInterface and assert on set() calls using ::with() to verify cache keys, values, and TTLs without hitting real storage.

Gotchas and Tips

  • ⚠️ Purely interfaces: This package does nothing at runtime—no fallback, no driver logic. You must pair it with an adapter (e.g., spiral/cache-adapter-redis) or build your own.
  • ⚠️ PSR-6 vs PSR-16 pitfalls: PSR-16’s get() returns null for misses and for cache errors by default, while PSR-6 allows distinguishing errors via isHit(). Your adapter implementation dictates behavior—ensure error handling aligns with your expectations.
  • Best practice: Use factory classes to resolve context-specific cache pools (e.g., ShortTtlCacheFactoryttl=60, PersistentCacheFactoryttl=86400).
  • 🔍 Debugging tip: Add logging in your adapter’s get() and set() methods to surface cache misses or latency spikes during high-load scenarios.
  • 📦 Laravel users: Avoid re-implementing PSR support—illuminate/cache already provides Psr\SimpleCache\CacheInterface via Cache::store() when psr/simple-cache is installed (it is, by default in Laravel 8+). This package adds little unless you’re migrating Spiral conventions.
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