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
Namespaced Cache

Namespaced Cache Laravel Package

cache/namespaced-cache

PSR-6 cache pool decorator that adds namespaces on top of a hierarchical cache (e.g., Redis). Wrap an existing cache pool and automatically prefix keys per namespace, helping isolate apps/modules while reusing the same backend.

View on GitHub
Deep Wiki
Context7

A decorator that makes your cache support namespaces

Frequently asked questions about Namespaced Cache
How do I integrate this package with Laravel’s built-in cache system?
Replace your existing cache driver in `config/cache.php` with a namespaced decorator. For example, wrap Redis by adding `'driver' => 'namespaced'` and specifying the underlying store. Then use `Cache::store('namespaced_redis')->get('tenant:123:key')` in your code. No changes to Laravel’s facade or container are needed.
Does this work with Laravel’s session or queue cache drivers?
No, this package targets PSR-6-compatible drivers like Redis or Memcached. Laravel’s session or queue cache (which use file/database) aren’t PSR-6 compliant, so they won’t work directly. Use Redis or Memcached for full compatibility.
What’s the performance impact of adding namespaces to cache keys?
The decorator adds minimal overhead for prefixing/suffixing keys, but in high-throughput systems, benchmark with your cache backend. Redis or Memcached will handle this efficiently, but file-based caches may see slower key operations due to disk I/O.
Can I use hierarchical namespaces like `tenant:123:feature:abc`?
Yes, the package supports hierarchical namespaces. Configure it in the decorator’s constructor or via Laravel’s cache configuration. For example, `Cache::store('namespaced')->get('tenant:123:feature:abc')` will generate keys like `laravel_namespaced_tenant:123:feature:abc:key`.
How do I clear all caches for a specific namespace (e.g., tenant) in production?
Use Laravel’s `Cache::store('namespaced')->flush()` to clear all namespaced keys. For tenant-specific cleanup, implement a custom script using `Cache::store('namespaced')->getKeys('tenant:123:*')` and iterate to delete them, or use Redis/Memcached’s `KEYS` pattern matching if supported.
Will this break existing cache keys if I migrate to namespaced-cache?
Yes, existing keys won’t be automatically prefixed. Plan a migration: either update all cache keys manually or use a script to repopulate caches with namespaced keys. Consider a phased rollout starting with non-critical caches.
Does this package support Laravel 10+? Are there any compatibility issues?
The last release (2022) may not fully support Laravel 10+. Test thoroughly, especially with newer features like cache tags or improved PSR-6 compliance. If issues arise, check the GitHub issues or fork the package for updates.
How do I handle cache misses when a namespace doesn’t exist (e.g., `tenant:999`)?
The decorator treats missing namespaces as invalid keys, returning `null` for `get()` calls. Handle this in your application logic (e.g., fetch data from the database) or use a fallback namespace. Example: `Cache::store('namespaced')->get('tenant:999:key')` will fail silently unless the key exists.
Can I use this with third-party Laravel cache packages like `spatie/laravel-cache`?
Only if those packages use PSR-6-compatible drivers like Redis. For example, `spatie/laravel-cache` extends Laravel’s cache, so it won’t work directly. Stick to native Laravel drivers (Redis, Memcached) or ensure the third-party package supports PSR-6 decorators.
What’s the best way to monitor cache hits/misses per namespace in Laravel?
Use Laravel Telescope’s cache listener to log hits/misses, or instrument the decorator with custom metrics. For Redis, enable `slowlog` or use `INFO stats` to track key patterns. Example: `Cache::store('namespaced')->namespace('tenant:123')->get('key')` and log the namespace in your application.
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor