企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ### 组件说明 > * Lying支持多种缓存,你可以定义一种缓存,或者同时定义多种缓存,只要ID不同就行。 > * 缓存的配置有默认值和选填的选项都是可以省略不写的。 ### 示例配置 #### 文件缓存 配置选项: | 配置名 | 参数类型 | 可选 | 默认值 | 说明 | | --- | --- | --- | --- | --- | | class | string | 是 | lying\cache\FileCache | 可配置 | | dir | string | 是 | DIR_RUNTIME . DS . 'cache' | 缓存文件存放的目录 | | gc | int | 是 | 50 | 垃圾清除的频率,数值为0到100之间,越小回收的越频繁 | | suffix | string | 是 | bin | 文件缓存的后缀名 | | serialize | bool | 是 | true | 是否对数据进行序列化 | 示例配置: ~~~php 'cache' => [ 'class' => 'lying\cache\FileCache', 'dir' => DIR_RUNTIME . DS . 'cache', //缓存文件存放的目录,默认'runtime/cache' 'gc' => 50, //垃圾清除的频率,数值为0到100之间,越小回收的越频繁,默认50 'suffix' => 'bin', //文件缓存的后缀名 'serialize' => true, //是否对数据进行序列化 ], ~~~ #### Apcu缓存 配置选项: | 配置名 | 参数类型 | 可选 | 默认值 | 说明 | | --- | --- | --- | --- | --- | | class | string | 是 | lying\cache\ApcuCache | 可配置 | 示例配置: ~~~php 'apcu' => 'lying\cache\ApcuCache', ~~~ #### Memcached缓存 配置选项: | 配置名 | 参数类型 | 可选 | 默认值 | 说明 | | --- | --- | --- | --- | --- | | class | string | 是 | lying\cache\Memcached | 可配置 | | persistentId | string | 是 | null | 如果配置了此参数则会以长连接打开mencached | | servers | array | 是 | [['127.0.0.1', 11211, 1]] | Memcached服务器连接列表 | | username | string | 是 | '' | 用户名 | | password | string | 是 | '' | 密码 | | options | array | 是 | [] | 额外的memcached选项 | 示例配置: ~~~php 'Memcached' => [ 'class' => 'lying\cache\Memcached', 'persistentId' => false, //长连接ID,如果配置了此参数则会以长连接打开mencached 'servers' => [ ['127.0.0.1', 11211, 1], //Memcached服务器连接列表,选填,默认['127.0.0.1', 11211, 1] ], 'username' => '', //用户名,选填 'password' => '', //密码,选填 'options' => [], //额外的memcached选项 ], ~~~ #### 数组缓存 配置选项: | 配置名 | 参数类型 | 可选 | 默认值 | 说明 | | --- | --- | --- | --- | --- | | class | string | 是 | lying\cache\ArrayCache | 可配置 | 示例配置: ~~~php 'arraycache' => [ 'class' => 'lying\cache\ArrayCache', ], ~~~ * 数组缓存的生命周期是在一次请求里面,所以当请求结束后,里面的缓存数据会全部清空! ### 调用方式 ~~~php \Lying::$maker->get('cacheId'); \Lying::$maker->cache('cacheId'); \Lying::$maker->cacheId; ~~~ ### 方法列表 ~~~php /** * 添加一个缓存,如果缓存已经存在,此次设置的值不会覆盖原来的值,并返回false * @param string $key 缓存的键 * @param mixed $value 缓存的数据 * @param int $ttl 缓存生存时间,默认为0 * @return bool 成功返回true,失败返回false */ public function add($key, $value, $ttl = 0); ~~~ * * * * * ~~~php /** * 添加一组缓存,如果缓存已经存在,此次设置的值不会覆盖原来的值 * @param array $data 一个关联数组,如['name'=>'lying'] * @param int $ttl 缓存生存时间,默认为0 * @return array 返回设置失败的数组,如['name', 'sex'],否则返回空数组 */ public function madd($data, $ttl = 0); ~~~ * * * * * ~~~php /** * 添加一个缓存,如果缓存已经存在,此次缓存会覆盖原来的值并且重新设置生存时间 * @param string $key 缓存的键 * @param mixed $value 缓存的数据 * @param int $ttl 缓存生存时间,默认为0 * @return bool 成功返回true,失败返回false */ public function set($key, $value, $ttl = 0); ~~~ * * * * * ~~~php /** * 添加一组缓存,如果缓存已经存在,此次缓存会覆盖原来的值并且重新设置生存时间 * @param array $data 一个关联数组,如['name' => 'lying'] * @param int $ttl 缓存生存时间,默认为0 * @return array 返回设置失败的数组,如['name', 'sex'],否则返回空数组 */ public function mset($data, $ttl = 0); ~~~ * * * * * ~~~php /** * 从缓存中提取存储的变量 * @param string $key 缓存的键 * @return mixed 成功返回值,失败返回false */ public function get($key); ~~~ * * * * * ~~~php /** * 从缓存中提取一组存储的变量 * @param array $keys 缓存的键数组 * @return array 返回查找到的数据数组,没找到则返回空数组 */ public function mget($keys); ~~~ * * * * * ~~~php /** * 检查缓存是否存在 * @param string $key 要查找的缓存键 * @return bool 如果键存在,则返回true,否则返回false */ public function exists($key); ~~~ * * * * * ~~~php /** * 从缓存中删除存储的变量 * @param string $key 从缓存中删除存储的变量 * @return bool 成功返回true,失败返回false */ public function del($key); ~~~ * * * * * ~~~php /** * 清除所有缓存 * @return bool 成功返回true,失败返回false */ public function flush(); ~~~