PHP OPCache를 사용하는 방법?
PHP 5.5가 출시되었으며 OPCache라는 새로운 코드 캐싱 모듈을 제공하지만 이에 대한 문서는 없습니다.
그렇다면 문서는 어디에 있으며 OPcache를 어떻게 사용합니까?
설치
OpCache는 기본적으로 PHP5.5 +에서 컴파일됩니다. 그러나 기본적으로 비활성화되어 있습니다. PHP5.5 +에서 OpCache를 사용하려면 먼저 활성화해야합니다. 이렇게하려면 다음을 수행해야합니다.
에 다음 줄을 추가하십시오 php.ini
.
zend_extension=/full/path/to/opcache.so (nix)
zend_extension=C:\path\to\php_opcache.dll (win)
경로에 공백이 있으면 따옴표로 묶어야합니다.
zend_extension="C:\Program Files\PHP5.5\ext\php_opcache.dll"
또한 실제 Zend 엔진 (예 : PHP를 실행하는 엔진)에 영향을 미치기 때문에 zend_extension
"normal"지시문 대신 지시문 을 사용해야합니다 extension
.
용법
현재 사용할 수있는 네 가지 기능이 있습니다.
opcache_get_configuration()
:
OpCache가 현재 사용하는 구성을 포함하는 배열을 반환합니다. 여기에는 모든 ini 설정과 버전 정보 및 블랙리스트 파일이 포함됩니다.
var_dump(opcache_get_configuration());
opcache_get_status()
:
캐시의 현재 상태에 대한 정보가있는 배열을 반환합니다. 이 정보에는 캐시 상태 (활성화, 재시작, 전체 등), 메모리 사용량, 적중, 누락 및 기타 유용한 정보가 포함됩니다. 캐시 된 스크립트도 포함됩니다.
var_dump(opcache_get_status());
opcache_reset()
:
전체 캐시를 재설정합니다. 다음에 방문 할 때 가능한 모든 캐시 된 스크립트가 다시 구문 분석됩니다.
opcache_reset();
opcache_invalidate()
:
캐시 된 특정 스크립트를 무효화합니다. 다음에 방문 할 때 스크립트가 다시 파싱됨을 의미합니다.
opcache_invalidate('/path/to/script/to/invalidate.php', true);
유지 보수 및 보고서
OpCache를 유지 관리하고 유용한 보고서를 생성하는 데 도움이되는 GUI가 있습니다. 이러한 도구는 위의 기능을 활용합니다.
OpCacheGUI
면책 조항이 프로젝트의 저자입니다
풍모:
- OpCache 상태
- OpCache 구성
- OpCache 통계
- OpCache 재설정
- 캐시 된 스크립트 개요
- 캐시 된 스크립트 무효화
- 다국어
- 휴대 기기 지원
- 반짝이는 그래프
스크린 샷 :
URL : https://github.com/PeeHaa/OpCacheGUI
opcache- 상태
풍모:
- OpCache 상태
- OpCache 구성
- OpCache 통계
- 캐시 된 스크립트 개요
- 단일 파일
스크린 샷 :
URL : https://github.com/rlerdorf/opcache-status
opcache-gui
풍모:
- OpCache 상태
- OpCache 구성
- OpCache 통계
- OpCache 재설정
- 캐시 된 스크립트 개요
- 캐시 된 스크립트 무효화
- 자동 새로 고침
스크린 샷 :
URL : https://github.com/amnuts/opcache-gui
OPcache가 APC를 대체합니다
OPcache는 APC 모듈을 대체하도록 설계되었으므로 PHP에서 병렬로 실행할 수 없습니다. 이것은 코드 작성 방법에 영향을 미치지 않기 때문에 PHP opcode 캐싱에 좋습니다.
그러나 현재 APC를 사용하여 apc_store()
기능을 통해 다른 데이터를 저장 하는 경우 OPCache를 사용하기로 결정한 경우 에는 해당 기능을 수행 할 수 없습니다.
공유 PHP 메모리에 데이터를 저장하는 APCu 또는 Yac 와 같은 다른 라이브러리를 사용 하거나 메모리에 별도의 프로세스로 데이터를 PHP에 저장하는 memcached와 같은 것을 사용하도록 전환해야합니다.
또한 OPcache에는 APC에 존재하는 업로드 진행률 표시기가 없습니다. 대신 세션 업로드 진행을 사용해야합니다 .
OPcache 설정
OPcache에 대한 문서를 찾을 수 있습니다 여기에 나열된 모든 설정 옵션과 함께 여기 . 권장되는 설정은 다음과 같습니다.
; Sets how much memory to use
opcache.memory_consumption=128
;Sets how much memory should be used by OPcache for storing internal strings
;(e.g. classnames and the files they are contained in)
opcache.interned_strings_buffer=8
; The maximum number of files OPcache will cache
opcache.max_accelerated_files=4000
;How often (in seconds) to check file timestamps for changes to the shared
;memory storage allocation.
opcache.revalidate_freq=60
;If enabled, a fast shutdown sequence is used for the accelerated code
;The fast shutdown sequence doesn't free each allocated block, but lets
;the Zend Engine Memory Manager do the work.
opcache.fast_shutdown=1
;Enables the OPcache for the CLI version of PHP.
opcache.enable_cli=1
코드 주석을 사용하는 라이브러리 또는 코드를 사용하는 경우 주석 저장을 활성화해야합니다.
opcache.save_comments=1
If disabled, all PHPDoc comments are dropped from the code to reduce the size of the optimized code. Disabling "Doc Comments" may break some existing applications and frameworks (e.g. Doctrine, ZF2, PHPUnit)
I am going to drop in my two cents for what I use opcache.
I have made an extensive framework with a lot of fields and validation methods and enums to be able to talk to my database.
Without opcache
When using this script without opcache and I push 9000 requests in 2.8 seconds to the apache server it maxes out at 90-100% cpu for 70-80 seconds until it catches up with all the requests.
Total time taken: 76085 milliseconds(76 seconds)
With opcache enabled
With opcache enabled it runs at 25-30% cpu time for about 25 seconds and never passes 25% cpu use.
Total time taken: 26490 milliseconds(26 seconds)
I have made an opcache blacklist file to disable the caching of everything except the framework which is all static and doesnt need changing of functionality. I choose explicitly for just the framework files so that I could develop without worrying about reloading/validating the cache files. Having everything cached saves a second on the total of the requests 25546 milliseconds
This significantly expands the amount of data/requests I can handle per second without the server even breaking a sweat.
With PHP 5.6 on Amazon Linux (should be the same on RedHat or CentOS):
yum install php56-opcache
and then restart apache.
I encountered this when setting up moodle. I added the following lines in the php.ini file.
zend_extension=C:\xampp\php\ext\php_opcache.dll
[opcache]
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 4000
opcache.revalidate_freq = 60
; Required for Moodle
opcache.use_cwd = 1
opcache.validate_timestamps = 1
opcache.save_comments = 1
opcache.enable_file_override = 0
; If something does not work in Moodle
;opcache.revalidate_path = 1 ; May fix problems with include paths
;opcache.mmap_base = 0x20000000 ; (Windows only) fix OPcache crashes with event id 487
; Experimental for Moodle 2.6 and later
;opcache.fast_shutdown = 1
;opcache.enable_cli = 1 ; Speeds up CLI cron
;opcache.load_comments = 0 ; May lower memory use, might not be compatible with add-ons and other apps
extension=C:\xampp\php\ext\php_intl.dll
[intl]
intl.default_locale = en_utf8
intl.error_level = E_WARNING
intl -> http://php.net/manual/en/book.intl.php
참고URL : https://stackoverflow.com/questions/17224798/how-to-use-php-opcache
'Programing' 카테고리의 다른 글
도메인 기반 디자인 : 도메인 서비스, 응용 프로그램 서비스 (0) | 2020.04.10 |
---|---|
명령 행에서 PostgreSQL 쿼리 실행 (0) | 2020.04.10 |
자바 스크립트 : 파일 생성 및 저장 (0) | 2020.04.10 |
실행중인 MySQL 쿼리를 중지하려면 어떻게해야합니까? (0) | 2020.04.10 |
왜 일부 플랫폼에서는 그렇지 않고 다른 플랫폼에서는 루프 종료에 사용합니까? (0) | 2020.04.10 |