Tuesday, October 18, 2011

How to flush Cache using Curl Command in Adobe CQ / AEM

Use Case Flush cache for a path in regular interval of time

Solution You can use following command to flush cache


curl -H "CQ-Action:Flush" -H "CQ-Handle: /content/geometrixx/en/toolbar" -H "CQ-Path:/content/geometrixx/en/toolbar" -H "Content-Length: 0" -H "Content-Type: application/octet-stream" http://dispatcher-server-hostname:port/dispatcher/invalidate.cache

In Newer version of dispatcher try this

curl -H "CQ-Action: DELETE" -H "CQ-Handle:/content/geometrixx/en/toolbar" -H "CQ-Path:/content/geometrixx/en/toolbar" -H "Content-Length: 0" -H "Content-Type: application/octet-stream" http://dispatcher-server-hostname:port/dispatcher/invalidate.cache

Expected Response: <H1>OK</H1>

If you see error like
[Thu May 30 09:23:53 2013] [D] [91650(140735211479424)] Found farm <Your FARM>
[Thu May 30 09:23:53 2013] [D] [91650(140735211479424)] checking [/dispatcher/invalidate.cache]
[Thu May 30 09:23:53 2013] [W] [91650(140735211479424)] Flushing rejected from <Something>
That mean you are not in allowed list of client to flush cache. In that case please allow your IP (<SOMETHING>) in above case in allowedClient section of dispatcher.any

 /allowedClients
        {
         /0000
          {
          /glob "*"
          /type "deny"
          }
        /0001
          {
          /glob "<Something>"
          /type "allow"
          }
... More allowed
        }
      }

Important Note: Make sure that from outside (DMZ may be) no one should be able to run this command. This command should be allowed to run only from some trusted boxes

Thanks Andrew Khoury from Adobe for this information

16 comments:

  1. Whats the difference between the CQ-Handle and CQ-Path?

    ReplyDelete
  2. With the header "CQ-Handle" (CQ-Handle: /content/geometrixx/en), the
    dispatcher will delete the following files and directories in its cache and touch stat file:

    ./content/geometrixx/en.*
    ./content/geometrixx/en/_jcr_content/

    CQ-Path is optional it looks like

    ReplyDelete
  3. Is there anyway that in this curl script we can specify the folder where we want the cache to be flushed? Sometimes we need to make some script, code, css/js files take effect, and would like these folder to be set up in the script.

    ReplyDelete
    Replies
    1. Yes. Please see example above. By supplying CQ-Handle and CQ-Path you can specify folder path that want to flush.

      Delete
  4. dispatcherServerName=localhost
    dispatcherServerPort=80
    dispatcherHandle=/etc/designs/offroad

    curl -u admin:admin -X POST -H "CQ-Action: Activate" -H "CQ-Handle: $dispatcherHandle" -H "CQ-Path: $dispatcherHandle" -H "Content-Length: 0" -H "Content-Type: application/octet-stream" http://$dispatcherServerName:$dispatcherServerPort/dispatcher/invalidate.cache
    -------------------
    I intend to run the above script via cron on the local Apache webserver with dispatcher module.
    When you run this, would it pull updated file from publisher or delete the files under $dispatcherHandle? I noticed this is not happening, even the timestamp of the file. But when I tried to rename a file inside it and hit the page that triggers in caching that file, it pulls the updated one from the publisher.


    ReplyDelete
    Replies
    1. Thanks for your feedback. I just updated blog with new command and some things that you should look if command is not working.

      Delete
  5. How do you setup trusted box in order to run this command externally?

    ReplyDelete
    Replies
    1. Hello Saurav,

      You can use allowedClient property in dispatcher and then allow that IP from where you are running this command. Only IP listed in allowed Client can do dispatcher flush.

      Yogesh

      Delete
  6. I am trying to flush multiple paths. Is it not possible to pass multiple paths in CQ-Handle/Path in cURL. Please let me know.

    ReplyDelete
    Replies
    1. In Syntax you can not pass multiple path. However you can write a bash script to have multiple path.

      Delete
    2. I already have a script but i was looking if there was a way to flush multiple paths at a shot. Anyway thnx...

      Delete
  7. Can we unbind/save a configuration using curl?

    ReplyDelete
    Replies
    1. Do you mean change dispatcher configuration ? If yes then yes you can do it using sling post servlet http://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html all replication agents are under /etc/replication/

      Yogesh

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Nope. This is completely unrelated. I would like to unbind or save a configuration in configmgr (/system/console/configmgr) using curl.

      Venkatesh.

      Delete
    4. Hello Venkat,

      If this is custom config then I would suggest to use osgi:config to do that and use curl to update that config. If this is OOTB, you can use curl like

      curl -u admin:admin 'http://HOST:PORT/system/console/configMgr/PID' --data 'unbind=1'

      Yogesh

      Delete