apc

最後更新: 2015-03-13

 

介紹

Alternative PHP Cache

http://hk.php.net/apc

 

 


Debain 安裝

apt-get install php-apc

 

File:

  • /usr/lib/php5/20090626+lfs/apc.so
  • /usr/share/doc/php-apc/apc.php.gz

 


設定

 

Default: /etc/php5/conf.d/apc.ini

extension=apc.so

修改後:

[apc]
extension = apc.so
apc.enabled=1
# mmap: only one memory segment
# SHM: support that uses multiple memory segments
apc.shm_segments=1

# The size of each shared memory segment in MB
# default: "32M"
apc.shm_size=512M

apc.optimization=1
# number of distinct source files that will be included or requested on your web server
apc.num_files_hint=1024

apc.user_entries_hint=4096       <-- distinct user cache variables


# The number of seconds a cache entry is allowed to idle in a slot
# in case this cache entry slot is needed by another entry.
# the cache will be completely expunged if ttl is equal to 0

apc.ttl=7200

apc.gc_ttl=3600                  <-- seconds remain on the garbage-collection list

apc.cache_by_default=1           <-- default: on


apc.max_file_size=1M             <-- default: 1M

# defaults: on
apc.stat=1                       <-- 是否在每個 request 都 check php 檔有沒有改變, off 要 restat 才 clean cache
apc.stat_ctime=0                 <-- normally only check mtime

# 用圖不大, 因為 cli 行完就無
apc.enable_cli=0


# mmap module for determining whether your mmap'ed memory region
# (file-backed(/tmp/apc.XXXXXX) or shared memory backed(/dev/zero))
apc.mmap_file_mask=/dev/zero

 


 

Window

 

http://downloads.php.net/pierre/

把裡面的 apc.dll 複製到 PHP 安裝資料夾的  ext 資料夾下

php.ini

[apc]
extension="php_apc.dll"
apc.cache_by_default = On
apc.enable_cli = Off
apc.enabled = On
apc.optimization = Off
apc.shm_segments = 1
apc.shm_size = 64M
apc.gc_ttl = 3600
apc.ttl = 7200
apc.user_ttl = 7200
apc.num_files_hint = 1024
apc.rfc1867 = 1
apc.max_file_size = 1M
apc.stat = On
apc.write_lock = On


Info:
<?php print_r(apc_sma_info()); ?>
apc.php <-- 現在情況 ( http://pecl.php.net/package/apc)


c:\apache2\bin>ab.exe -n 50 -c 1 -d http://mymoodle.com/moodle/index.php


If Cache Full Count > 0, this indicates that the cache is filling-up and churning as there is not enough memory allocated. To solve this problem increase the memory allocation (apc.shm_size).
The Fragmentation % indicator should be 0% but may increase from time-to-time especially if the cache is churning.

 


Php Function

apc_store

bool apc_store ( string $key , mixed $var [, int $ttl = 0 ] )
array apc_store ( array $values [, mixed $unused [, int $ttl = 0 ]] )

# persist between requests (until the value is removed from the cache). (default:  ttl is 0)

<?php
$bar = 'BAR';
apc_store('foo', $bar);
var_dump(apc_fetch('foo'));
?>

 


在某 Folder disable APC

.htaccess

Disable APC:

php_flag apc.cache_by_default Off