用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
[TOC] # 缓存 日志类库可以存放一些信息 ## **配置文件** ``` project 应用部署目录 ├─conf 配置文件目录 │ └─config.php 配置信息 ``` ## **配置信息** ``` // +---------------------------------------------------------------------- // | 缓存配置 // +---------------------------------------------------------------------- // 'cache' => [ 'Default' => [ 'type' => 'File', 'path' => DATA_CACHE_PATH, 'ttl' => 3600, ] ], ``` ### 多缓存配置 > 以数组信息进行多个缓存配置 > 缓存支持 `File` `Redis` `MongoDB` ``` // +---------------------------------------------------------------------- // | 缓存配置 // +---------------------------------------------------------------------- // 'cache' => [ 'Default' => [ 'type' => 'File', 'path' => DATA_CACHE_PATH, 'ttl' => 3600, ], 'Mongodb' => [ 'type' => 'Mongodb', 'host' => '127.0.0.1', 'port' => 27017, 'username' => '', 'password' => '', ], 'Redis' => [ 'type' => 'Redis', 'host' => '127.0.0.1', 'port' => 27017, 'password' => '', ], ], ``` ## **缓存方法类型** 缓存方法遵循PSR-16定义 | 方法 | 作用 | | --- | --- | | get($key, $default = null) | 获取单个缓存信息 | | set($key, $value, $ttl = null) | 设置缓存和缓存失效时间 | | delete($key) | 删除对应缓存信息 | | getMultiple($keys, $default = null) | 获取多个keys值的缓存信息 返回数组模式 | | setMultiple($values, $ttl = 0) | 设置多个缓存值 和过期时间 | | deleteMultiple($keys) | 删除多个缓存key | | clear() | 清空所有缓存信息 | | has($key) | 判断当前缓存是否存在 | ## **使用缓存** ### **使用默认缓存配置信息** ``` use denha\Cache; Cache::set($key,$value,0); Cache::get($key); Cache::delete($key); ``` > Cache方法默认调用config.php配置中cache配置的第一个配置信息 ### **调用指定配置信息缓存** ``` use denha\Cache; $cache = Cache::channel('Mongodb'); // 调用名称为Mongodb的配置信息 $cache->set($key,$value); // 使用Mongodb配置进行缓存 ``` ### **直接设置配置信息缓存** ``` use denha\Cache; $config = [ 'type' => 'Mongodb', 'host' => '127.0.0.1', 'port' => 27017, 'username' => 'root', 'password' => '123' ]; $cache = Cache::create($config); // 调用配置信息 $cache->set($key,$value); // 使用Mongodb配置进行缓存 ```