pulkitjalan/multicache

此包已被废弃且不再维护。没有建议的替代包。

为Laravel的缓存驱动和自定义驱动添加数组缓存

0.3.1 2015-09-22 14:07 UTC

This package is auto-updated.

Last update: 2019-09-06 06:54:57 UTC


README

为Laravel的缓存驱动和自定义驱动添加数组缓存。

Build Status Scrutinizer Code Quality Coverage Status License Latest Version Total Downloads

需求

  • PHP >= 5.5.9
  • Laravel = 5.1

Laravel 5.2 本地支持多个项目缓存。

安装

通过 composer 安装 - 编辑你的 composer.json 以要求该包。

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

然后在终端运行 composer update 以获取它。

现在将以下内容添加到你的 config/app.php 文件中的 providers 数组中

PulkitJalan\Cache\Providers\MultiCacheServiceProvider::class

用法

任何现有的缓存驱动和自定义驱动都将能够访问以下新方法

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

大多数现有方法(如 hasgetputforget...)也接受数组作为参数,并将自动运行相关的 Many 函数。如果没有传入数组,则原始方法将返回与之前相同格式的结果。

示例

以下是一些函数使用示例以及它们的返回结果。

存在

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

Cache::hasMany($keys);

// or

Cache::has($keys);

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

获取

$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 方法与 putMany 方法的工作方式略有不同。其中 putMany 方法接受一个键值数组作为第一个参数,以及存储时间为第二个参数,而 put 方法接受两个数组作为前两个参数,以及存储时间为第三个参数。

例如:

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

Cache::putMany($data, 10);

// or

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

忘记

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

Cache::forgetMany($keys);

// or

Cache::forget($keys);

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

它是如何工作的?

对于任何没有底层 Many 方法的驱动,方法将针对数组中的每个项目调用该方法的 Non-Many 版本。

例如,当使用不提供自己的 getMany 方法的 apc 驱动时,如果输入数组中有十个项目,则将调用 get 方法十次。

现在,当使用具有自己的 getMany 方法的 memcached 驱动时,将只调用一次 getMany 方法并返回数据。

目前,只有 MemcachedStoreDatabaseStoreRedisStoreArrayStore 提供自己的 Many 方法。 更多即将到来...