Is there any way to invalidate cached version of a whole-cached page?
#1
Posted 31 December 2010 - 07:07 AM
with kind regards,
Augustin
#2
Posted 01 January 2011 - 08:31 PM
<?php /** * Flush cache Command * * Deletes all cached data. * * Usage: * yiic flushcache * * @author Alexander Makarov */ class FlushCacheCommand extends CConsoleCommand { /** * Executes the command. * @param array command line parameters for this command. */ public function run($args) { $cache=Yii::app()->getComponent('cache'); if($cache!==null){ $cache->flush(); echo "Done.\n"; } else { echo "Can't flush cache.\n"; } } }
Enjoying Yii? Star us at github
Support me so I can work more on Yii: https://www.patreon.com/samdark
#3
Posted 02 January 2011 - 09:22 AM
regards,
Augustin
#4
Posted 02 January 2011 - 09:33 AM
$myCachedPage = Yii::app()->getComponent($pageCacheIDorFragment);
$myCachedPage->flush(); // there you go:
resources:
flush: http://www.yiiframew...he#flush-detail
getCache (check source): http://www.yiiframew...getCache-detail
#5
Posted 02 January 2011 - 04:06 PM
#6
Posted 03 January 2011 - 03:44 AM
#7
Posted 03 January 2011 - 04:18 AM
"Hey cache, please delete page xyz..."
The cache will ask you each time, a page is found in the cache:
"Hey, you out there: I have a cached version for the current request here. Is it still valid?"
So, what you want to do instead, is to attach a cache dependency, that will answer this question with no, if a page is expired. Then the cache component will throw it out of the cache automatically.
#8
Posted 03 January 2011 - 04:42 AM
#9
Posted 03 January 2011 - 05:09 AM

CExpressionDependency should be the most flexible one.
Let's say, if you edit a page, that could have been cached, you want this page to get removed from the cache. To do so you first need to think about how you compose a custom unique page key, that identifies a page. This could be "actionId + itemId" or something more complicated. It completely depends on your application. You should be able to compose this key from the request data of any page that should be cached.
Now the basic principle could be like this:
- When you edit the page, you use use your custom page key and save a "expired" flag under that key somewhere (maybe also in the cache)
- In the dependency you use the same key and check if the expired flag is set. If it is set, you remove that flag and return false.
I have to admit, that i've never used a cache dependency, but that's the way i would try it.
#10
Posted 03 January 2011 - 06:23 AM
Antonio Ramirez, on 03 January 2011 - 06:14 AM, said:
What do you mean with Cache Objects / Fragments here? There's usually only 1 cache component in the application. If you call flush() on that component, it will erase all content from the cache.
#11
Posted 03 January 2011 - 06:32 AM
"the ID of the cache application component. Defaults to 'cache' (the primary cache application component.)"
Nevermind, I am a moron
I should read very carefully what I wrongly suggest. +1 to you Mike. Apologies to all.
#12
Posted 03 January 2011 - 06:35 AM
public function getCacheKey() { // Retrieve any request data, that you need to identify the current page here return self::createCacheKey($param1,$param2,$param3,...); } public static function createCacheKey($param1,$param2,$param3,...) { // put some logic to compose a unique key from the params here }
Now, when you want to remove a specific page from the cache, you should know the params, that identify this page (again: depends heaviliy on the different request params of your pages). Then you can remove this page from the cache like this:
$key=YourOutputCacheWidget::createCacheKey($param1,$param2,$param3,...); Yii::app()->cache->delete($key);
#14
Posted 28 March 2011 - 03:02 AM
$filters[]= array( 'COutputCache', 'requestTypes' =>array('GET'), 'duration' =>isset($_GET['nocache'] ? 0 : 3600, ),
This does not work, because the cached value is not removed from cache if duration=0.
@Alexander: Maybe this could be fixed? Would give a nice way to manually remove pages from cache.
Here's a workaround (but it comes with a drawback):
$filters[]= array( 'COutputCache', 'requestTypes' =>array('GET'), 'duration' =>3600, 'dependency'=>array( 'class'=>'CExpressionDependency', 'expression'=>'isset($_GET["nocache"])', ), ),
Now if you add "?nocache" to a cached page, the cached page will be removed. So far so good - but now a new version was cached with "?nocache". So if you reload the page with "?nocache" it will show the recently cached page. To flush this page again, you'll have to load the page again without "?nocache".
Apart from that it seems to work nicely.
#15
Posted 28 March 2011 - 03:35 AM
I think it worth looking at. Can you add an issue for it to the tracker?
Enjoying Yii? Star us at github
Support me so I can work more on Yii: https://www.patreon.com/samdark
#18
Posted 29 November 2011 - 11:46 PM