Yii Framework Forum: Is there any way to invalidate cached version of a whole-cached page? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Is there any way to invalidate cached version of a whole-cached page? Rate Topic: -----

#1 User is offline   augustin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-April 10

Posted 31 December 2010 - 07:07 AM

Is there any way to invalidate cached version of a whole-cached page (I use COutputCache as a filter in controller)? I know that I can use subclasses of CCacheDependency, but this solution isn't perfect: one needs to call the method with all request and it can be complicated if conditions are more complicated.

with kind regards,
Augustin
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,778
  • Joined: 17-January 09
  • Location:Russia

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";
        }
    }
}

Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#3 User is offline   augustin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-April 10

Posted 02 January 2011 - 09:22 AM

I didn't mean "clear the whole cache" but "delete the single page from cache". Single page = what was cached with COutputCache filter. And I don't want to do this from console level, but from the class (api) level.

regards,
Augustin
0

#4 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 02 January 2011 - 09:33 AM

The first thing you need to know, is what ID value is the cache you want to flush (erase). Assuming you know it and you place that value in $pageCacheIDorFragment, hen, just use:


$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
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#5 User is offline   augustin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-April 10

Posted 02 January 2011 - 04:06 PM

how to compute this ID in an elegant way (i.e. using some function, not looking into implementation details and writing one's own...)?
0

#6 User is offline   augustin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-April 10

Posted 03 January 2011 - 03:44 AM

I don't wont to flush all the cache (cacheID is the ID of WHOLE cache), I want to delete SINGLE page in cache (keyCache instead of cacheID is what I want).
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 January 2011 - 04:18 AM

This question comes up from time to time. Yii's caching mechanism is a little different. Maybe it helps to first understand how the components interact with each other. Instead of talking to the cache like this:

"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.
0

#8 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 03 January 2011 - 04:42 AM

In that case, CExpressionDependency will be the best to suit the needs of a cache that we want to erase upon a custom expression is that right?
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 January 2011 - 05:09 AM

Well, it depends (pun intended). ;)

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.
0

#10 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 January 2011 - 06:23 AM

View PostAntonio Ramirez, on 03 January 2011 - 06:14 AM, said:

Otherwise, it would be silly that all Cache Objects and Fragments that implement the ICache interface have their own flush and flushValues. Dont you think?


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.
1

#11 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 03 January 2011 - 06:32 AM

http://www.yiiframew....1/COutputCache

"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.
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#12 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 03 January 2011 - 06:35 AM

Another, maybe easier way to solve this issue might be, to extend COutputCache and override getCacheKey() there. You could create some static method that generates a key from a set of given parameters. Again you need to come up with a logic how to create a unique cache id for any given page that should get cached.

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);

0

#13 User is offline   RusAlex 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 23-August 10

Posted 27 March 2011 - 04:28 PM

check this wiki page
how to invalidate specific page
0

#14 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 March 2011 - 03:02 AM

Interesting approach. This got me to another idea, that doesn't require to override COutputCache:



     		$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.
0

#15 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,778
  • Joined: 17-January 09
  • Location:Russia

Posted 28 March 2011 - 03:35 AM

Mike
I think it worth looking at. Can you add an issue for it to the tracker?
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#16 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 March 2011 - 04:22 AM

Done:
http://code.google.c.../detail?id=2258
0

#17 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 25 June 2011 - 04:05 AM

Cool, this was fixed in SVN and will be in 1.1.8!
0

#18 User is offline   DryKillLogic 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 14-December 10

Posted 29 November 2011 - 11:46 PM

is there any way to invalidate cache when it's an ajax request?. I have problems with the pagination. Thanks.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users