All Projects → spatie → Laravel Blink

spatie / Laravel Blink

Licence: mit
Cache that expires in the blink of an eye

Projects that are alternatives of or similar to Laravel Blink

Laravel Partialcache
Blade directive to cache rendered partials in laravel
Stars: ✭ 205 (+79.82%)
Mutual labels:  cache, laravel, performance
Laravel Responsecache
Speed up a Laravel app by caching the entire response
Stars: ✭ 1,874 (+1543.86%)
Mutual labels:  cache, laravel, performance
Wordpress Rest Cache
WordPress Plugin to lazy cache HTTP requests in database and update via cron.
Stars: ✭ 8 (-92.98%)
Mutual labels:  cache, performance
Easy Cache
a cache trait for Laravel to do cache eaily
Stars: ✭ 12 (-89.47%)
Mutual labels:  cache, laravel
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-69.3%)
Mutual labels:  cache, laravel
Once
A magic memoization function
Stars: ✭ 821 (+620.18%)
Mutual labels:  cache, performance
Lazycache
An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
Stars: ✭ 901 (+690.35%)
Mutual labels:  cache, performance
Laravel Couchbase
Couchbase providers for Laravel
Stars: ✭ 31 (-72.81%)
Mutual labels:  cache, laravel
React Esi
React ESI: Blazing-fast Server-Side Rendering for React and Next.js
Stars: ✭ 537 (+371.05%)
Mutual labels:  cache, performance
Kirby3 Autoid
Automatic unique ID for Pages, Files and Structures including performant helpers to retrieve them. Bonus: Tiny-URL.
Stars: ✭ 58 (-49.12%)
Mutual labels:  cache, performance
Ansible Role Memcached
Ansible Role - Memcached
Stars: ✭ 54 (-52.63%)
Mutual labels:  cache, performance
Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (-16.67%)
Mutual labels:  cache, laravel
Django Cachalot
No effort, no worry, maximum performance.
Stars: ✭ 790 (+592.98%)
Mutual labels:  cache, performance
Laravel Repositories
[ABANDONED] Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Stars: ✭ 664 (+482.46%)
Mutual labels:  cache, laravel
Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (-83.33%)
Mutual labels:  cache, laravel
Bloom
🌸 HTTP REST API caching middleware, to be used between load balancers and REST API workers.
Stars: ✭ 553 (+385.09%)
Mutual labels:  cache, performance
Laravel Image Optimizer
Optimize images in your Laravel app
Stars: ✭ 873 (+665.79%)
Mutual labels:  laravel, performance
Laravel Cacheable
Rinvex Cacheable is a granular, intuitive, and fluent caching system for eloquent models. Simple, but yet powerful, plug-n-play with no hassle.
Stars: ✭ 107 (-6.14%)
Mutual labels:  cache, laravel
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (+4552.63%)
Mutual labels:  cache, performance
Laravel Eloquent Query Cache
Adding cache on your Laravel Eloquent queries' results is now a breeze.
Stars: ✭ 529 (+364.04%)
Mutual labels:  cache, laravel

Cache that expires in the blink of an eye

Latest Version on Packagist GitHub Workflow Status Check & fix styling Total Downloads

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
// 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

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

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. We publish all received postcards on our virtual postcard wall.

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

A Blink instance can just be newed up.

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

You can call the following methods on it:

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

/*
 * 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

/**
 * 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

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

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 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 all values from the blink cache.
 *
 * @return $this
 */
 public function flush()

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

/**
 * 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 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 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

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].