PHPackages                             spatie/laravel-blink - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. spatie/laravel-blink

ActiveLibrary[Caching](/categories/caching)

spatie/laravel-blink
====================

Cache that expires in the blink of an eye

1.7.2(2mo ago)1621.1M—9.7%1410MITPHPPHP ^7.4|^8.0CI passing

Since Mar 17Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/spatie/laravel-blink)[ Packagist](https://packagist.org/packages/spatie/laravel-blink)[ Docs](https://github.com/spatie/laravel-blink)[ Fund](https://spatie.be/open-source/support-us)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-laravel-blink/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (23)Used By (10)

Cache that expires in the blink of an eye
=========================================

[](#cache-that-expires-in-the-blink-of-an-eye)

[![Latest Version on Packagist](https://camo.githubusercontent.com/93e9fe4a7ededa80f69d69584320d1bb6841a94fc5d273ba7431cd2068cb46c2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d626c696e6b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-blink)[![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)[![Check & fix styling](https://github.com/spatie/laravel-blink/workflows/Check%20&%20fix%20styling/badge.svg)](https://github.com/spatie/laravel-blink/workflows/Check%20&%20fix%20styling/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/876064511d8a77d6a59e6cbcf5364cc843504e5be9c283a4674ff7f2f9cdf267/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d626c696e6b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-blink)

This 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.

```
blink()->put('key', 'value');

blink()->get('key'); // Returns 'value'
blink()->get('prefix*'); // Returns an array of values whose keys start with 'prefix'

// once will only execute the given callable if the given key didn't exist yet
$expensiveFunction = function() {
   return rand();
});
blink()->once('random', $expensiveFunction); // returns random number
blink()->once('random', $expensiveFunction); // returns the same number

blink()->has('key'); // Returns true
blink()->has('prefix*'); // Returns true if the blink contains a key that starts with 'prefix'

// Specify a default value for when the specified key does not exist
blink()->get('non existing key', 'default') // Returns 'default'

blink()->put('anotherKey', 'anotherValue');

// Put multiple items in one go
blink()->put(['ringo' => 'drums', 'paul' => 'bass']);

blink()->all(); // Returns an array with all items

blink()->forget('key'); // Removes the item
blink()->forget('prefix*'); // Forget all items of which the key starts with 'prefix'

blink()->flush(); // Empty the entire blink

blink()->flushStartingWith('somekey'); // Remove all items whose keys start with "somekey"

blink()->increment('number'); // blink()->get('number') will return 1
blink()->increment('number'); // blink()->get('number') will return 2
blink()->increment('number', 3); // blink()->get('number') will return 5

// Blink implements ArrayAccess
blink()['key'] = 'value';
blink()['key']; // Returns 'value'
isset(blink()['key']); // Returns true
unset(blink()['key']); // Equivalent to removing the value

// Blink implements Countable
count(blink()); // Returns 0
blink()->put('key', 'value');
count(blink()); // Returns 1
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/16339217513f7f9f92039f857a800474a75242bd8cc36e464383c9c40951ae43/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d626c696e6b2e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-blink)

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).

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).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-blink
```

To enable the package, register the service provider, and optionally register the facade:

```
// config/app.php

'providers' => [
    // ...
    Spatie\LaravelBlink\BlinkServiceProvider::class,
],

'aliases' => [
    ...
    'Blink' => Spatie\LaravelBlink\BlinkFacade::class,
],
```

Usage
-----

[](#usage)

A `Blink` instance can just be newed up.

```
$blink = new \Spatie\Blink\Blink()
```

You can call the following methods on it:

### put

[](#put)

```
/**
 * Put a value in the blink cache.
 *
 * @param string|array $name
 * @param string|int|null $value
 *
 * @return $this
 */
public function put($name, $value = null)
```

### get

[](#get)

```
/**
 * Get a value from the blink cache.
 *
 * This function has support for the '*' wildcard.
 *
 * @param string $name
 *
 * @return null|string
 */
public function get(string $name)
```

### has

[](#has)

```
/*
 * Determine if the blink cache has a value for the given name.
 *
 * This function has support for the '*' wildcard.
 */
public function has(string $name) : bool
```

### once

[](#once)

```
/**
 * Only if the given key is not present in the blink cache the callable will be executed.
 *
 * The result of the callable will be stored in the given key and returned.
 *
 * @param $key
 * @param callable $callable
 *
 * @return mixed
 */
```

You can use this to avoid using static variables to cache stuff.

This piece of code

```
function foo()
{
    static $result = null;

    if (is_null($result) {
        $result = ...// do some expensive stuff here
    }

    return $result;
}
```

can be rewritten to

```
function foo()
{
    return blink()->once('fooCache', function() {
       return ... // do some expensive stuff here
    });
}
```

### all

[](#all)

```
/*
* Get all values in the blink cache.
*/
public function all() : array
```

### allStartingWith

[](#allstartingwith)

```
/**
 * Get all values from the blink cache which keys start with the given string.
 *
 * @param string $startingWith
 *
 * @return array
*/
public function allStartingWith(string $startingWith = '') : array
```

### forget

[](#forget)

```
/**
 * Forget a value from the blink cache.
 *
 * This function has support for the '*' wildcard.
 *
 * @param string $key
 *
 * @return $this
 */
public function forget(string $key)
```

### flush

[](#flush)

```
/**
 * Flush all values from the blink cache.
 *
 * @return $this
 */
 public function flush()
```

### flushStartingWith

[](#flushstartingwith)

```
/**
 * Flush all values from the blink cache which keys start with the specified value.
 *
 * @param string $startingWith
 *
 * @return $this
 */
 public function flushStartingWith(string $startingWith)
```

### pull

[](#pull)

```
/**
 * Get and forget a value from the blink cache.
 *
 * This function has support for the '*' wildcard.
 *
 * @param string $name
 *
 * @return null|string
 */
public function pull(string $name)
```

### increment

[](#increment)

```
/**
 * Increment a value from the blink cache.
 *
 * @param string $name
 * @param int $by
 *
 * @return int|null|string
 */
 public function increment(string $name, int $by = 1)
```

### decrement

[](#decrement)

```
/**
 * Decrement a value from the blink cache.
 *
 * @param string $name
 * @param int $by
 *
 * @return int|null|string
 */
 public function decrement(string $name, int $by = 1)
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity55

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 53.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~163 days

Recently: every ~373 days

Total

21

Last Release

86d ago

Major Versions

0.0.1 → 1.0.02017-03-17

PHP version history (5 changes)0.0.1PHP ^7.1

1.0.0PHP ^7.0

1.2.0PHP ^7.2

1.4.0PHP ^7.4

1.5.0PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (57 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (8 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (8 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (8 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (7 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (3 commits)")[![rubenvanassche](https://avatars.githubusercontent.com/u/619804?v=4)](https://github.com/rubenvanassche "rubenvanassche (3 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (3 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (2 commits)")[![ziming](https://avatars.githubusercontent.com/u/679513?v=4)](https://github.com/ziming "ziming (1 commits)")[![jpuck](https://avatars.githubusercontent.com/u/15305396?v=4)](https://github.com/jpuck "jpuck (1 commits)")[![m1guelpf](https://avatars.githubusercontent.com/u/23558090?v=4)](https://github.com/m1guelpf "m1guelpf (1 commits)")[![rdarcy1](https://avatars.githubusercontent.com/u/15962421?v=4)](https://github.com/rdarcy1 "rdarcy1 (1 commits)")[![danjdewhurst](https://avatars.githubusercontent.com/u/25198612?v=4)](https://github.com/danjdewhurst "danjdewhurst (1 commits)")

---

Tags

cachelaravelperformancephpspatielaravel-blink

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-laravel-blink/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-blink/health.svg)](https://phpackages.com/packages/spatie-laravel-blink)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[spatie/once

A magic memoization function

1.4k29.1M79](/packages/spatie-once)[spatie/valuestore

Easily store some values

7661.0M46](/packages/spatie-valuestore)[spatie/blink

Cache that expires in the blink of an eye

1685.0M8](/packages/spatie-blink)[spatie/laravel-varnish

Making Varnish and Laravel play nice together

418235.0k3](/packages/spatie-laravel-varnish)[laragear/cache-query

Remember your query results using only one method. Yes, only one.

272122.8k](/packages/laragear-cache-query)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
