{"id":30017453,"url":"https://github.com/natsumework/laravel-cache-manager","last_synced_at":"2025-08-20T06:18:18.803Z","repository":{"id":57024049,"uuid":"332436801","full_name":"natsumework/laravel-cache-manager","owner":"natsumework","description":"Allows you to easily manage the cache with config file. And provide a simple solution to solve the cache penetration and hotspot invalid.","archived":false,"fork":false,"pushed_at":"2024-03-23T13:21:58.000Z","size":45,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-05T22:58:28.328Z","etag":null,"topics":["cache","cachemanager","laravel","redis"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/natsumework.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-24T11:53:29.000Z","updated_at":"2024-03-23T10:32:11.000Z","dependencies_parsed_at":"2022-08-23T14:20:59.378Z","dependency_job_id":null,"html_url":"https://github.com/natsumework/laravel-cache-manager","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/natsumework/laravel-cache-manager","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natsumework%2Flaravel-cache-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natsumework%2Flaravel-cache-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natsumework%2Flaravel-cache-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natsumework%2Flaravel-cache-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/natsumework","download_url":"https://codeload.github.com/natsumework/laravel-cache-manager/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/natsumework%2Flaravel-cache-manager/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271275011,"owners_count":24731147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","cachemanager","laravel","redis"],"created_at":"2025-08-05T22:58:20.666Z","updated_at":"2025-08-20T06:18:18.788Z","avatar_url":"https://github.com/natsumework.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Easily manage your cache for laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/natsumework/laravel-cache-manager.svg?style=flat-square)](https://packagist.org/packages/natsumework/laravel-cache-manager)\n![Test Status](https://img.shields.io/github/actions/workflow/status/natsumework/laravel-cache-manager/test.yml?branch=main)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Total Downloads](https://img.shields.io/packagist/dt/natsumework/laravel-cache-manager.svg?style=flat-square)](https://packagist.org/packages/natsumework/laravel-cache-manager)\n\nAllows you to easily manage the cache with config file. And provide a simple solution to solve the cache penetration and hotspot invalid.\n\n## Contents\n\n- [Installation](#installation)\n- [Configuration](#configuration)\n    - [General Configuration](#general-configuration)\n    - [Hotspot Invalid Protection Configuration](#hotspot-invalid-protection-configuration)\n    - [Penetrate Protection Configuration](#penetrate-protection-configuration)\n    - [Types Configuration](#types-configuration)\n- [Usage](#usage)\n- [Changelog](#changelog)\n- [Testing](#testing)\n- [Security](#security)\n- [Contributing](#contributing)\n- [Credits](#credits)\n- [License](#license)\n\n\n## Installation\n\nInstall this package via composer:\n\n```\ncomposer require natsumework/laravel-cache-manager\n```\n\nPublish the configuration file to `config/cache-manager.php`:\n\n```\nphp artisan vendor:publish --provider=\"Natsumework\\CacheManager\\CacheManagerServiceProvider\"\n```\n\n## Configuration\n\n`config/cache-manager.php`\n\n### General Configuration\n\n```\nreturn [\n    // ...\n\n    /*\n    |--------------------------------------------------------------------------\n    | Cache Store\n    |--------------------------------------------------------------------------\n    |\n    | This option controls the cache connection that gets used while\n    | using this caching library.\n    |\n    | Before use the \"store\", you need to define your `Cache Stores` in config/cache.php\n    |\n    | @see https://laravel.com/docs/master/cache#configuration\n    */\n\n    'store' =\u003e 'file',\n\n    /*\n    |--------------------------------------------------------------------------\n    | Storage time\n    |--------------------------------------------------------------------------\n    |\n    | Specifies that items will expire in a few seconds.\n    | If set to null, items will be stored indefinitely.\n    }\n    */\n    'ttl' =\u003e 600,\n\n    // ...\n];\n```\n\n### Hotspot Invalid Protection Configuration\n\n```\nreturn [\n    // ...\n  \n    /*\n    |--------------------------------------------------------------------------\n    | Hotspot Invalid Protection\n    |--------------------------------------------------------------------------\n    |\n    | When we set the cache, we usually set an expiration time for the cache.\n    | After this time, the cache will be invalid.\n    | For some hotspot data, when the cache fails, there will be a large number\n    | of requests coming over, and then hit the database, which will cause the\n    | database enormous pressure.\n    |\n    | Set \"hotspot_protection\" to `true` will use the \"Mutex lock\" to avoid\n    | the problem of database corruption caused by the failure of hotspot data.\n    |\n    | You must confirm that the store you are using supports atomic-locks.\n    | @see https://laravel.com/docs/master/cache#atomic-locks\n    */\n    'hotspot_protection' =\u003e false,\n\n    /*\n    |--------------------------------------------------------------------------\n    | Hotspot Invalid Protection Lock Timeout\n    |--------------------------------------------------------------------------\n    |\n    | Specifies that \"Mutex lock\" will be automatically released in a few seconds.\n    |\n    */\n    'hotspot_protection_ttl' =\u003e 15,\n\n    /*\n    |--------------------------------------------------------------------------\n    | Hotspot Invalid Protection Suffix\n    |--------------------------------------------------------------------------\n    |\n    | When Hotspot Invalid Protection is enabled, `{type}:{index}:{hotspot_protection_suffix}`\n    | will be used as the key of \"Mutex lock\".\n    | You must specify a unique value and avoid using this value as the key for storing data,\n    | otherwise the data may be overwritten due to duplicate keys.\n    |\n    */\n    'hotspot_protection_suffix' =\u003e 'hotspot_protection',\n\n    // ...\n];\n```\n\n### Penetrate Protection Configuration\n\n```\nreturn [\n    // ...\n\n    /*\n    |--------------------------------------------------------------------------\n    | Penetrate Protection\n    |--------------------------------------------------------------------------\n    |\n    | When the business system initiates a query, the query will first go to\n    | the cache, if the cache does not exist, it will go to the database for query.\n    | If no data is found in the database, the data will not be stored in the cache,\n    | which will cause the query hit the database every time.\n    |\n    | When Penetrate protection is enabled, `null` data will be converted to `false`\n    | and stored in the cache.\n    */\n    'penetrate_protection' =\u003e false,\n\n    // ...\n];\n```\n\n### Types Configuration\n\nThis configuration allows you to easily manage all caches here.  \n\n##### Notes\n+ Before you put item into the cache storage, you have to define the type first.\n\n```\nreturn [\n    // ...\n\n    /*\n    |--------------------------------------------------------------------------\n    | Cache Types\n    |--------------------------------------------------------------------------\n    | Before you put item into the cache storage, you have to define the type first.\n    |\n    | You can also define `ttl`, `hotspot_protection`, `penetrate_protection`,\n    | `hotspot_protection_ttl` for each type. It takes precedence over a globally defined one.\n    |\n    */\n    'types' =\u003e [\n        'user' =\u003e [\n            'ttl' =\u003e 300, // seconds\n            'hotspot_protection' =\u003e true,\n            'penetrate_protection' =\u003e true,\n            'hotspot_protection_ttl' =\u003e 10, // seconds\n        ],\n        'active_users' =\u003e [\n            'ttl' =\u003e 60 * 60,\n            'hotspot_protection' =\u003e false,\n            'penetrate_protection' =\u003e false,\n        ],\n        'post' =\u003e [],\n\n        // ...\n    ],\n\n    // ...\n];\n```\n\n## Usage\n\n#### Retrieve \u0026 Store item\n\nIf the item does not exist in the cache, the Closure passed to\nthe remember method will be executed and its result will be placed in the cache.\n\n```\nuse Natsumework\\CacheManager\\Facades\\CacheManager;\n\n// CacheManager::remember($type, $index = null, \\Closure $callback = null)\n\n// you must define type `user` in config/cache-manager.php\n$user = CacheManager::remember('user', 1, function () {\n    return User::find(1);\n});\n```\n\n```\n$data = CacheManager::remember('type', 999, function () {\n    return null;\n});\n\n// If `penetration_protection` is enabled, $data === false\n// If `penetration_protection` is not enabled, $data === null\n```\n\n#### After the item is updated, use the Cache-Aside Pattern to invalid the item\n\nThe `updated` method will remove item.\n\n```\n// CacheManager::updated($type, $index = null)\n\n$user = User::find(1);\n$user-\u003ename = 'New name';\n$user-\u003esave();\n\n// user updated\nCacheManager::updated('user', 1);\n```\n\nYou can also use the `forget` method to remove item.\n\n```\nCacheManager::forget('user', 1);\n```\n\n#### Retrieve item\n\n```\nuse Natsumework\\CacheManager\\Facades\\CacheManager;\n\n// CacheManager::get($type, $index = null, $default = null)\n\n// you must define type `user` in config/cache-manager.php\n$user = CacheManager::get('user', 1, 'user not found');\n```\n\n#### Storing Items In The Cache\n\n```\nuse Natsumework\\CacheManager\\Facades\\CacheManager;\n\n// CacheManager::put($type, $index = null, $value = null)\n\n// you must define type `user` in config/cache-manager.php\nCacheManager::put('user', 1, User::find(1));\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Security\n\nIf you discover any security related issues, please email natsumework0902@gmail.com instead of using the issue tracker.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Credits\n\n- [natsumework](https://github.com/natsumework)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatsumework%2Flaravel-cache-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnatsumework%2Flaravel-cache-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnatsumework%2Flaravel-cache-manager/lists"}