在 Debian 上给 Laravel 配置缓存,通常分 系统层面(安装缓存服务) 和 Laravel 层面(配置驱动) 两部分。下面按常见场景说明。
Laravel 支持:
file(默认,无需安装)redis(推荐)memcacheddatabasearray(仅请求生命周期)生产环境常用:Redis
sudo apt update
sudo apt install redis-server -y
启动并设置开机自启:
sudo systemctl enable redis-server
sudo systemctl start redis-server
测试:
redis-cli ping
# 返回 PONG 表示正常
安装 PHP Redis 扩展:
sudo apt install php-redis -y
sudo systemctl restart php-fpm # 或 apache2
sudo apt install memcached libmemcached-tools -y
sudo apt install php-memcached -y
sudo systemctl enable memcached
sudo systemctl start memcached
.envCACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
如果是 Memcached:
CACHE_DRIVER=memcached
MEMCACHED_HOST=127.0.0.1
php artisan config:clear
php artisan config:cache
use Illuminate\Support\Facades\Cache;
// 写入
Cache::put('key', 'value', 600);
// 读取
$value = Cache::get('key');
// 永久
Cache::forever('key', 'value');
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache
arrayredis maxmemorysupervisor 管理队列(如用缓存做队列)如果你告诉我:
我可以给你更精确的配置示例。