CakePHP
  • 文档
    • 书籍
    • API
    • 视频
    • 报告安全问题
    • 隐私政策
    • 标识和商标
  • 商业解决方案
  • 周边产品
  • 公路旅行
  • 团队
  • 社区
    • 社区
    • 参与
    • 问题(Github)
    • 烘焙坊
    • 精选资源
    • 培训
    • 聚会
    • 我的 CakePHP
    • CakeFest
    • 时事通讯
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • 帮助和支持
    • 论坛
    • Stack Overflow
    • IRC
    • Slack
    • 付费支持
CakePHP

C CakePHP 5.1 Chiffon API

  • 项目
    • CakePHP
      • CakePHP
      • Chronos
      • Elastic Search
      • 队列
  • 版本
    • 5.1
      • 5.1
      • 5.0
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

命名空间

  • 全局
  • Cake
    • 缓存
      • 引擎
      • 异常
    • 集合
    • 命令
    • 控制台
    • 控制器
    • 核心
    • 数据库
    • 数据源
    • 错误
    • 事件
    • 表单
    • Http
    • I18n
    • 日志
    • 邮件器
    • 网络
    • ORM
    • 路由
    • 测试套件
    • 实用程序
    • 验证
    • 视图

类缓存

缓存为您的应用程序中的缓存提供了一个一致的接口。它允许您使用多种不同的缓存引擎,而不会将您的应用程序耦合到特定的实现。它还允许您更改缓存存储或配置,而不会影响应用程序的其他部分。

配置缓存引擎

您可以在应用程序的 Config/cache.php 文件中配置缓存引擎。示例配置如下

Cache::config('shared', [
   'className' => Cake\Cache\Engine\ApcuEngine::class,
   'prefix' => 'my_app_'
]);

这将为 'shared' 别名配置一个 APCu 缓存引擎。然后,您可以使用它作为各种 Cache 方法中的 $config 参数来读写该缓存别名。

通常,所有缓存引擎都支持所有缓存操作。但是,Cache::increment() 和 Cache::decrement() 不受文件缓存支持。

有 7 个内置缓存引擎

  • ApcuEngine - 使用 APCu 对象缓存,它是速度最快的缓存引擎之一。
  • ArrayEngine - 只使用内存来存储所有数据,实际上不是一个持久引擎。在测试或 CLI 环境中可能很有用。
  • FileEngine - 使用简单文件来存储内容。性能较差,但适合存储大型对象,或对 IO 不敏感的对象。非常适合开发,因为它易于检查和手动清除缓存。
  • MemcacheEngine - 使用 PECL::Memcache 扩展和 Memcached 进行存储。读写速度快,并且得益于 Memcache 的分布式特性。
  • RedisEngine - 使用 redis 和 php-redis 扩展来存储缓存数据。
  • XcacheEngine - 使用 Xcache 扩展,它是 APCu 的替代方案。

有关预期配置键,请参见缓存引擎文档。

命名空间: Cake\Cache
参见: config/app.php 以获取配置设置

属性摘要

  • $_config protected static
    array<string|int, array<string, mixed>>

    配置集。

  • $_dsnClassMap protected static
    array<string, string>

    一个将 URL 方案映射到完全限定的缓存引擎类名的数组。

  • $_enabled protected static
    bool

    用于跟踪是否启用了缓存的标志。

  • $_groups protected static
    array<string, array>

    组到配置映射

  • $_registry protected static
    Cake\Cache\CacheRegistry

    用于创建和使用缓存适配器的缓存注册表。

方法摘要

  • _buildEngine() protected static

    查找并构建所需引擎类的实例。

  • add() public static

    如果缓存引擎中不存在,则将键的数据写入缓存引擎。

  • clear() public static

    从缓存中删除所有键。

  • clearAll() public static

    从所有配置的缓存中删除所有键。

  • clearGroup() public static

    从缓存中删除属于同一组的所有键。

  • configured() public static

    返回一个包含命名配置的数组

  • decrement() public static

    递减键下的数字,并返回递减后的值。

  • delete() public static

    从缓存中删除一个键。

  • deleteMany() public static

    从缓存中删除多个键。

  • disable() public static

    禁用缓存。

  • drop() public static

    删除构造的适配器。

  • enable() public static

    重新启用缓存。

  • enabled() public static

    检查是否启用了缓存。

  • getConfig() public static

    读取现有配置。

  • getConfigOrFail() public static

    读取特定键的现有配置。

  • getDsnClassMap() public static

    返回此类的 DSN 类映射。

  • getRegistry() public static

    返回用于创建和使用缓存适配器的缓存注册表实例。

  • groupConfigs() public static

    检索组名称到配置映射。

  • increment() public static

    递增键下的数字,并返回递增后的值。

  • parseDsn() public static

    将 DSN 解析为有效的连接配置

  • pool() public static

    为命名的缓存池获取一个 SimpleCacheEngine 对象。

  • read() public static

    从缓存中读取一个键。

  • readMany() public static

    从缓存中读取多个键。

  • remember() public static

    提供了一种轻松执行读穿缓存的能力。

  • setConfig() public static

    此方法可用于为应用程序定义配置适配器。

  • setDsnClassMap() public static

    更新此类的 DSN 类映射。

  • setRegistry() public static

    设置用于创建和使用缓存适配器的缓存注册表实例。

  • write() public static

    将键的数据写入缓存。

  • writeMany() public static

    将多个键的数据写入缓存。

方法详情

_buildEngine() ¶ protected static

_buildEngine(string $name): void

查找并构建所需引擎类的实例。

参数
string $name

需要构建引擎实例的配置数组的名称

返回
void
抛出
Cake\Cache\Exception\InvalidArgumentException
当无法创建缓存引擎时。
RuntimeException
如果引擎加载失败。

add() ¶ public static

add(string $key, mixed $value, string $config = 'default'): bool

如果缓存引擎中不存在,则将键的数据写入缓存引擎。

用法

写入活动缓存配置

Cache::add('cached_data', $data);

写入特定缓存配置

Cache::add('cached_data', $data, 'long_term');
参数
string $key

数据的标识符。

mixed $value

要缓存的数据 - 除资源外的任何东西。

string $config optional

要写入的可选字符串配置名称。默认为 'default'。

返回
bool

clear() ¶ public static

clear(string $config = 'default'): bool

从缓存中删除所有键。

参数
string $config optional

要使用的配置的名称。默认为 'default'

返回
bool

clearAll() ¶ public static

clearAll(): array<string, bool>

从所有配置的缓存中删除所有键。

返回
array<string, bool>

clearGroup() ¶ public static

clearGroup(string $group, string $config = 'default'): bool

从缓存中删除属于同一组的所有键。

参数
string $group

要清除的组的名称

string $config optional

要使用的配置的名称。默认为 'default'

返回
bool

configured() ¶ public static

configured(): list<string>

返回一个包含命名配置的数组

返回
list<string>

decrement() ¶ public static

decrement(string $key, int $offset = 1, string $config = 'default'): int|false

递减键下的数字,并返回递减后的值。

参数
string $key

数据的标识符

int $offset optional

要减去的数量

string $config optional

可选字符串配置名称。默认为 'default'

返回
int|false
抛出
Cake\Cache\Exception\InvalidArgumentException
当 offset < 0 时

delete() ¶ public static

delete(string $key, string $config = 'default'): bool

从缓存中删除一个键。

用法

从活动缓存配置中删除。

Cache::delete('my_data');

从特定缓存配置中删除。

Cache::delete('my_data', 'long_term');
参数
string $key

数据的标识符

string $config optional

要使用的配置的名称。默认为 'default'

返回
bool

deleteMany() ¶ public static

deleteMany(iterable $keys, string $config = 'default'): bool

从缓存中删除多个键。

用法

从活动缓存配置中删除多个键。

Cache::deleteMany(['my_data_1', 'my_data_2']);

从特定缓存配置中删除。

Cache::deleteMany(['my_data_1', 'my_data_2], 'long_term');
参数
iterable $keys

要删除的缓存键的数组或可遍历对象

string $config optional

要使用的配置的名称。默认为 'default'

返回
bool
抛出
Cake\Cache\Exception\InvalidArgumentException

disable() ¶ public static

disable(): void

禁用缓存。

禁用后,所有缓存操作将返回 null。

返回
void

drop() ¶ public static

drop(string $config): bool

删除构造的适配器。

如果您希望修改现有配置,则应将其删除、更改配置,然后重新添加。

如果实现对象支持 $_registry 对象,则命名配置也将从注册表中卸载。

参数
string $config

您希望删除的现有配置。

返回
bool

enable() ¶ public static

enable(): void

重新启用缓存。

如果使用 Cache::disable() 禁用了缓存,此方法将反转该效果。

返回
void

enabled() ¶ public static

enabled(): bool

检查是否启用了缓存。

返回
bool

getConfig() ¶ public static

getConfig(string $key): mixed|null

读取现有配置。

参数
string $key

配置的名称。

返回
mixed|null

getConfigOrFail() ¶ public static

getConfigOrFail(string $key): mixed

读取特定键的现有配置。

此键的配置值必须存在,绝不能为 null。

参数
string $key

配置的名称。

返回
mixed
抛出
InvalidArgumentException
如果值不存在。

getDsnClassMap() ¶ public static

getDsnClassMap(): array<string, class-string>

返回此类的 DSN 类映射。

返回
array<string, class-string>

getRegistry() ¶ public static

getRegistry(): Cake\Cache\CacheRegistry

返回用于创建和使用缓存适配器的缓存注册表实例。

返回
Cake\Cache\CacheRegistry

groupConfigs() ¶ public static

groupConfigs(string|null $group = null): array<string, array>

检索组名称到配置映射。

Cache::config('daily', ['duration' => '1 day', 'groups' => ['posts']]);
Cache::config('weekly', ['duration' => '1 week', 'groups' => ['posts', 'archive']]);
$configs = Cache::groupConfigs('posts');

$configs 将等于 ['posts' => ['daily', 'weekly']] 调用此方法将加载所有配置的引擎。

参数
string|null $group optional

组名或 null 用于检索所有组映射

返回
array<string, array>
抛出
Cake\Cache\Exception\InvalidArgumentException

increment() ¶ public static

increment(string $key, int $offset = 1, string $config = 'default'): int|false

递增键下的数字,并返回递增后的值。

参数
string $key

数据的标识符

int $offset optional

要添加的多少

string $config optional

可选字符串配置名称。默认为 'default'

返回
int|false
抛出
Cake\Cache\Exception\InvalidArgumentException
当 offset < 0 时

parseDsn() ¶ public static

parseDsn(string $dsn): array<string, mixed>

将 DSN 解析为有效的连接配置

此方法允许使用类似于 PEAR::DB 使用的格式设置 DSN。以下是如何使用它的示例

$dsn = 'mysql://user:pass@localhost/database?';
$config = ConnectionManager::parseDsn($dsn);

$dsn = 'Cake\Log\Engine\FileLog://?types=notice,info,debug&file=debug&path=LOGS';
$config = Log::parseDsn($dsn);

$dsn = 'smtp://user:secret@localhost:25?timeout=30&client=null&tls=null';
$config = Email::parseDsn($dsn);

$dsn = 'file:///?className=\My\Cache\Engine\FileEngine';
$config = Cache::parseDsn($dsn);

$dsn = 'File://?prefix=myapp_cake_translations_&serialize=true&duration=+2 minutes&path=/tmp/persistent/';
$config = Cache::parseDsn($dsn);

对于所有类,scheme 的值都设置为 className 的值,除非它们已另行指定。

请注意,查询字符串参数也会被解析并作为返回配置中的值设置。

参数
string $dsn

要转换为配置数组的 DSN 字符串

返回
array<string, mixed>
抛出
InvalidArgumentException
如果没有传递字符串,或者传递了无效字符串

pool() ¶ public static

pool(string $config): Psr\SimpleCache\CacheInterfaceCake\Cache\CacheEngineInterface

为命名的缓存池获取一个 SimpleCacheEngine 对象。

参数
string $config

配置的缓存后端的名称。

返回
Psr\SimpleCache\CacheInterfaceCake\Cache\CacheEngineInterface

read() ¶ public static

read(string $key, string $config = 'default'): mixed

从缓存中读取一个键。

用法

从活动缓存配置中读取。

Cache::read('my_data');

从特定缓存配置中读取。

Cache::read('my_data', 'long_term');
参数
string $key

数据的标识符

string $config optional

要使用的配置的可选名称。默认为 'default'

返回
mixed

readMany() ¶ public static

readMany(iterable $keys, string $config = 'default'): iterable

从缓存中读取多个键。

用法

从活动缓存配置中读取多个键。

Cache::readMany(['my_data_1', 'my_data_2]);

从特定缓存配置中读取。

Cache::readMany(['my_data_1', 'my_data_2], 'long_term');
参数
iterable $keys

要从缓存中获取的键的数组或可遍历对象

string $config optional

要使用的配置的可选名称。默认为 'default'

返回
iterable
抛出
Cake\Cache\Exception\InvalidArgumentException

remember() ¶ public static

remember(string $key, Closure $default, string $config = 'default'): mixed

提供了一种轻松执行读穿缓存的能力。

如果键未设置,则运行默认回调以获取默认值。然后,结果将存储在 key 处的缓存配置中。

示例

使用闭包提供数据,假设 $this 是一个 Table 对象

$results = Cache::remember('all_articles', function () {
     return $this->find('all')->toArray();
});
参数
string $key

要读取/存储数据的缓存键。

Closure $default

在缓存键为空的情况下提供数据的回调。

string $config optional

要用于此操作的缓存配置。默认为 default。

返回
mixed

setConfig() ¶ public static

setConfig(array<string, mixed>|string $key, mixed $config = null): void

此方法可用于为应用程序定义配置适配器。

要更改适配器的配置在运行时,首先删除适配器,然后重新配置它。

适配器在执行第一个操作之前不会被构造。

用法

假设类的名称是 Cache,则支持以下情况

设置缓存引擎。

Cache::setConfig('default', $settings);

注入已构造的适配器

Cache::setConfig('default', $instance);

一次配置多个适配器

Cache::setConfig($arrayOfConfig);
参数
array<string, mixed>|string $key

配置的名称,或多个配置的数组。

mixed $config optional

配置值。通常是 name => 适配器配置数据的数组。

返回
void
抛出
BadMethodCallException
当尝试修改现有配置时。
LogicException
当尝试存储结构无效的配置数组时。

setDsnClassMap() ¶ public static

setDsnClassMap(array<string, string> $map): void

更新此类的 DSN 类映射。

参数
array<string, string> $map

要应用的类映射的添加/编辑。

返回
void

setRegistry() ¶ public static

setRegistry(Cake\Cache\CacheRegistry $registry): void

设置用于创建和使用缓存适配器的缓存注册表实例。

还允许注入新的注册表实例。

参数
Cake\Cache\CacheRegistry $registry

可注入的注册表对象。

返回
void

write() ¶ public static

write(string $key, mixed $value, string $config = 'default'): bool

将键的数据写入缓存。

用法

写入活动缓存配置

Cache::write('cached_data', $data);

写入特定缓存配置

Cache::write('cached_data', $data, 'long_term');
参数
string $key

数据的标识符

mixed $value

要缓存的数据 - 除资源外的任何内容

string $config optional

要写入的可选字符串配置名称。默认为 'default'

返回
bool

writeMany() ¶ public static

writeMany(iterable $data, string $config = 'default'): bool

将多个键的数据写入缓存。

用法

写入活动缓存配置

Cache::writeMany(['cached_data_1' => 'data 1', 'cached_data_2' => 'data 2']);

写入特定缓存配置

Cache::writeMany(['cached_data_1' => 'data 1', 'cached_data_2' => 'data 2'], 'long_term');
参数
iterable $data

要存储在缓存中的数据的数组或可遍历对象

string $config optional

要写入的可选字符串配置名称。默认为 'default'

返回
bool
抛出
Cake\Cache\Exception\InvalidArgumentException

属性详情

$_config ¶ protected static

配置集。

类型
array<string|int, array<string, mixed>>

$_dsnClassMap ¶ protected static

一个将 URL 方案映射到完全限定的缓存引擎类名的数组。

类型
array<string, string>

$_enabled ¶ protected static

用于跟踪是否启用了缓存的标志。

类型
bool

$_groups ¶ protected static

组到配置映射

类型
array<string, array>

$_registry ¶ protected static

用于创建和使用缓存适配器的缓存注册表。

类型
Cake\Cache\CacheRegistry
OpenHub
Pingping
Linode
  • 商业解决方案
  • 展示
  • 文档
  • 书籍
  • API
  • 视频
  • 报告安全问题
  • 隐私政策
  • 标识和商标
  • 社区
  • 参与
  • 问题(Github)
  • 烘焙坊
  • 精选资源
  • 培训
  • 聚会
  • 我的 CakePHP
  • CakeFest
  • 时事通讯
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • 帮助和支持
  • 论坛
  • Stack Overflow
  • IRC
  • Slack
  • 付费支持

使用 CakePHP API 文档 生成