PHPackages                             spatie/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/blink

ActiveLibrary[Caching](/categories/caching)

spatie/blink
============

Cache that expires in the blink of an eye

1.4.0(2y ago)1685.0M—0.7%187MITPHPPHP ^8.0CI passing

Since Mar 14Pushed 8mo ago5 watchersCompare

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

READMEChangelog (10)Dependencies (1)Versions (13)Used By (7)

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/cf31b156dcf50d6034095497c5d9d04ffe3d60be9996aa763f0a749b94ce21a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f626c696e6b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/blink)[![run-tests](https://github.com/spatie/blink/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/blink/actions/workflows/run-tests.yml)[![Total Downloads](https://camo.githubusercontent.com/a942da921a1449d14df319566deed8a2ffa80da89e380c0414d44530b5a81b52/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f626c696e6b2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/blink)

This package contains a class called `Blink` that can cache values. The cache only spans the length of a single request.

It can be used like this:

```
$blink = new Blink();

$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
```

If you want to use the same instance within the current request, you can use the static method `global`.

```
Blink::global()->put('key', 'value');

Blink::global()->get('key') // Returns 'value'
```

Read the [usage](#usage) section of this readme to learn the other methods.

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

[](#support-us)

[![](https://camo.githubusercontent.com/3abb5a9d0048ddb315da8a71a28bad3932c2636eff21161175f29cecd1450d1b/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f626c696e6b2e6a70673f743d31)](https://spatie.be/github-ad-click/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/blink
```

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
 */
public function once($key, callable $callable)
```

### onceIf

[](#onceif)

```
/**
 * Use the "once" method only if the given condition is true.
 *
 * Otherwise, the callable will be executed.
 *
 * @param bool $shouldBlink
 * @param $key
 * @param callable
 *
 * @return mixed
 */
public function onceIf($shouldBlink, $key, callable $callable)
```

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

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.

License
-------

[](#license)

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

###  Health Score

56

—

FairBetter than 98% of packages

Maintenance43

Moderate activity, may be stable

Popularity61

Solid adoption and visibility

Community32

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor1

Top contributor holds 64.6% 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 ~210 days

Recently: every ~312 days

Total

12

Last Release

1034d ago

Major Versions

0.0.2 → 1.0.02017-03-17

PHP version history (3 changes)0.0.1PHP ^7.0

1.1.3PHP ^7.3 || ^8.0

1.2.0PHP ^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 (51 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 (5 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (2 commits)")[![Jarlskov](https://avatars.githubusercontent.com/u/392225?v=4)](https://github.com/Jarlskov "Jarlskov (2 commits)")[![jasonvarga](https://avatars.githubusercontent.com/u/105211?v=4)](https://github.com/jasonvarga "jasonvarga (2 commits)")[![adamkelso](https://avatars.githubusercontent.com/u/2157765?v=4)](https://github.com/adamkelso "adamkelso (1 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")[![edalzell](https://avatars.githubusercontent.com/u/6069653?v=4)](https://github.com/edalzell "edalzell (1 commits)")[![joecampo](https://avatars.githubusercontent.com/u/3619398?v=4)](https://github.com/joecampo "joecampo (1 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (1 commits)")[![sfinktah](https://avatars.githubusercontent.com/u/4650770?v=4)](https://github.com/sfinktah "sfinktah (1 commits)")[![shaffe-fr](https://avatars.githubusercontent.com/u/3834222?v=4)](https://github.com/shaffe-fr "shaffe-fr (1 commits)")[![stfndamjanovic](https://avatars.githubusercontent.com/u/22433990?v=4)](https://github.com/stfndamjanovic "stfndamjanovic (1 commits)")

---

Tags

blinkcachephpspatiecachecachingBlink

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[psr/simple-cache

Common interfaces for simple caching

8.1k727.3M2.1k](/packages/psr-simple-cache)[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)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[tedivm/stash

The place to keep your cache.

9824.8M124](/packages/tedivm-stash)[gregwar/cache

A lightweight file-system cache system

1084.5M22](/packages/gregwar-cache)

PHPackages © 2026

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