{"id":19324848,"url":"https://github.com/spatie/laravel-blink","last_synced_at":"2025-05-15T10:06:29.238Z","repository":{"id":45960984,"uuid":"82813073","full_name":"spatie/laravel-blink","owner":"spatie","description":"Cache that expires in the blink of an eye","archived":false,"fork":false,"pushed_at":"2025-02-19T07:02:22.000Z","size":94,"stargazers_count":157,"open_issues_count":0,"forks_count":14,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-10T06:39:17.025Z","etag":null,"topics":["cache","laravel","performance","php"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source/packages","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/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2017-02-22T14:22:49.000Z","updated_at":"2025-04-04T04:40:49.000Z","dependencies_parsed_at":"2024-06-18T13:39:06.276Z","dependency_job_id":"78c8f7df-606f-4353-a2c8-1f35a63d703f","html_url":"https://github.com/spatie/laravel-blink","commit_stats":{"total_commits":84,"total_committers":15,"mean_commits":5.6,"dds":0.5476190476190477,"last_synced_commit":"bea829f5d19194c04ae973298ba24bd8b3d26402"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-blink","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-blink/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-blink/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-blink/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-blink/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319718,"owners_count":22051072,"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","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","laravel","performance","php"],"created_at":"2024-11-10T02:07:08.473Z","updated_at":"2025-05-15T10:06:24.169Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":[],"sub_categories":[],"readme":"# Cache that expires in the blink of an eye\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-blink.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-blink)\n[![run-tests](https://github.com/spatie/laravel-blink/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-blink/actions/workflows/run-tests.yml)\n![Check \u0026 fix styling](https://github.com/spatie/laravel-blink/workflows/Check%20\u0026%20fix%20styling/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-blink.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-blink)\n\nThis package contains a helper function (and a Facade should you prefer that) called `blink` that can cache values. The cache only spans the length of a single request.\n\n```php\nblink()-\u003eput('key', 'value');\n\nblink()-\u003eget('key'); // Returns 'value'\nblink()-\u003eget('prefix*'); // Returns an array of values whose keys start with 'prefix'\n\n// once will only execute the given callable if the given key didn't exist yet\n$expensiveFunction = function() {\n   return rand();\n});\nblink()-\u003eonce('random', $expensiveFunction); // returns random number\nblink()-\u003eonce('random', $expensiveFunction); // returns the same number\n\nblink()-\u003ehas('key'); // Returns true\nblink()-\u003ehas('prefix*'); // Returns true if the blink contains a key that starts with 'prefix'\n\n// Specify a default value for when the specified key does not exist\nblink()-\u003eget('non existing key', 'default') // Returns 'default'\n\nblink()-\u003eput('anotherKey', 'anotherValue');\n\n// Put multiple items in one go\nblink()-\u003eput(['ringo' =\u003e 'drums', 'paul' =\u003e 'bass']);\n\nblink()-\u003eall(); // Returns an array with all items\n\nblink()-\u003eforget('key'); // Removes the item\nblink()-\u003eforget('prefix*'); // Forget all items of which the key starts with 'prefix'\n\nblink()-\u003eflush(); // Empty the entire blink\n\nblink()-\u003eflushStartingWith('somekey'); // Remove all items whose keys start with \"somekey\"\n\nblink()-\u003eincrement('number'); // blink()-\u003eget('number') will return 1 \nblink()-\u003eincrement('number'); // blink()-\u003eget('number') will return 2\nblink()-\u003eincrement('number', 3); // blink()-\u003eget('number') will return 5\n\n// Blink implements ArrayAccess\nblink()['key'] = 'value';\nblink()['key']; // Returns 'value'\nisset(blink()['key']); // Returns true\nunset(blink()['key']); // Equivalent to removing the value\n\n// Blink implements Countable\ncount(blink()); // Returns 0\nblink()-\u003eput('key', 'value');\ncount(blink()); // Returns 1\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-blink.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-blink)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/laravel-blink\n```\n\nTo enable the package, register the service provider, and optionally register the facade:\n\n```php\n// config/app.php\n\n'providers' =\u003e [\n    // ...\n    Spatie\\LaravelBlink\\BlinkServiceProvider::class,\n],\n\n'aliases' =\u003e [\n    ...\n    'Blink' =\u003e Spatie\\LaravelBlink\\BlinkFacade::class,\n],\n```\n\n## Usage\n\nA `Blink` instance can just be newed up.\n\n```php\n$blink = new \\Spatie\\Blink\\Blink()\n```\n\nYou can call the following methods on it:\n\n### put\n```php\n/**\n * Put a value in the blink cache.\n *\n * @param string|array $name\n * @param string|int|null $value\n * \n * @return $this\n */\npublic function put($name, $value = null)\n```\n\n### get\n\n```php\n/**\n * Get a value from the blink cache.\n *\n * This function has support for the '*' wildcard.\n *\n * @param string $name\n *\n * @return null|string\n */\npublic function get(string $name)\n```\n\n### has\n\n```php\n/*\n * Determine if the blink cache has a value for the given name.\n *\n * This function has support for the '*' wildcard.\n */\npublic function has(string $name) : bool\n```\n\n### once\n\n```php\n/**\n * Only if the given key is not present in the blink cache the callable will be executed.\n * \n * The result of the callable will be stored in the given key and returned.\n * \n * @param $key\n * @param callable $callable\n *\n * @return mixed\n */\n```\n\nYou can use this to avoid using static variables to cache stuff.\n\nThis piece of code\n\n```php\nfunction foo()\n{\n    static $result = null;\n    \n    if (is_null($result) {\n        $result = ...// do some expensive stuff here\n    }\n    \n    return $result;\n}\n```\n\ncan be rewritten to\n\n```php\nfunction foo()\n{\n    return blink()-\u003eonce('fooCache', function() {\n       return ... // do some expensive stuff here\n    });\n}\n```\n\n### all\n```php\n/*\n* Get all values in the blink cache.\n*/\npublic function all() : array\n```\n\n### allStartingWith\n```php\n/**\n * Get all values from the blink cache which keys start with the given string.\n *\n * @param string $startingWith\n *\n * @return array\n*/\npublic function allStartingWith(string $startingWith = '') : array\n```\n\n### forget\n```php\n/**\n * Forget a value from the blink cache.\n *\n * This function has support for the '*' wildcard.\n *\n * @param string $key\n *\n * @return $this\n */\npublic function forget(string $key)\n```\n\n### flush\n```php\n/**\n * Flush all values from the blink cache.\n *\n * @return $this\n */\n public function flush()\n```\n\n### flushStartingWith\n```php\n/**\n * Flush all values from the blink cache which keys start with the specified value.\n *\n * @param string $startingWith\n *\n * @return $this\n */\n public function flushStartingWith(string $startingWith)\n```\n\n### pull\n```php\n/**\n * Get and forget a value from the blink cache.\n *\n * This function has support for the '*' wildcard.\n *\n * @param string $name \n *\n * @return null|string\n */\npublic function pull(string $name)\n```\n\n### increment\n```php\n/**\n * Increment a value from the blink cache.\n *\n * @param string $name\n * @param int $by\n *\n * @return int|null|string\n */\n public function increment(string $name, int $by = 1)\n```\n\n### decrement\n```php\n/**\n * Decrement a value from the blink cache.\n *\n * @param string $name\n * @param int $by\n *\n * @return int|null|string\n */\n public function decrement(string $name, int $by = 1)\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\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%2Fspatie%2Flaravel-blink","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-blink","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-blink/lists"}