CodeIgniter4提供了灵活且易用的缓存组件,支持多种缓存驱动,开发者可以根据项目需求选择合适的缓存方案,下面介绍具体的配置和使用方法。

CodeIgniter4缓存基础配置
首先需要修改框架的缓存配置文件,开启对应驱动的支持。缓存配置文件位于app/Config/Cache.php,主要需要关注以下几个配置项:
<?php
namespace Config;
use CodeIgniterConfigBaseConfig;
class Cache extends BaseConfig
{
// 默认缓存驱动,可设置为 file、redis、memcached 等
public $defaultHandler = 'file';
// 缓存前缀,避免不同项目缓存键冲突
public $prefix = '';
// 缓存有效期,单位秒,0 表示永不过期
public $ttl = 60;
// 各驱动的具体配置
public $handlers = [
// 文件缓存配置
'file' => [
'storePath' => WRITEPATH . 'cache/',
],
// Redis缓存配置
'redis' => [
'host' => '127.0.0.1',
'password' => null,
'port' => 6379,
'timeout' => 0,
'database' => 0,
],
// Memcached缓存配置
'memcached' => [
'host' => '127.0.0.1',
'port' => 11211,
'weight' => 1,
],
];
}
文件缓存的使用方法
文件缓存是CodeIgniter4的默认缓存驱动,无需额外安装扩展即可使用,适合小型项目或者本地开发场景。
缓存写入
可以通过service('cache')获取缓存实例,调用save方法写入缓存,示例代码如下:
<?php
namespace AppControllers;
use CodeIgniterController;
class UserController extends Controller
{
public function getUserList()
{
// 获取缓存实例
$cache = ConfigServices::cache();
// 缓存键
$cacheKey = 'user_list';
// 尝试从缓存获取用户列表
$userList = $cache->get($cacheKey);
if (empty($userList)) {
// 缓存不存在,从数据库查询数据
$userModel = new AppModelsUserModel();
$userList = $userModel->findAll();
// 将查询结果存入缓存,有效期300秒
$cache->save($cacheKey, $userList, 300);
}
return view('user_list', ['userList' => $userList]);
}
}
缓存读取与删除
读取缓存使用get方法,删除指定缓存使用delete方法,清除所有缓存使用clean方法:
<?php
$cache = ConfigServices::cache();
// 读取缓存,不存在返回null
$value = $cache->get('test_key');
// 删除指定缓存
$cache->delete('test_key');
// 清除所有缓存
$cache->clean();
Redis缓存的配置与使用
Redis缓存性能更高,支持持久化,适合中大型项目使用,使用前需要确保服务器安装了Redis服务以及php的redis扩展。
Redis驱动配置
修改app/Config/Cache.php中的defaultHandler为redis,并根据实际Redis服务信息调整handlers中的redis配置:
// 修改默认驱动为redis
public $defaultHandler = 'redis';
// 调整redis配置,假设Redis服务地址为192.168.0.1,端口6379,密码为123456
public $handlers = [
'redis' => [
'host' => '192.168.0.1',
'password' => '123456',
'port' => 6379,
'timeout' => 0,
'database' => 0,
],
];
Redis缓存操作示例
Redis缓存的读写方法和文件缓存完全一致,框架已经做了驱动层封装,无需修改业务代码即可切换驱动:
<?php
namespace AppControllers;
use CodeIgniterController;
class ArticleController extends Controller
{
public function getArticleDetail($articleId)
{
$cache = ConfigServices::cache();
$cacheKey = 'article_' . $articleId;
// 获取缓存
$article = $cache->get($cacheKey);
if (empty($article)) {
$articleModel = new AppModelsArticleModel();
$article = $articleModel->find($articleId);
if (!empty($article)) {
// 存入缓存,有效期600秒
$cache->save($cacheKey, $article, 600);
}
}
return view('article_detail', ['article' => $article]);
}
// 更新文章后删除对应缓存
public function updateArticle($articleId)
{
$articleModel = new AppModelsArticleModel();
$articleModel->update($articleId, $this->request->getPost());
$cache = ConfigServices::cache();
$cache->delete('article_' . $articleId);
return redirect()->to('/article/' . $articleId);
}
}
缓存使用注意事项
- 缓存键建议添加统一前缀,避免不同模块或者不同项目的缓存键冲突
- 缓存有效期根据数据更新频率设置,更新频繁的数据设置较短的过期时间
- 数据更新后及时删除或者更新对应缓存,避免脏数据问题
- 生产环境建议优先使用Redis或者Memcached驱动,文件缓存性能相对较低
CodeIgniter4php缓存Redis缓存缓存配置修改时间:2026-06-10 09:45:33