{"id":19324847,"url":"https://github.com/spatie/blink","last_synced_at":"2025-04-12T21:28:26.816Z","repository":{"id":45146916,"uuid":"84998612","full_name":"spatie/blink","owner":"spatie","description":"Cache that expires in the blink of an eye","archived":false,"fork":false,"pushed_at":"2023-10-31T23:07:42.000Z","size":55,"stargazers_count":166,"open_issues_count":0,"forks_count":18,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-04T00:57:17.355Z","etag":null,"topics":["blink","cache","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-03-14T21:24:15.000Z","updated_at":"2025-03-02T10:20:59.000Z","dependencies_parsed_at":"2023-01-24T19:33:31.421Z","dependency_job_id":"5bf1ade0-752b-4cca-89f4-394bcee54e01","html_url":"https://github.com/spatie/blink","commit_stats":{"total_commits":63,"total_committers":14,"mean_commits":4.5,"dds":"0.39682539682539686","last_synced_commit":"7fa7a0cbea2e326a0c743783779cea790445fcb5"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fblink","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fblink/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fblink/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fblink/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/blink/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248633952,"owners_count":21136952,"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":["blink","cache","php"],"created_at":"2024-11-10T02:07:08.409Z","updated_at":"2025-04-12T21:28:26.794Z","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/blink.svg?style=flat-square)](https://packagist.org/packages/spatie/blink)\n[![run-tests](https://github.com/spatie/blink/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/blink/actions/workflows/run-tests.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/blink.svg?style=flat-square)](https://packagist.org/packages/spatie/blink)\n\nThis package contains a class called `Blink` that can cache values. The cache only spans the length of a single request.\n\nIt can be used like this:\n\n```php\n$blink = new Blink();\n\n$blink-\u003eput('key', 'value');\n\n$blink-\u003eget('key'); // Returns 'value'\n$blink-\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});\n$blink-\u003eonce('random', $expensiveFunction); // returns random number\n$blink-\u003eonce('random', $expensiveFunction); // returns the same number\n\n$blink-\u003ehas('key'); // Returns true\n$blink-\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\n$blink-\u003eget('non existing key', 'default') // Returns 'default'\n\n$blink-\u003eput('anotherKey', 'anotherValue');\n\n// Put multiple items in one go\n$blink-\u003eput(['ringo' =\u003e 'drums', 'paul' =\u003e 'bass']);\n\n$blink-\u003eall(); // Returns an array with all items\n\n$blink-\u003eforget('key'); // Removes the item\n$blink-\u003eforget('prefix*'); // Forget all items of which the key starts with 'prefix'\n\n$blink-\u003eflush(); // Empty the entire blink\n\n$blink-\u003eflushStartingWith('somekey'); // Remove all items whose keys start with \"somekey\"\n\n$blink-\u003eincrement('number'); // $blink-\u003eget('number') will return 1\n$blink-\u003eincrement('number'); // $blink-\u003eget('number') will return 2\n$blink-\u003eincrement('number', 3); // $blink-\u003eget('number') will return 5\n\n// Blink implements ArrayAccess\n$blink['key'] = 'value';\n$blink['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\n$blink-\u003eput('key', 'value');\ncount($blink); // Returns 1\n```\n\nIf you want to use the same instance within the current request, you can use the static method `global`.\n\n```php\nBlink::global()-\u003eput('key', 'value');\n\nBlink::global()-\u003eget('key') // Returns 'value'\n```\n\nRead the [usage](#usage) section of this readme to learn the other methods.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/blink.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/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/blink\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 */\npublic function once($key, callable $callable)\n```\n\n### onceIf\n\n```php\n/**\n * Use the \"once\" method only if the given condition is true.\n *\n * Otherwise, the callable will be executed.\n *\n * @param bool $shouldBlink\n * @param $key\n * @param callable\n *\n * @return mixed\n */\npublic function onceIf($shouldBlink, $key, callable $callable)\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 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\nWe got the idea and the name for this package from [Statamic's Blink helper](https://docs.statamic.com/addons/helpers#blink-cache). We reached out to them and got permission for using the `blink` name.\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%2Fblink","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fblink","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fblink/lists"}