2017 © Pedro Peláez
 

library multicache

Adds array caching to Laravels cache drivers and custom drivers

image

pulkitjalan/multicache

Adds array caching to Laravels cache drivers and custom drivers

  • Wednesday, January 27, 2016
  • by pulkitjalan
  • Repository
  • 2 Watchers
  • 7 Stars
  • 13,352 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 2 % Grown

The README.md

Multicache

Adds array caching to Laravels cache drivers and custom drivers., (*1)

Build Status Scrutinizer Code Quality Coverage Status License Latest Version Total Downloads, (*2)

Requirements

  • PHP >= 5.5.9
  • Laravel = 5.1

Laravel 5.2 natively supports a caching multiple items., (*3)

Installation

Install via composer - edit your composer.json to require the package., (*4)

"require": {
    "pulkitjalan/multicache": "0.3.*"
}

Then run composer update in your terminal to pull it in., (*5)

Now add the following to the providers array in your config/app.php, (*6)

PulkitJalan\Cache\Providers\MultiCacheServiceProvider::class

Usage

Any existing cache drivers and custom drivers will have access to the following new methods:, (*7)

    /**
     * Determine if an array of items exists in the cache.
     *
     * @param  array  $keys
     * @return array
     */
    public function hasMany(array $keys);

    /**
     * Retrieve an array of items from the cache by keys.
     *
     * @param  array  $keys
     * @param  mixed  $default
     * @return array
     */
    public function getMany(array $keys, $default = null);

    /**
     * Retrieve an array of items from the cache and delete them.
     *
     * @param  array  $keys
     * @param  mixed  $default
     * @return array
     */
    public function pullMany(array $keys, $default = null);

    /**
     * Store an array of items in the cache.
     *
     * @param  array  $items
     * @param  \DateTime|int  $minutes
     * @return void
     */
    public function putMany(array $items, $minutes);

    /**
     * Store an array of items in the cache if the key does not exist.
     *
     * @param  array  $items
     * @param  \DateTime|int  $minutes
     * @return bool
     */
    public function addMany(array $items, $minutes);

    /**
     * Store an array of items in the cache indefinitely.
     *
     * @param  array  $items
     * @return void
     */
    public function foreverMany(array $items);

    /**
     * Get an array of items from the cache, or store the default value.
     *
     * @param  array  $keys
     * @param  \DateTime|int  $minutes
     * @param  \Closure  $callback
     * @return mixed
     */
    public function rememberMany(array $keys, $minutes, Closure $callback);

    /**
     * Get an array of items from the cache, or store the default value forever.
     *
     * @param  array  $keys
     * @param  \Closure  $callback
     * @return mixed
     */
    public function searMany(array $keys, Closure $callback);

    /**
     * Get an array of items from the cache, or store the default value forever.
     *
     * @param  array  $keys
     * @param  \Closure  $callback
     * @return mixed
     */
    public function rememberManyForever(array $keys, Closure $callback);

    /**
     * Remove an array of items from the cache.
     *
     * @param  array  $keys
     * @return bool
     */
    public function forgetMany(array $keys);

Most of the existing methods like has, get, put, forget... will also accept an array and automatically run the relevant Many function. As Expected the original methods will return results in the same format as they always have if called without an array., (*8)

Examples

Below are a few examples of how to use the functions and what they return., (*9)

Has

$keys = [
    'one', // exists
    'two', // does not exist
    'three', // exists
];

Cache::hasMany($keys);

// or

Cache::has($keys);

// will return: ['one' => true, 'two' => false, 'three' => true]

Get

$keys = [
    'one', // exists
    'two', // does not exist
    'three', // exists
];

Cache::getMany($keys);

// or

Cache::get($keys);

// will return: ['one' => 'data', 'two' => null, 'three' => 'data']

Put

The put method works a little differently to putMany. Where putMany accepts a key value array as the first parameter and the number of minutes to store for as the second parameter, the put method takes two separate arrays as the first two parameters and minutes as the third parameter., (*10)

Eg:, (*11)

$data = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
];

Cache::putMany($data, 10);

// or

Cache::put(array_keys($data), array_values($data), 10);

Forget

$keys = [
    'one',
    'two',
    'three',
];

Cache::forgetMany($keys);

// or

Cache::forget($keys);

// will return: ['one' => true, 'two' => true, 'three' => true]

How does it work?

For any driver that does not have an underlying Many method, the methods will call the Non-Many version of the method for every item in the array., (*12)

For example, when using the apc driver, which does not offer its own getMany method, then the get method will be called ten times if there are ten items in the input array., (*13)

Now, when using the memcached driver, which does have its own getMany method, then the getMany method will be called once and the data returned., (*14)

Currently the MemcachedStore, DatabaseStore, RedisStore and the ArrayStore are the only ones to offer their own Many methods. More to come soon..., (*15)

The Versions

27/01 2016

dev-master

9999999-dev https://github.com/pulkitjalan/multicache

Adds array caching to Laravels cache drivers and custom drivers

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cache array

22/09 2015

0.3.1

0.3.1.0 https://github.com/pulkitjalan/multicache

Adds array caching to Laravels cache drivers and custom drivers

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cache array

22/09 2015

0.3.0

0.3.0.0 https://github.com/pulkitjalan/multicache

Adds array caching to Laravels cache drivers and custom drivers

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cache array

20/09 2015

0.2.1

0.2.1.0 https://github.com/pulkitjalan/multicache

Adds array caching to Laravels cache drivers and custom drivers

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cache array

19/09 2015

0.2.0

0.2.0.0 https://github.com/pulkitjalan/laravel-multicache

Adds array caching to Laravels cache drivers and custom drivers

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cache

19/09 2015

0.1.0

0.1.0.0 https://github.com/pulkitjalan/laravel-multicache

Adds array caching to Laravels cache drivers and custom drivers

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel cache