originphp/cache

OriginPHP 缓存

2.0.0 2021-01-04 19:22 UTC

This package is auto-updated.

Last update: 2024-09-05 02:51:22 UTC


README

license build coverage

OriginPHP 缓存库默认支持 ApcuMemcachedRedis,你也可以使用文件缓存来存储大对象或耗时内容。

安装

要安装此包

$ composer require originphp/cache

在你的引导文件中,你可以找到缓存的配置。缓存库可以同时与多个配置和引擎一起工作。

一旦配置完成,使用缓存就非常简单。配置默认缓存存储

Cache::config('default', [
    'engine' => 'File',
    'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
    'prefix' => 'cache_'
    'serialize' => true // set to false if you going to cache strings such as output
    'path' => dirname(__DIR__) . '/cache'
     ]);

缓存

写入

将项目添加到缓存中。

Use Origin\Cache\Cache;

$success = Cache::write('key',$value);

读取

从版本 3.x 开始,当从缓存中读取时,如果没有命中,则返回 null 而不是像旧版本那样返回 false

要从缓存中读取一个项目,如果没有找到项目,它将返回 null

Use Origin\Cache\Cache;

$value = Cache::read('key');

存在

检查缓存中是否存在键

Use Origin\Cache\Cache;

if(Cache::exists('key')){
    $bool = Cache::read('key');
}

删除

项目会根据配置中的持续时间自动删除,但如果您想手动删除项目,则使用删除方法。

Use Origin\Cache\Cache;

Cache::delete('key');

清除缓存

Cache::clear();

启用和禁用缓存

有时您可能需要禁用缓存,当我们禁用时,我们会将引擎切换到 NullEngine,您的程序可以正常工作。

Cache::disable();
Cache::enable();

使用多个配置

无论您是使用多个缓存引擎,还是为单个缓存引擎使用多个配置(例如,短期和长期缓存),缓存实用程序都是灵活的。

您可以得到配置的缓存存储

$cache = Cache::store('long-duration');
$value = $cache->read('My.key');

或者您可以通过传递一个选项数组来告诉缓存对象使用哪个配置

$value = Cache::read('My.key',[
     'config'=>'long-duration'
     ]);

引擎

在所有这些示例中,我们只配置了默认配置,您可以用不同的配置名称代替默认配置。当您使用缓存功能时,默认配置默认使用,除非您指定其他情况。

文件引擎

Cache::config('default', [
    'engine' => 'File',
    'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
    'prefix' => 'cache_'
    'serialize' => true // set to false if you going to cache strings such as output,
    'mode' => 0664
     ]);

Apcu 引擎

Cache::config('default', [
    'engine' => 'Apcu',
    'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
    'prefix' => 'cache_'
     ]);

Memcached 引擎

这是一个使用 Memcached 的简单配置。

Cache::config('default', [
        'engine' => 'Memcached',
        'host' => '127.0.0.1',
        'port' => '11211',
        'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
        'prefix' => 'cache_'
     ]);

如果您的 Memcached 服务器配置了用户名和密码,那么

Cache::config('default', [
        'engine' => 'Memcached',
        'host' => '127.0.0.1',
        'port' => '11211',
        'username' => 'james',
        'password' => 'secret',
        'duration' => '+60 minutes', // 3600,
        'prefix' => 'cache_'
     ]);

如果您打算使用套接字,那么而不是设置主机和端口,请使用 path 键设置套接字的位置。

Cache::config('default', [
     'engine' => 'Memcached',
     'path' => '/var/sockets/memcached'
     ]);

您还可以通过将 persistent 键设置为 true 或一个字符串来使连接持久化,该字符串将是持久 ID。

Memcached 支持服务器池,如果您打算使用它们,则使用 servers 键设置一个数组而不是主机和端口。该数组应与 memcached addservers 兼容。

Redis 引擎

这是一个使用 Redis 的简单配置。

Cache::config('default', [
        'engine' => 'Redis',
        'host' => '127.0.0.1',
        'port' => 6379,
        'duration' => '+60 minutes', // string or number of seconds e.g. 3600,
        'timeout' => 0,
        'prefix' => 'cache_'
     ]);

如果您的 Redis 服务器配置了密码,那么

Cache::config('default', [
        'engine' => 'Redis',
        'host' => '127.0.0.1',
        'port' =>  6379,
        'password' => 'secret',
        'duration' => 3600, // duration can also be string e.g. +60 minutes
        'prefix' => 'cache_'
     ]);

如果您打算使用套接字,那么而不是设置主机和端口,请使用 path 键设置套接字的位置。

Cache::config('default', [
        'engine' => 'Redis',
        'path' => '/var/sockets/redis',
     ]);

您还可以通过将 persistent 键设置为 true 或一个字符串来使连接持久化,该字符串将是持久 ID。

Cache::config('default', [
        'engine' => 'Redis',
        'host' => '127.0.0.1',
        'port' => 6379,
        'persistent' => 'my-app',
        'duration' => 3600, // duration can also be string e.g. +60 minutes
        'timeout' => 0,
        'prefix' => 'cache_'
     ]);

自定义引擎

如果您想与不同的后端一起工作,可以轻松创建自己的。当配置缓存时,不要传递 engine 键,而是使用 className 并包含完整的命名空间。

Cache::config('default', [
    'className' => 'App\Cache\CustomEngine',
    'duration' => 3600,
    'prefix' => 'cache_'
     ]);
namespace App\Cache;
use Origin\Cache\Engine\BaseEngine;
class CustomEngine extends BaseEngine
{

}

安装缓存引擎

命令行说明已在 Ubuntu 18.x 上进行测试。

Apcu

Apcu 已安装在 Docker 容器中,但是如果您需要手动安装,请这样做。

sudo apt-get update
sudo apt-get install php-apcu
sudo echo 'apc.enable_cli=1' >>  /etc/php/7.2/cli/php.ini

Memcached

要在 Docker 容器中安装 Memcached

首先将以下内容添加到 docker-compose.yml 文件中,这将加载 memcached 镜像。

  memcached:
      image: memcached

Dockerfile 中,将 php-memcached 添加到安装 PHP 扩展的行中。

    php-memcached \

然后在 docker-compose 中运行构建命令。

docker-compose build

然后在您的缓存配置中将主机设置为 memcached

在基于 Ubuntu/Debian 的服务器上安装 Memcached

sudo apt-get update
sudo apt-get install memcached
sudo apt-get install php-memcached

Redis

在 Docker 容器中安装 Redis

首先将以下内容添加到 docker-compose.yml 文件中,这将加载 Redis 镜像。

  redis:
      image: redis

Dockerfile 中添加以下行以安装并启用 Redis PHP 扩展。

RUN pecl install redis
RUN echo 'extension=redis.so' >> /etc/php/7.2/apache2/php.ini
RUN echo 'extension=redis.so' >> /etc/php/7.2/cli/php.ini

然后在 docker-compose 中运行构建命令。

docker-compose build

然后在您的缓存配置中将主机设置为 redis

在基于 Ubuntu/Debian 的服务器上安装 Redis

pecl install redis
sudo echo 'extension=redis.so' >> /etc/php/7.2/apache2/php.ini
sudo echo 'extension=redis.so' >> /etc/php/7.2/cli/php.ini

测试

要测试此功能,首先构建 Docker 容器。

docker-compose build

启动容器,并访问 bash。

$ docker-compose up
$ docker-compose run app bash
$ vendor/bin/phpunit