Is it possible to use apcu (as a php 5.5 module) in combination with suPHP?
The answer to What is best PHP Handler for APC says no:
suPHP also cannot work with an opcode caching extension such as eAccelerator or APC
but this post is over a year old and maybe not referring to apcu.
I can successfully enable the apcu module (in cPanel) and transfer content via apc_store and apc_fetch - but not between requests. It forgets what I've been storing when I reload the script/page.
So my first concern is to rule out if I'm trying something that's impossible anyway?
PS: for the record, What is userland caching APCu extension in PHP? gives some helpful background
Not possible.
suPHP runs each PHP request in a new process, so caching extensions that try to save data in memory across requests don't work. (Or, rather, they work, but any stored data is only accessible to that job, and is lost at the end of the request.)
Use something like memcached if you need data caching.
Related
I am running PHP Version 5.3.5 and developing a website in Codeigniter 2.1.3
I am working now to improve website performance, and I found caching can be used like APC or memcached or Zend OpCache.
In codeigniter I used $this->output->cache(43829); for caching.
Output of actions got cached, and It returns same output on every request.
But I have dynamic pages so this caching will not work. I am looking around how APC can be used for ope-code caching and used to enhance web site performance.
Is APC stores opecode in cache that cane be used by zend engine for execution execution, without regenerating opecode on each request?
Attached is my PHP APC configuration
PHP 5.3 is nearly 2 years past end of life, and APC is not available for current PHP versions. PHP 5.5+ comes with Zend Opcache, which has equivalent functionality. Instead of trying to get your site to work with old technologies, simply upgrading your server to a current (and secure) version of PHP would give you this performance boost without you having to do anything else.
Output of actions got cached, and It returns same output on every request
While its faster than regenerating the page, its still far from optimal. Better to use a caching reverse proxy in such a case.
I am looking around how APC can be used for op[e]-code caching
Your question implies that you think you are not already using op-code caching. I suggest your first port of call is to see if it is already enabled. You can see this from the output of phpinfo() or check function_exists('apc_sma_info');
APC is unlikely to be provisioned for future versions of PHP. ZOP+ is the opcode cache which is currently bundled with PHP 5.4+. It will work with 5.3, but requires some effort. Tim is correct in saying the 5.3 is past its sell-by-date and should be upgraded.
Note that ZOP+ also includes an optimizer (and, rather alarmingly, it seems to have a significant impact on the performance of many frameworks). The performance of ZOP+ is more dependent on sizing the cache than in APC - but even with APC, sizing the cache wrongly will not give good results.
i'm new in PHP and want to try caching(for the first time), so i make website and it has :
dynamic home page
dynamic portfolio page
dynamic contact page
static about page
static admin page
so i read the tutorial about caching and i try to make my own caching system:
using file cache based on the what page is requested, when the page is requested the cache system will check if there's cache in cache directory if there's no cache file yet then write all the output(html) from the php script(in this case output from output buffer) and if there's cache file that corresponds with the specific id(based on URI) then just include_once() the html file.
Then i read in CodeIgniter(i make this website using CI) says there's APC for caching, then i read again about APC, what i read about APC is that it caches the DB results, but now i'm confused which should i use
what i get so far:
file caching probably would slower if there's alot of request (i dont know if this is true or not but i read it somewhere from search engine)
APC is fast
but i'm still confused which i should use , i'm on shared hosting
The levels of caching most relevant in a PHP application:
File / Script caching - The operating system will actually do this to a large extent. When a file is opened it's added to an OS-level cache. It stays there until the file is touched or the OS needs to free memory for other processes. A homegrown PHP solution isn't a good replacement for this.
Opcode caching - In order to function, PHP needs to parse and compile a script into opcodes. A mechanism like APC will cache the opcodes of every PHP script executed by Apache, provided that the cache doesn't overflow. A homegrown PHP solution build on top of APC can partially do this, but APC already does it ... so don't bother.
Query caching - If your script accesses a lot of data that doesn't change very frequently, or wherein some latency between updates and the visibility of those updates is acceptable, caching the results from complex queries is beneficial. A homegrown PHP solution built on APC is acceptable and beneficial at this level. But a database level solution is also appropriate here, and often more appropriate.
Output caching - If your page is largely deterministic and/or the same sort of latency applicable to query caching is acceptable, you can cache the entire output of the script using output buffering and APC. A homegrown PHP solution built on APC is acceptable here, but generally not necessary. If the page is static, you're probably not saving yourself any re-computation. And if it's dynamic, it's usually preferable to just re-render the page anyway.
In a dedicated or virtual-dedicated environment you'd need install APC (or something similar) yourself. But, in a shared hosting environment, it's very likely that APC is installed. And if it weren't you couldn't install it yourself anyway.
And, due to my own uncertainty, I'd recommend not performing any query or output caching with APC in a shared environment -- I'm not sure whether APC segregates caches by virtual host. Even if it does, I wouldn't assume that my site is truly a separate virtual host.
So this is whats bothering me. I just installed APC cache and Im testing it.
When using APC Admin interface, in apc.php file, I can see all the info about APC etc.
When I go to System Cache Entries I can see that every script i invoke gets written there.
So does this means that APC Cache works out of the box? I can just install APC cache and it already speeds up my application by caching scripts? And if I want I can then cache variables to make it even faster?
Hope you get the question, its probably simple to someone with more experience with APC.
Its I know i can add some variables to cache, and then get them out and that will speed up my app. But is it true, that APC will speed up the app and cache scripts all by him self?
And is there any good documentation where I could learn more about APC?
Yeah, APC "just works". Anyone running PHP in production without APC/(other opcodecache) is missing out on the easiest performance improvement they can readily achieve.
A few caveats though.
If you are in development, you can still run APC, however, you probably want to enable stat calls. This means that APC will check the last modified of your files.
apc.stat = [1|0]
So if you don't have stat calls enabled, and you change a file and APC has already cached it, then it won't observe your changes, and you will continue using the cached opcode.
As you have mentioned, APC isn't just for opcode caching, it is also useful for user space caching. You have your system cache and your user cache.
You can store things against your user cache by just performing something like:
apc_store("fooKey", "barValue");
A very noob question here.
I just installed APC and when I go to the monitoring page (apc.php) and click on the "System Cache Entries" tab I can see a lot of pages on that list after browsing my app that's hosted on the server. To test I restarted apache and all the cache entries were gone but as soon as I started browsing more pages on my app again they started appearing on that list.
I didn't make any changes to my code, so is this all I have to do to enable optcode caching? Or are changes to my code also necessary?
I ask because my app is using codeigniter and there is a page in the codeigniter docs on caching docs that describes code changes:
http://codeigniter.com/user_guide/libraries/caching.html
APC stores opcode caches as they are parsed. As you have already discovered the caches only persist as long as apache remains open. But when an opcode cache is missing for a page that is requested, APC will store it for as long as Apache remains running. However, opcode caches are only half the battle. While it is true you will receive a speed increase from caching opcode, a lot of time in PHP is lost to file input/output and socket communications (ie. database queries). As long as you can be sure that your script is the only resource which will be modifying the database or a file, you can safely caching database query results or file contents so that each request doesn't need to touch the filesystem or database layer. The logic for this uses some APC functions:
if(apc_exists('some_database_value')) {
$value = apc_fetch('some_database_value');
} else {
//Query db for value, store in $value
apc_store('some_database_value', $value);
}
The only disadvantage to this solution is if you need to modify any cached resource outside of the PHP script, you will need to clear the APC cache from the CLI.
No, APC requires no code changes to accellerate the actual running of the code; for a bit more information, see for example this answer
With APC, you first get an opcode cache -- for that part, you having
nothing to modify in your code : just install the extension, and
enable it.
The opcode cache will generally speed up things : it prevents the PHP
scripts from being compiled again and again, by keeping the opcodes --
the result of the compilation of the PHP files -- in memory.
i need to caching xml and json response requested to a remote host. Is There a simple Cache System developed in PHP?
If you are looking to get up and running right away, you can use APC, which is installed (or can be easily installed) with PHP, depending on what platform you are running PHP on. Then you can use the apc_add() function call to add keys into the cache.
Example:
<?php
$bar = 'BAR';
apc_add('foo', $bar);
?>
You can then use apc_exists() and apc_fetch() to check and fetch items out of the cache.
Obviously if your requirements are more complex, you may want to take a look at Memcached
PHP itself can't cache because it is interpreted at request time - there isn't any in-memory caching capabilities naively available. If you have control over the web server, you may be able to install APC cache. see this for a little info on how to use API. If not, you can check if that extension is installed (in my experience, not may hosts have this installed), ask for it to be installed, or simply grow your own (see below).
APC cache is a simple file-based caching mechanism that provides some neat facilities to expunge caches that grow old. You can build this yourself using the standard PHP library if APC isn't available (or you're doing something really simple). If you have to roll your own, use the above like as an implementation guide - build in the features you need and leave out what you don't. =)