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

Scrapbook Laravel Package

matthiasmullie/scrapbook

Scrapbook is a PHP caching library that wraps PSR-6/PSR-16 cache backends with adapters, multi-cache fallbacks, buffering and stampede protection. Use it to add fast, resilient caching with minimal code changes across files, memory or Redis stores.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require matthiasmullie/scrapbook. Then instantiate a cache adapter (e.g., new \ MatthiasMullie\Scrapbook\Adapters\Redis($redisClient)) and wrap it in the main Cache class. The simplest use case is basic get/set operations: $cache->set('key', $data); $data = $cache->get('key');. Review the Adapters namespace to understand available backend options—Redis and Memcached are most common in production. Check out examples/ in the repo for quick integration snippets.

Implementation Patterns

  • Stampede Protection: Use $cache->get('key', null, true) to enable automatic stampede protection via getWithStampedeProtection(), which locks cache regeneration for heavy operations.
  • Transactional Caching: Wrap related cache operations in a transaction to ensure atomicity:
    $cache->transaction(function() use ($cache) {
        $cache->set('a', 1);
        $cache->set('b', 2);
    });
    
  • Tagging (via TaggableAdapter): Wrap adapters like ArrayAdapter or Redis with TaggableAdapter to invalidate groups:
    $cache = new \Matthiasmullie\Scrapbook\Adapters\Taggable($adapter);
    $cache->set('post:123', $data, 'posts');
    $cache->flushByTag('posts');
    
  • Custom TTL per Call: Pass expiry as the third param: $cache->set('key', $value, 3600);.

Gotchas and Tips

  • Tagging Requires Wrapping: Tag support is not built into core adapters—you must explicitly wrap them in TaggableAdapter, or tag-related methods will silently fail or throw.
  • Transaction Performance: Transactions serialize operations, so avoid long-running callbacks; use only for logically grouped writes.
  • Redis Serialization: Ensure your Redis client is configured with a compatible serializer (e.g., igbinary or json), as mismatches can corrupt cached data across clients.
  • Fallback for null: get() returns null both for cache misses and when null is the stored value. Use has() before get() or check explicitly if null is valid data.
  • Stampede Protection Caveat: Only safe if the regeneration callback is idempotent and side-effect-free—e.g., avoid database writes inside regeneration logic.
  • Test with Multiple Adapters: Since behavior can differ (e.g., Memcached truncates keys >250 chars), write adapter-agnostic code and test with real-world backends.
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