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

Simple Cache Laravel Package

psr/simple-cache

PSR-16 Simple Cache interfaces from PHP-FIG. Defines a common API for cache implementations (get/set/delete, TTL, etc.) but provides no caching itself. Use this package to type-hint against the standard and pair with any compatible cache provider.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by requiring the interface package: composer require psr/simple-cache. This installs only the PSR-16 interface definitions—no caching backend. To get working caching, install a concrete implementation (e.g., symfony/cache with CacheItemPoolAdapter, league/cache, or use Laravel’s built-in adapter: cache()->driver() is PSR-16 compatible from Laravel 8.40+). Your first real use case will be injecting Psr\SimpleCache\CacheInterface into a service to cache lightweight results (e.g., a slow DB count or third-party API response) using get()/set().

Implementation Patterns

  • Laravel services: Resolve CacheInterface via DI—e.g., public function __construct(CacheInterface $cache)—and use it directly in domain services, avoiding framework-specific Cache::remember() calls for portable code.
  • Memoization helper: Wrap recurring logic like:
    $result = $cache->get("user_{$id}_stats", fn() => $this->expensiveStats($id), 300);
    
    (Note: TTL support depends on the underlying implementation; some accept int $ttl as third arg even if not in PSR-16 spec)
  • Middleware caching: In HTTP middleware, cache short-lived response fragments (e.g., navigation counts, dashboard stats):
    $key = 'dashboard_' . md5($request->user()->id);
    if ($cache->has($key)) {
        return response()->setContent($cache->get($key));
    }
    // ... render, store, return
    
  • Testing: Bind ArrayCache (from _aryie/array-cache) in phpunit.xml or test containers to avoid filesystem/network side effects.
  • Interoperable libraries: Use when building packages that consume caching—e.g., an analytics bundle expecting CacheInterface—to maximize adoption.

Gotchas and Tips

  • TTL ambiguity: PSR-16 v3.0 does not guarantee $ttl in set(). While many implementations (like Symfony’s adapter) accept set($key, $value, $ttl), it’s non-portable. For strict portability, use implementation-specific extensions or pass TTL via context (e.g., get($key, null, ['ttl' => 300]))—but this is not standardized.
  • Null-safety: If null is a valid cached value, get($key)’s default behavior returns null on miss—leading to false positives. Always use has($key) first or specify a unique sentinel as $default (e.g., get($key, self::CACHE_MISS_SENTINEL)).
  • PHP version trap: PSR-16 v3 requires PHP 8+. If your project targets PHP 7.4, you must use ^2.0 (composer require psr/simple-cache:^2.0). Conflicts with newer implementations often break silently.
  • Bulk operations: Prefer getMultiple() over looping get()—e.g., getMultiple(['config_a', 'config_b'])—to avoid cache round-trips.
  • Key validation: Implementations like symfony/cache may throw InvalidArgumentException for non-string keys or serialize()-unsafe values. Always ensure keys are strings/scalars and values are serializable.
  • No eviction policy: PSR-16 doesn’t define LRU/LFU—rely on your underlying store (Redis, APCu, file) for memory management. Don’t assume items expire unless TTL is explicitly supported and enforced by the driver.
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