简体   繁体   English

Laravel 部署到 heroku 时缓存文件出现问题

[英]Laravel problem with cache file when deploy to heroku

My application is using laravel 8. I deploy it from github to heroku and I want to run the command below:我的应用程序正在使用 laravel 8。我将它从 github 部署到 heroku,我想运行以下命令:

php artisan optimize

But I don't know how to run the command in heroku and if the command creates cache files in my app then heroku will delete them after specific time because they are not permanently files deployed from github. Anyone know how to deal with this pls tell me what to do.但我不知道如何在 heroku 中运行命令,如果该命令在我的应用程序中创建缓存文件,那么 heroku 将在特定时间后删除它们,因为它们不是从 github 部署的永久文件。任何人都知道如何处理这个请告诉我该怎么办。 Thank you!谢谢!

P/s: i tried to do like the link below but it didn't create any cache file in my app. P/s:我试着喜欢下面的链接,但它没有在我的应用程序中创建任何缓存文件。 https://dev.to/autoidle/laravel-deployment-optimization-on-heroku-1hkc https://dev.to/autoidle/laravel-deployment-optimization-on-heroku-1hkc

[new update] Problem solved. [新更新] 问题已解决。 Just follow the link above and my answer below.只需点击上面的链接和下面我的回答。

[new update] You can follow the link below and do exactly the same but you need to fix the part of Procfile from composer warmup to composer run-script warmup . [新更新] 您可以点击下面的链接并执行完全相同的操作,但您需要修复 Procfile 从composer warmupcomposer run-script warmup的部分。 That will solve the problem in this thread.这将解决此线程中的问题。 https://dev.to/autoidle/laravel-deployment-optimization-on-heroku-1hkc https://dev.to/autoidle/laravel-deployment-optimization-on-heroku-1hkc

composer.json:作曲家.json:

    "scripts": {
       ...
       "warmup": [
           "@php artisan config:cache",
           "@php artisan route:cache",
           "@php artisan view:cache"
    ]
},

Procfile:简介:

web: composer run-script warmup && heroku-php-apache2

enter image description here在此处输入图像描述

Heroku uses an ephemeral filesystem , that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Heroku 使用临时文件系统,这意味着在测功机运行时对文件系统的任何更改只会持续到测功机关闭或重新启动。 Each dyno boots with a clean copy of the filesystem from the most recent deploy.每个 dyno 都使用最近部署的文件系统的干净副本启动。 This is similar to how many container-based systems, such as Docker, operate.这类似于多少个基于容器的系统,例如 Docker,在运行。

The solution to your problem is to use Redis as cache (and also for session, queue, ...)您的问题的解决方案是使用 Redis 作为缓存(以及 session,队列,...)

  1. Add to your Heroku environments: CACHE_DRIVER=redis添加到你的 Heroku 环境: CACHE_DRIVER=redis

  2. composer require ext-json

  3. Install Redis as add-on for example: https://elements.heroku.com/addons/heroku-redis安装 Redis 作为附加组件,例如: https://elements.heroku.com/addons/heroku-redis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM