2017 © Pedro Peláez
 

library blink

Cache that expires in the blink of an eye

image

spatie/blink

Cache that expires in the blink of an eye

  • Saturday, March 18, 2017
  • by Spatie
  • Repository
  • 3 Watchers
  • 38 Stars
  • 16,176 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 5 Versions
  • 13 % Grown

The README.md

Cache that expires in the blink of an eye

Latest Version on Packagist run-tests Total Downloads, (*1)

This package contains a class called Blink that can cache values. The cache only spans the length of a single request., (*2)

It can be used like this:, (*3)

$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., (*4)

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

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

Read the usage section of this readme to learn the other methods., (*5)

Support us

, (*6)

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., (*7)

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., (*8)

Installation

You can install the package via composer:, (*9)

``` bash composer require spatie/blink, (*10)


## Usage A `Blink` instance can just be newed up. ```php $blink = new \Spatie\Blink\Blink()

You can call the following methods on it:, (*11)

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

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

/*
 * 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 what has changed recently., (*12)

Testing

bash $ composer test, (*13)

Contributing

Please see CONTRIBUTING for details., (*14)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*15)

Credits

We got the idea and the name for this package from Statamic's Blink helper. We reached out to them and got permission for using the blink name., (*16)

License

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

The Versions

18/03 2017

dev-master

9999999-dev https://github.com/spatie/blink

Cache that expires in the blink of an eye

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

cache caching spatie blink

17/03 2017

1.0.1

1.0.1.0 https://github.com/spatie/blink

Cache that expires in the blink of an eye

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

cache caching spatie blink

17/03 2017

1.0.0

1.0.0.0 https://github.com/spatie/blink

Cache that expires in the blink of an eye

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

cache caching spatie blink

17/03 2017

0.0.2

0.0.2.0 https://github.com/spatie/blink

Cache that expires in the blink of an eye

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

cache caching spatie blink

14/03 2017

0.0.1

0.0.1.0 https://github.com/spatie/blink

Cache that expires in the blink of an eye

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

cache caching spatie blink