Weave 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
Model Cache

Model Cache Laravel Package

hyperf/model-cache

Hyperf model-cache adds automatic caching for Hyperf ORM models, reducing repeated database queries and speeding up reads. It integrates with model events for cache invalidation and supports configurable TTL and cache stores.

View on GitHub
Deep Wiki
Context7

hyperf/model-cache adds transparent query caching to your Hyperf models, so repeated database lookups are served from a cache store instead of hitting the database. It integrates with hyperf/database and the Hyperf cache component, letting you enable caching per model with minimal configuration while keeping Eloquent-style query APIs unchanged.

  • Automatic caching of model queries with configurable TTL
  • Cache invalidation on create, update, and delete operations
  • Support for custom cache keys and cache prefix/tag grouping
  • Works with any Hyperf cache driver (Redis, file, memory)
  • Simple trait/config-based opt-in per model
Frequently asked questions about Model Cache
What is hyperf/model-cache used for?
hyperf/model-cache adds automatic caching to Hyperf models so repeated database queries can be served from cache instead of hitting the database each time. It stores query results and model attributes in cache, supports configurable TTLs, and handles invalidation when models are created, updated, or deleted. It integrates with Hyperf's cache component, so it fits naturally into an existing Hyperf application.
How do I install hyperf/model-cache in a Hyperf project?
Install it with Composer using composer require hyperf/model-cache, then make sure Hyperf's cache component is configured and available in your application. Check the repository README for any config publishing or registration steps specific to your Hyperf version. After installation, apply caching behavior to the models where you want to reduce repeated queries.
Does hyperf/model-cache work with Laravel?
No, hyperf/model-cache is built for the Hyperf framework and its cache component, not for Laravel. Laravel developers evaluating it should treat it as a reference for automatic model caching patterns rather than a drop-in package. For Laravel applications, look at Laravel-specific model cache packages or implement caching at the repository or query layer.
Which PHP and Hyperf versions does hyperf/model-cache support?
Supported versions depend on the package release, so check the composer.json constraints and the repository release notes before installing. Hyperf packages generally require a modern PHP version and a compatible Hyperf framework release, and they run in a Swoole or Swow coroutine environment. Pin a package version that matches your Hyperf minor version to avoid dependency conflicts.
How does cache invalidation work when a model changes?
The package handles invalidation on create, update, and delete events so cached query results and attributes do not stay stale after writes. TTLs provide a second layer of protection by expiring entries automatically. For bulk operations or raw query-builder updates that bypass model events, verify that the relevant cache entries are cleared or set a short enough TTL.
Can I configure different TTLs for different models?
Yes, the package supports configurable TTLs, so you can set a default and tune cache lifetime per model or use case. Use short TTLs for frequently changing data and longer TTLs for mostly static lookup data. TTL choices should balance database load against how much staleness your application can tolerate.
How do I test model caching in my Hyperf application?
Use Hyperf's testing utilities and a fake or in-memory cache driver so tests do not depend on Redis or another external store. Assert that repeated reads produce fewer database queries and that create, update, and delete operations invalidate the expected cache entries. Clear the cache between test cases to keep results deterministic.
What are the production concerns with using hyperf/model-cache?
Watch for stale data when writes bypass model events, and choose TTLs that match how quickly your data changes. Cache stampedes and memory growth are real risks with high-traffic models, so monitor hit rates, key counts, and Redis memory usage. Use a namespaced cache store and warm critical caches after deployments.
What alternatives exist to hyperf/model-cache?
You can cache manually with hyperf/cache, wrap repository methods, or use a dedicated query cache layer. In Laravel applications, comparable behavior comes from Laravel-specific model cache packages or explicit Cache::remember calls. Evaluate alternatives based on invalidation guarantees, TTL control, and how well they fit your existing data access patterns.
Is hyperf/model-cache actively maintained?
Maintenance activity varies, so review the repository's recent commits, releases, and open issues before adopting it. Confirm that the latest release supports your Hyperf and PHP versions. Pinning a known-good version and having a fallback caching strategy reduces risk if updates slow down.
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.
vento/laravel-airbrake
wfeller/laravel-batch
maco-studios/mage-os-installer
busarm/armie
siberfx/laravel-logger
kfteam1991/dcat-admin
dashed/dashed-laposta
dashed/dashed-ecommerce-montaportal
dashed/dashed-ecommerce-etsy
neohq/string-blade-compiler
log1x/acf-composer
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager