├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ └── config.yml └── workflows │ ├── php-cs-fixer.yml │ ├── update-changelog.yml │ └── run-tests.yml ├── composer.json ├── LICENSE.md ├── .php-cs-fixer.dist.php ├── CHANGELOG.md ├── README.md └── src └── Blink.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: spatie 2 | custom: https://spatie.be/open-source/support-us 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: true 2 | contact_links: 3 | - name: Feature Request 4 | url: https://github.com/spatie/blink/discussions/new?category=ideas 5 | about: Share ideas for new features 6 | - name: Ask a Question 7 | url: https://github.com/spatie/blink/discussions/new?category=q-a 8 | about: Ask the community for help 9 | -------------------------------------------------------------------------------- /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [ push ] 4 | 5 | jobs: 6 | php-cs-fixer: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | with: 13 | ref: ${{ github.head_ref }} 14 | 15 | - name: Run PHP CS Fixer 16 | uses: docker://oskarstark/php-cs-fixer-ga 17 | with: 18 | args: --config=.php-cs-fixer.dist.php --allow-risky=yes 19 | 20 | - name: Commit changes 21 | uses: stefanzweifel/git-auto-commit-action@v4 22 | with: 23 | commit_message: Fix styling 24 | -------------------------------------------------------------------------------- /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v2 14 | with: 15 | ref: main 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v4 25 | with: 26 | branch: main 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md 29 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/blink", 3 | "description": "Cache that expires in the blink of an eye", 4 | "keywords": [ 5 | "spatie", 6 | "blink", 7 | "cache", 8 | "caching" 9 | ], 10 | "homepage": "https://github.com/spatie/blink", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Freek Van der Herten", 15 | "email": "freek@spatie.be", 16 | "homepage": "https://spatie.be", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^8.0" 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit": "^9.5" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Spatie\\Blink\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "Spatie\\Blink\\Test\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "vendor/bin/phpunit" 38 | }, 39 | "config": { 40 | "sort-packages": true 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | runs-on: ${{ matrix.os }} 8 | strategy: 9 | fail-fast: false 10 | matrix: 11 | os: [ubuntu-latest, windows-latest] 12 | php: [8.2, 8.1, 8.0] 13 | dependency-version: [prefer-lowest, prefer-stable] 14 | 15 | name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 16 | 17 | steps: 18 | - name: Checkout code 19 | uses: actions/checkout@v1 20 | 21 | - name: Setup PHP 22 | uses: shivammathur/setup-php@v2 23 | with: 24 | php-version: ${{ matrix.php }} 25 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick 26 | coverage: none 27 | 28 | - name: Install dependencies 29 | run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest 30 | 31 | - name: Execute tests 32 | run: vendor/bin/phpunit 33 | -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | notPath('bootstrap/*') 5 | ->notPath('storage/*') 6 | ->notPath('resources/view/mail/*') 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/tests', 10 | ]) 11 | ->name('*.php') 12 | ->notName('*.blade.php') 13 | ->ignoreDotFiles(true) 14 | ->ignoreVCS(true); 15 | 16 | return (new PhpCsFixer\Config) 17 | ->setRules([ 18 | '@PSR2' => true, 19 | 'array_syntax' => ['syntax' => 'short'], 20 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 21 | 'no_unused_imports' => true, 22 | 'not_operator_with_successor_space' => true, 23 | 'trailing_comma_in_multiline' => true, 24 | 'phpdoc_scalar' => true, 25 | 'unary_operator_spaces' => true, 26 | 'binary_operator_spaces' => true, 27 | 'blank_line_before_statement' => [ 28 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 29 | ], 30 | 'phpdoc_single_line_var_spacing' => true, 31 | 'phpdoc_var_without_name' => true, 32 | 'method_argument_space' => [ 33 | 'on_multiline' => 'ensure_fully_multiline', 34 | 'keep_multiple_spaces_after_comma' => true, 35 | ] 36 | ]) 37 | ->setFinder($finder); 38 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `blink` will be documented in this file 4 | 5 | ## 1.4.0 - 2023-07-19 6 | 7 | ### What's Changed 8 | 9 | - Fix the global method to return always the same instance by @stfndamjanovic in https://github.com/spatie/blink/pull/37 10 | 11 | ### New Contributors 12 | 13 | - @stfndamjanovic made their first contribution in https://github.com/spatie/blink/pull/37 14 | 15 | **Full Changelog**: https://github.com/spatie/blink/compare/1.3.0...1.4.0 16 | 17 | ## 1.3.0 - 2023-06-30 18 | 19 | ### What's Changed 20 | 21 | - Add PHP 8.2 Support by @patinthehat in https://github.com/spatie/blink/pull/32 22 | - added Blink::global() by @sfinktah in https://github.com/spatie/blink/pull/34 23 | - Add onceIf method by @jasonvarga in https://github.com/spatie/blink/pull/35 24 | 25 | ### New Contributors 26 | 27 | - @sfinktah made their first contribution in https://github.com/spatie/blink/pull/34 28 | 29 | **Full Changelog**: https://github.com/spatie/blink/compare/1.2.0...1.3.0 30 | 31 | ## 1.2.0 - 2022-01-05 32 | 33 | ## What's Changed 34 | 35 | - Add PHP 8.1 Support by @patinthehat in https://github.com/spatie/blink/pull/27 36 | - add return types by @edalzell in https://github.com/spatie/blink/pull/30 37 | - Drop support for PHP 7 38 | 39 | ## New Contributors 40 | 41 | - @patinthehat made their first contribution in https://github.com/spatie/blink/pull/27 42 | - @edalzell made their first contribution in https://github.com/spatie/blink/pull/30 43 | 44 | **Full Changelog**: https://github.com/spatie/blink/compare/1.1.3...1.2.0 45 | 46 | ## 1.1.3 - 2020-11-29 47 | 48 | - support PHP 8.0 and PHPUnit 9 49 | - now requires PHP 7.3 and up 50 | 51 | ## 1.1.2 - 2019-02-16 52 | 53 | - support numeric keys (#19) 54 | 55 | ## 1.1.1 - 2019-10-08 56 | 57 | - optimize pull method (#16) 58 | 59 | ## 1.1.0 - 2019-10-07 60 | 61 | - add `flushStartingWith()` and `allStartingWith()` methods (#13) 62 | 63 | ## 1.0.2 - 2018-11-27 64 | 65 | - add support of namespaces as cache key 66 | 67 | ## 1.0.1 - 2017-03-17 68 | 69 | - fix bug that returns wrong value type when forgetting a key 70 | 71 | ## 1.0.0 - 2017-03-17 72 | 73 | - initial release 74 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cache that expires in the blink of an eye 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/blink.svg?style=flat-square)](https://packagist.org/packages/spatie/blink) 4 | [![run-tests](https://github.com/spatie/blink/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/blink/actions/workflows/run-tests.yml) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/blink.svg?style=flat-square)](https://packagist.org/packages/spatie/blink) 6 | 7 | This package contains a class called `Blink` that can cache values. The cache only spans the length of a single request. 8 | 9 | It can be used like this: 10 | 11 | ```php 12 | $blink = new Blink(); 13 | 14 | $blink->put('key', 'value'); 15 | 16 | $blink->get('key'); // Returns 'value' 17 | $blink->get('prefix*'); // Returns an array of values whose keys start with 'prefix' 18 | 19 | // once will only execute the given callable if the given key didn't exist yet 20 | $expensiveFunction = function() { 21 | return rand(); 22 | }); 23 | $blink->once('random', $expensiveFunction); // returns random number 24 | $blink->once('random', $expensiveFunction); // returns the same number 25 | 26 | $blink->has('key'); // Returns true 27 | $blink->has('prefix*'); // Returns true if the blink contains a key that starts with 'prefix' 28 | 29 | // Specify a default value for when the specified key does not exist 30 | $blink->get('non existing key', 'default') // Returns 'default' 31 | 32 | $blink->put('anotherKey', 'anotherValue'); 33 | 34 | // Put multiple items in one go 35 | $blink->put(['ringo' => 'drums', 'paul' => 'bass']); 36 | 37 | $blink->all(); // Returns an array with all items 38 | 39 | $blink->forget('key'); // Removes the item 40 | $blink->forget('prefix*'); // Forget all items of which the key starts with 'prefix' 41 | 42 | $blink->flush(); // Empty the entire blink 43 | 44 | $blink->flushStartingWith('somekey'); // Remove all items whose keys start with "somekey" 45 | 46 | $blink->increment('number'); // $blink->get('number') will return 1 47 | $blink->increment('number'); // $blink->get('number') will return 2 48 | $blink->increment('number', 3); // $blink->get('number') will return 5 49 | 50 | // Blink implements ArrayAccess 51 | $blink['key'] = 'value'; 52 | $blink['key']; // Returns 'value' 53 | isset($blink['key']); // Returns true 54 | unset($blink['key']); // Equivalent to removing the value 55 | 56 | // Blink implements Countable 57 | count($blink); // Returns 0 58 | $blink->put('key', 'value'); 59 | count($blink); // Returns 1 60 | ``` 61 | 62 | If you want to use the same instance within the current request, you can use the static method `global`. 63 | 64 | ```php 65 | Blink::global()->put('key', 'value'); 66 | 67 | Blink::global()->get('key') // Returns 'value' 68 | ``` 69 | 70 | Read the [usage](#usage) section of this readme to learn the other methods. 71 | 72 | ## Support us 73 | 74 | [](https://spatie.be/github-ad-click/blink) 75 | 76 | We 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). 77 | 78 | We 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). 79 | 80 | ## Installation 81 | 82 | You can install the package via composer: 83 | 84 | ``` bash 85 | composer require spatie/blink 86 | ``` 87 | 88 | ## Usage 89 | 90 | A `Blink` instance can just be newed up. 91 | 92 | ```php 93 | $blink = new \Spatie\Blink\Blink() 94 | ``` 95 | 96 | You can call the following methods on it: 97 | 98 | ### put 99 | ```php 100 | /** 101 | * Put a value in the blink cache. 102 | * 103 | * @param string|array $name 104 | * @param string|int|null $value 105 | * 106 | * @return $this 107 | */ 108 | public function put($name, $value = null) 109 | ``` 110 | 111 | ### get 112 | 113 | ```php 114 | /** 115 | * Get a value from the blink cache. 116 | * 117 | * This function has support for the '*' wildcard. 118 | * 119 | * @param string $name 120 | * 121 | * @return null|string 122 | */ 123 | public function get(string $name) 124 | ``` 125 | 126 | ### has 127 | 128 | ```php 129 | /* 130 | * Determine if the blink cache has a value for the given name. 131 | * 132 | * This function has support for the '*' wildcard. 133 | */ 134 | public function has(string $name) : bool 135 | ``` 136 | 137 | ### once 138 | 139 | ```php 140 | /** 141 | * Only if the given key is not present in the blink cache the callable will be executed. 142 | * 143 | * The result of the callable will be stored in the given key and returned. 144 | * 145 | * @param $key 146 | * @param callable $callable 147 | * 148 | * @return mixed 149 | */ 150 | public function once($key, callable $callable) 151 | ``` 152 | 153 | ### onceIf 154 | 155 | ```php 156 | /** 157 | * Use the "once" method only if the given condition is true. 158 | * 159 | * Otherwise, the callable will be executed. 160 | * 161 | * @param bool $shouldBlink 162 | * @param $key 163 | * @param callable 164 | * 165 | * @return mixed 166 | */ 167 | public function onceIf($shouldBlink, $key, callable $callable) 168 | ``` 169 | 170 | ### all 171 | ```php 172 | /* 173 | * Get all values in the blink cache. 174 | */ 175 | public function all() : array 176 | ``` 177 | 178 | ### allStartingWith 179 | ```php 180 | /** 181 | * Get all values from the blink cache which keys start with the given string. 182 | * 183 | * @param string $startingWith 184 | * 185 | * @return array 186 | */ 187 | public function allStartingWith(string $startingWith = '') : array 188 | ``` 189 | 190 | ### forget 191 | ```php 192 | /** 193 | * Forget a value from the blink cache. 194 | * 195 | * This function has support for the '*' wildcard. 196 | * 197 | * @param string $key 198 | * 199 | * @return $this 200 | */ 201 | public function forget(string $key) 202 | ``` 203 | 204 | ### flush 205 | ```php 206 | /** 207 | * Flush all values from the blink cache. 208 | * 209 | * @return $this 210 | */ 211 | public function flush() 212 | ``` 213 | 214 | ### flushStartingWith 215 | ```php 216 | /** 217 | * Flush all values from the blink cache which keys start with the specified value. 218 | * 219 | * @param string $startingWith 220 | * 221 | * @return $this 222 | */ 223 | public function flushStartingWith(string $startingWith) 224 | ``` 225 | 226 | ### pull 227 | ```php 228 | /** 229 | * Get and forget a value from the blink cache. 230 | * 231 | * This function has support for the '*' wildcard. 232 | * 233 | * @param string $name 234 | * 235 | * @return null|string 236 | */ 237 | public function pull(string $name) 238 | ``` 239 | 240 | ### increment 241 | ```php 242 | /** 243 | * Increment a value from the blink cache. 244 | * 245 | * @param string $name 246 | * @param int $by 247 | * 248 | * @return int|null|string 249 | */ 250 | public function increment(string $name, int $by = 1) 251 | ``` 252 | 253 | ### decrement 254 | ```php 255 | /** 256 | * Decrement a value from the blink cache. 257 | * 258 | * @param string $name 259 | * @param int $by 260 | * 261 | * @return int|null|string 262 | */ 263 | public function decrement(string $name, int $by = 1) 264 | ``` 265 | 266 | ## Changelog 267 | 268 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 269 | 270 | ## Testing 271 | 272 | ``` bash 273 | $ composer test 274 | ``` 275 | 276 | ## Contributing 277 | 278 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 279 | 280 | ## Security 281 | 282 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 283 | 284 | ## Credits 285 | 286 | - [Freek Van der Herten](https://github.com/freekmurze) 287 | - [All Contributors](../../contributors) 288 | 289 | We 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. 290 | 291 | ## License 292 | 293 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 294 | -------------------------------------------------------------------------------- /src/Blink.php: -------------------------------------------------------------------------------- 1 | values = $values; 38 | } 39 | 40 | /** 41 | * Put a value in the store. 42 | * 43 | * @param string|array $key 44 | * @param mixed $value 45 | * 46 | * @return $this 47 | */ 48 | public function put($key, $value = null) 49 | { 50 | $newValues = $key; 51 | 52 | if (is_array($key)) { 53 | $this->values = $this->values + $newValues; 54 | } else { 55 | $this->values[$key] = $value; 56 | } 57 | 58 | return $this; 59 | } 60 | 61 | /** 62 | * Get a value from the store. 63 | * 64 | * This function has support for the '*' wildcard. 65 | * 66 | * @param string $key 67 | * @param mixed $default 68 | * 69 | * @return null|string|array 70 | */ 71 | public function get(string $key, $default = null) 72 | { 73 | if ($this->stringContainsWildcard($key)) { 74 | $values = $this->getValuesForKeys($this->getKeysMatching($key)); 75 | 76 | return count($values) ? $values : $default; 77 | } 78 | 79 | return $this->has($key) 80 | ? $this->values[$key] 81 | : $default; 82 | } 83 | 84 | /** 85 | * Determine if the store has a value for the given name. 86 | * 87 | * This function has support for the '*' wildcard. 88 | */ 89 | public function has(string $key): bool 90 | { 91 | if ($this->stringContainsWildcard($key)) { 92 | return count($this->getKeysMatching($key)) > 0; 93 | } 94 | 95 | return array_key_exists($key, $this->values); 96 | } 97 | 98 | /** 99 | * Get all values from the store. 100 | * 101 | * @return array 102 | */ 103 | public function all(): array 104 | { 105 | return $this->values; 106 | } 107 | 108 | /** 109 | * Get all keys starting with a given string from the store. 110 | * 111 | * @param string $startingWith 112 | * 113 | * @return array 114 | */ 115 | public function allStartingWith(string $startingWith = ''): array 116 | { 117 | $values = $this->all(); 118 | 119 | if ($startingWith === '') { 120 | return $values; 121 | } 122 | 123 | return $this->filterKeysStartingWith($values, $startingWith); 124 | } 125 | 126 | /** 127 | * Forget a value from the store. 128 | * 129 | * This function has support for the '*' wildcard. 130 | * 131 | * @param string $key 132 | * 133 | * @return $this 134 | */ 135 | public function forget(string $key) 136 | { 137 | $keys = $this->stringContainsWildcard($key) 138 | ? $this->getKeysMatching($key) 139 | : [$key]; 140 | 141 | foreach ($keys as $key) { 142 | unset($this->values[$key]); 143 | } 144 | 145 | return $this; 146 | } 147 | 148 | /** 149 | * Flush all values from the store. 150 | * 151 | * @return $this 152 | */ 153 | public function flush() 154 | { 155 | return $this->values = []; 156 | } 157 | 158 | /** 159 | * Flush all values from the store which keys start with a given string. 160 | * 161 | * @param string $startingWith 162 | * 163 | * @return $this 164 | */ 165 | public function flushStartingWith(string $startingWith = '') 166 | { 167 | $newContent = []; 168 | 169 | if ($startingWith !== '') { 170 | $newContent = $this->filterKeysNotStartingWith($this->all(), $startingWith); 171 | } 172 | 173 | $this->values = $newContent; 174 | 175 | return $this; 176 | } 177 | 178 | /** 179 | * Get and forget a value from the store. 180 | * 181 | * This function has support for the '*' wildcard. 182 | * 183 | * @param string $key 184 | * 185 | * @return null|string 186 | */ 187 | public function pull(string $key) 188 | { 189 | $value = $this->get($key); 190 | 191 | $this->forget($key); 192 | 193 | return $value; 194 | } 195 | 196 | /** 197 | * Increment a value from the store. 198 | * 199 | * @param string $key 200 | * @param int $by 201 | * 202 | * @return int|null|string 203 | */ 204 | public function increment(string $key, int $by = 1) 205 | { 206 | $currentValue = $this->get($key) ?? 0; 207 | 208 | $newValue = $currentValue + $by; 209 | 210 | $this->put($key, $newValue); 211 | 212 | return $newValue; 213 | } 214 | 215 | /** 216 | * Decrement a value from the store. 217 | * 218 | * @param string $key 219 | * @param int $by 220 | * 221 | * @return int|null|string 222 | */ 223 | public function decrement(string $key, int $by = 1) 224 | { 225 | return $this->increment($key, $by * -1); 226 | } 227 | 228 | /** 229 | * Whether a offset exists. 230 | * 231 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php 232 | * 233 | * @param mixed $offset 234 | * 235 | * @return bool 236 | */ 237 | public function offsetExists($offset): bool 238 | { 239 | return $this->has($offset); 240 | } 241 | 242 | /** 243 | * Offset to retrieve. 244 | * 245 | * @link http://php.net/manual/en/arrayaccess.offsetget.php 246 | * 247 | * @param mixed $offset 248 | * 249 | * @return mixed 250 | */ 251 | public function offsetGet($offset): mixed 252 | { 253 | return $this->get($offset); 254 | } 255 | 256 | /** 257 | * Offset to set. 258 | * 259 | * @link http://php.net/manual/en/arrayaccess.offsetset.php 260 | * 261 | * @param mixed $offset 262 | * @param mixed $value 263 | */ 264 | public function offsetSet($offset, $value): void 265 | { 266 | $this->put($offset, $value); 267 | } 268 | 269 | /** 270 | * Offset to unset. 271 | * 272 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php 273 | * 274 | * @param mixed $offset 275 | */ 276 | public function offsetUnset($offset): void 277 | { 278 | $this->forget($offset); 279 | } 280 | 281 | /** 282 | * Count elements. 283 | * 284 | * @link http://php.net/manual/en/countable.count.php 285 | * 286 | * @return int 287 | */ 288 | public function count(): int 289 | { 290 | return count($this->all()); 291 | } 292 | 293 | /** 294 | * Only if the given key is not present in the blink cache the callable will be executed. 295 | * 296 | * The result of the callable will be stored in the given key and returned. 297 | * 298 | * @param $key 299 | * @param callable $callable 300 | * 301 | * @return mixed 302 | */ 303 | public function once($key, callable $callable) 304 | { 305 | if (! $this->has($key)) { 306 | $this->put($key, $callable()); 307 | } 308 | 309 | return $this->get($key); 310 | } 311 | 312 | /** 313 | * Use the "once" method only if the given condition is true. 314 | * 315 | * Otherwise, the callable will be executed. 316 | * 317 | * @return mixed 318 | */ 319 | public function onceIf($shouldBlink, $key, callable $callable) 320 | { 321 | return $shouldBlink ? $this->once($key, $callable) : $callable(); 322 | } 323 | 324 | protected function filterKeysStartingWith(array $values, string $startsWith): array 325 | { 326 | return array_filter($values, function ($key) use ($startsWith) { 327 | return $this->startsWith($key, $startsWith); 328 | }, ARRAY_FILTER_USE_KEY); 329 | } 330 | 331 | protected function filterKeysNotStartingWith(array $values, string $startsWith): array 332 | { 333 | return array_filter($values, function ($key) use ($startsWith) { 334 | return ! $this->startsWith($key, $startsWith); 335 | }, ARRAY_FILTER_USE_KEY); 336 | } 337 | 338 | protected function startsWith(string $haystack, string $needle): bool 339 | { 340 | return substr($haystack, 0, strlen($needle)) === $needle; 341 | } 342 | 343 | protected function getKeysMatching(string $pattern): array 344 | { 345 | $keys = array_keys($this->values); 346 | 347 | return array_filter($keys, function ($key) use ($pattern) { 348 | return fnmatch($pattern, $key, FNM_NOESCAPE); 349 | }); 350 | } 351 | 352 | protected function stringContainsWildcard(string $string): bool 353 | { 354 | return $this->stringContains($string, '*'); 355 | } 356 | 357 | /** 358 | * @param string $haystack 359 | * @param string|array $needles 360 | * 361 | * @return bool 362 | */ 363 | protected function stringContains(string $haystack, $needles): bool 364 | { 365 | foreach ((array) $needles as $needle) { 366 | if ($needle != '' && mb_strpos($haystack, $needle) !== false) { 367 | return true; 368 | } 369 | } 370 | 371 | return false; 372 | } 373 | 374 | public function getValuesForKeys(array $keys): array 375 | { 376 | return array_filter($this->values, function ($key) use ($keys) { 377 | return in_array($key, $keys); 378 | }, ARRAY_FILTER_USE_KEY); 379 | } 380 | } 381 | --------------------------------------------------------------------------------