How To Use Opcache In Yii

Hi All,

I have read the OPCache is use to save pre-compiled script in php application.

But In Yii what is the process to use opcache. What configuration options are available, for the same?

How I come to know the effect of Opcache in my application.

I am using php 5.5.13 and Yii 1.1.14.

Further I am using yii 2.0 also.

But now my problem is with version 1.1.14

Any help will be appreciated.


There’s nothing to configure in Yii itself. Simply enable opcache and you’re good to go.

Hi Sourcerer,

Thanks for your reply.

I have enabled opcache, which I checked from opcache_get_status().

I get [opcache_enabled] => 1.

Anything else I need to do.

In theory, no. In practice, you’re best advised to use a control panel (such as this one) and see if you allocated enough space for the pcache to operate. I also wrote down some configuration settings here.

I have already go through this link.

and change opcache settings as per this configuration.

But still no success. :(

Which means … ?

It means.

public function actionTest()




and in opcache.php I just write

echo "hello";

Now I change text "hello" to "hello brother".

If opcache is working in the application, then on refresh the page the text should be "hello" not "hello brother", But I am getting "hello brother"

This is the issue, what I am facing.

I have enabled opcache and made all setting which you have told.

But not getting the expected result.

Sounds like the cache content is being purged before you can reload. The key value here is [font=“Courier New”]opcache.revalidate_freq[/font]. Check if it’s overwritten somewhere in your config. And make sure [font=“Courier New”]opcache.validate_timestamps[/font] is set to 0, as the cache will be refreshed if the timestamp of a cached file changes.


validate_timestamps is set to 0, is life saver. Thanks a lot, Finally it works. :D

Hi Sourcerer,

This is working fine if I work with setting in opcache.ini.

But these settings are for all application on a server.

Is there any way through which I can set opcache enable or disable for different applications.

I was trying changing in .htaccess file and try disable opcache there using

php_flag opcache.enable Off

But it doesn’t work.

Any idea or any other changes I need to do.

I am able to do the same.

As I was previously changing in localhost/application/protected/.htaccess

At this location it was not working.

But then I create a new .htaccess at localhost/application/.htaccess

And put the above mentioned code in that file and it works.


Is it possible in an application, that only few actions will work with opcache.

I don’t want to save all data save in cache. I want to save only few data as per requirement.

Any suggestions?

That is no good idea and I have serious doubts that’ll be possible at all. Those actions will likely pull in framework code into the opcache. You can try to explicitly blacklist a set of files.

As for your original problem: perhaps your Apache config doesn’t allow a [font=“Courier New”].htaccess[/font] were you first placed it. Check which [font=“Courier New”]AllowOverride[/font] directives match that directory.

Thanks for the reply.

Yeah you are right I had to change, in httpd.conf AllowOverride directive to All.

And regarding the blacklist option, I also go through this file details. But this doesn’t solve my problem.

Because I can write the path of scripts which are outside of my application.

So this is something which will not work in my case.

Any idea how can I achieve this.

It would be interesting to know why you would want this in the first place.

Actually some times. There is an action which simply render a page. And in that page we have static HTML data.

Now if we using opcache, so this data also be cached and if I want to change this static data, to see the change effect I need to reset the cache.

Which I dont want to do. I dont want to cache this static data in my application.

I want to cache only dynamic data, which may sometimes cause error in my application.

So … you’ve got static data that isn’t that static? Is this in production or on your local development system?

This is for my local development system.

And how can I clear cached data? Currently I am doing this with server restart.

I tried function opcache_reset(). But it does not work.

opcache_reset() is working, I wrote it in a different function and then call that function from url.

So it helps me to clear the opcache.

But it clear all cache in the system. How can I clear cache for only specific data, not all data in cache.

You can invalidate specific files via [font=“Courier New”]opcache_invalidate()[/font]. But to be honest: You really shouldn’t enable opcache on your dev system.