You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Leszek Gawron <lg...@mobilebox.pl> on 2004/06/01 01:04:52 UTC

JXTG and caching

While almost everybody buried XSP it has got one great feature that JXTG 
  lacks: it's cacheable. I know that there is no way to implement 
automatic JXTG caching but what if I know on what env does jx template 
operate. If I know that it is for example only request param "abc" and 
"bcd" and session attribute "xxx" then I know when I can use cached 
entry. The question is: how?

Should I extend JXTemplateGenerator so it is a cached component? I 
understand it gets the validity methods to implement then. So every time 
I call my extended component from flow I pass also the validity 
calculated in flow. Something like this:

function niceCachedResponse() {
   var cacheKey = cocoon.request.get( "abc" ) + "!!spacer!!"
                     + cocoon.request.get( "bcd" ) + "!!spacer!!"
                     + cocoon.session.getAttribute( "xxx" );
   var pageValidity = new DeltaTimeCacheValidity( 60 * 60 * 24 );
                                                 // one day
   cocoon.sendPage( "view/cached.jx", {
                                      JXTGExtCacheKey: cacheKey,
                                      JXTGExtpageValidity: pageValidity
                                      someOtherBizData: data } );
}

Does it have a chance to work ?
	Leszek Gawron

Re: JXTG and caching - IT'S DONE

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Upayavira wrote:
> Fab.
> 
> The below all reads okay to me. It would seem reasonable that the 
> template must be read in setup now. [As an aside, is the jxt file 
> cached? Or does it need to be read with every request? Maybe the parsed 
> jxt could be put into the transient store or something?]
The parsed template model is being put into "local" cache - a map. That 
should go to transient store - I do not know the purpose of current 
solution.

> I would suggest:
> (a) patch the JXTemplateGenerator itself, rather than creating a new class.
Sure, this is what I am about to do. I just prepared the initial version 
that does not affect any existing sources.

> (b) Post this as a patch to Bugzilla. Then, hopefully someone'll get the 
> chance to check it and commit it.
OK

One more thing: The jx transformer will never be cacheable - not possible.

	LG

Re: JXTG and caching - IT'S DONE

Posted by Upayavira <uv...@upaya.co.uk>.
Leszek Gawron wrote:

> Upayavira wrote:
>
>>>> It seems to me that Sylvains suggested extension of jxt has a great 
>>>> deal of power in it.
>>>
>>>
>>>
>>> OK. Then I'll continue my work and try to provide the appropriate 
>>> patch as I have already started to make needed changes.
>>
>>
>>
>> Great. I'd love to see a cacheable jxt.
>>
>> Regards, Upayavira
>
> You can test the first implementation if you fetch this file :
> http://ouzo.wlkp.org/cjxtg.zip

Fab.

The below all reads okay to me. It would seem reasonable that the 
template must be read in setup now. [As an aside, is the jxt file 
cached? Or does it need to be read with every request? Maybe the parsed 
jxt could be put into the transient store or something?]

I would suggest:
(a) patch the JXTemplateGenerator itself, rather than creating a new class.
(b) Post this as a patch to Bugzilla. Then, hopefully someone'll get the 
chance to check it and commit it.

Regards, Upayavira

> it contains :
> CachingJXTemplateGenerator.java which you should put to the same 
> directory as JXTG.java and rebuild your cocoon.
>
> There also is a simple test case in test directory. Put it into your 
> webapp root and then issue this url:
>
> http://localhost:8888/test/test.do?key=<something>
> if I got it right the view is generated once for each key and cached 
> so the date on the page won't change if you refresh your page.
>
> How it works:
>
> 1. every attribute in "http://apache.org/cocoon/templates/jx/1.0" 
> namespace get stripped from attributes and end up in StartDocument 
> event in templateProperties map (which I added). This is something 
> that could be used for providing additional parameters to template in 
> the future.
> These attributes are not generated to output. They are only stored in 
> map. This was the simplest solution. I think it's acceptable. How 
> about you?
>
> 2. In template's root element (or any element really) you put:
> <page jx:cache-key="${biz.getKey()}" 
> jx:cache-validity=${biz.getValidity()}">
> The key should be Serializable and the Validity should be 
> SourceValidity of course.
>
> Voilla .. the page gets cached.
>
>
> What changed:
>
> 1. CachingJXTemplateGenerator implements CacheableProcessingComponent
>    of course :)
> 2. StartDocument.templateProperties added
> 3. I had to move template parsing from generate to setup(). Otherwise 
> that happened:
>
> - fetch http://localhost:8888/test/test.do?key=abc
> - setup() called
> - getKey() called - template not parsed yet, cannot compute key - no view
>   caching will be done
> - generate() - template gets parsed and generated, template cached
> - refresh browser
> - setup() called
> - getKey() - got parsed template, can evaluate cache-key
> - lookup from cache fails as the first response was not cached
> - generate() - template already parsed
> - response cached, getValidity() called somewhere of course
>
> The response gets cached after second page hit because getKey() first 
> time is called before template is parsed. When parsing is moved to 
> setup() this happens:
>
> - fetch http://localhost:8888/test/test.do?key=abc
> - setup() called, template gets parsed
> - getKey() called, template accessible, key computed
> - cache lookup falils
> - generate() called
> - response cached!
>
> This class can become standard JXTemplateGenerator as if you do not 
> provide caching attributes the page never gets cached so it works like 
> it did before.
>
> Please make your comments
>



Re: JXTG and caching - IT'S DONE

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Upayavira wrote:
>>> It seems to me that Sylvains suggested extension of jxt has a great 
>>> deal of power in it.
>>
>>
>> OK. Then I'll continue my work and try to provide the appropriate 
>> patch as I have already started to make needed changes.
> 
> 
> Great. I'd love to see a cacheable jxt.
> 
> Regards, Upayavira
You can test the first implementation if you fetch this file :
http://ouzo.wlkp.org/cjxtg.zip

it contains :
CachingJXTemplateGenerator.java which you should put to the same directory as 
JXTG.java and rebuild your cocoon.

There also is a simple test case in test directory. Put it into your webapp 
root and then issue this url:

http://localhost:8888/test/test.do?key=<something>
if I got it right the view is generated once for each key and cached so the 
date on the page won't change if you refresh your page.

How it works:

1. every attribute in "http://apache.org/cocoon/templates/jx/1.0" namespace 
get stripped from attributes and end up in StartDocument event in 
templateProperties map (which I added). This is something that could be used 
for providing additional parameters to template in the future.
These attributes are not generated to output. They are only stored in map. 
This was the simplest solution. I think it's acceptable. How about you?

2. In template's root element (or any element really) you put:
<page jx:cache-key="${biz.getKey()}" jx:cache-validity=${biz.getValidity()}">
The key should be Serializable and the Validity should be SourceValidity of 
course.

Voilla .. the page gets cached.


What changed:

1. CachingJXTemplateGenerator implements CacheableProcessingComponent
    of course :)
2. StartDocument.templateProperties added
3. I had to move template parsing from generate to setup(). Otherwise that 
happened:

- fetch http://localhost:8888/test/test.do?key=abc
- setup() called
- getKey() called - template not parsed yet, cannot compute key - no view
   caching will be done
- generate() - template gets parsed and generated, template cached
- refresh browser
- setup() called
- getKey() - got parsed template, can evaluate cache-key
- lookup from cache fails as the first response was not cached
- generate() - template already parsed
- response cached, getValidity() called somewhere of course

The response gets cached after second page hit because getKey() first time is 
called before template is parsed. When parsing is moved to setup() this happens:

- fetch http://localhost:8888/test/test.do?key=abc
- setup() called, template gets parsed
- getKey() called, template accessible, key computed
- cache lookup falils
- generate() called
- response cached!

This class can become standard JXTemplateGenerator as if you do not provide 
caching attributes the page never gets cached so it works like it did before.

Please make your comments

-- 
Leszek Gawron                                      lgawron@mobilebox.pl

Re: JXTG and caching

Posted by Upayavira <uv...@upaya.co.uk>.
Leszek Gawron wrote:

> Upayavira wrote:
>
>> I think you are getting something wrong here.
>>
>> If you have two objects bizObj1 and bizObj2, and you pass these to a 
>> pipeline that does
>>
>> JXT(bizObj1)->tx->tx-\
>>                      >-aggregate->tx->serlialize
>> JXT(bizObj2)->tx->tx-/
>>
>> You would do:
>>
>> sendPage("your-pipeline", {"bizObj1": bizObj1, "bizObj2": bizObj2});
>
> It feel like it breaks SoC a little as controller has to know how your 
> view is rendered. Also if you change your rendering to 3 aggregate 
> parts you have to make appropriate changes in controller.

Huh? Surely, your controller needs to tell the view what information 
(models) it needs to display? This is a necessary part of MVC?

<huge-snip about="wrapping a heavy business object"/>

>> Does that make sense?
>
> Now it does. I never thought of a wrapper. It's real pleasure to 
> discuss with you all.

Glad that helped.

>>
>> It seems to me that Sylvains suggested extension of jxt has a great 
>> deal of power in it.
>
> OK. Then I'll continue my work and try to provide the appropriate 
> patch as I have already started to make needed changes.

Great. I'd love to see a cacheable jxt.

Regards, Upayavira



Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Upayavira wrote:
> I think you are getting something wrong here.
> 
> If you have two objects bizObj1 and bizObj2, and you pass these to a 
> pipeline that does
> 
> JXT(bizObj1)->tx->tx-\
>                      >-aggregate->tx->serlialize
> JXT(bizObj2)->tx->tx-/
> 
> You would do:
> 
> sendPage("your-pipeline", {"bizObj1": bizObj1, "bizObj2": bizObj2});
It feel like it breaks SoC a little as controller has to know how your view is 
rendered. Also if you change your rendering to 3 aggregate parts you have to 
make appropriate changes in controller.

> Then, your objects would implement:
> 
> bizObj.getValidity()
> 
> And the jxtemplate would ask the business object for its validity as 
> Sylvain has described.
> 
> Thus, if bizObj1 says it can be cached, and bizObj2 says it can't, then 
> at least one of your two aggregated pipelines doesn't need to be 
> generated, which will speed up processing.
> 
> Remember, the jxtemplate pulls the validity from the business object. 
> The flow doesn't need to even know that caching might be happening (and 
> in fact _shouldn't_ know it is happening). If you have to write 
> flowscript to deal with this, then something is probably wrong.
> 
> If your business object takes an age to instantiate, and you can decide 
> whether or not to instantiate it based upon request parameters, then 
> wrap it in a lighter component that does (in pseudocode):
> 
> lightObject.getValidity() {
>  return request.parameters["a"]+ request.parameters["b"];
> }
> 
> lightObject.getHeavyBusinessObject() {
>  if (this.heavyBusinessObject == null) {
>     this.heavyBusinessObject = HeavyBusinessObjectBuilder.newObject();
>  }
>  return this.heavyBusinessObject;
> }
> 
> Then, in your jxt, you use Sylvains 
> jx:cacheKey="lightObject.getValidity()" construct, and then later in 
> your jxt, you access your heavy object with 
> #{/lightObject/HeavyObject/property1}. This latter expression will only 
> be invoked if the page is not cached.
> 
> Does that make sense?
Now it does. I never thought of a wrapper. It's real pleasure to discuss with 
you all.

> 
> It seems to me that Sylvains suggested extension of jxt has a great deal 
> of power in it.
OK. Then I'll continue my work and try to provide the appropriate patch as I 
have already started to make needed changes.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl

AW: JXTG and caching

Posted by Marco Rolappe <m_...@web.de>.

> -----Ursprungliche Nachricht-----
> Von: dev-return-61763-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61763-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Leszek Gawron
> Gesendet: Mittwoch, 2. Juni 2004 20:59
> An: dev@cocoon.apache.org
> Betreff: Re: AW: JXTG and caching
>
>
> Marco Rolappe wrote:
> > a cache entry with a composite validity (AggregatedValidity).
> yes, but I'd like to know exactly which part is valid and which is not.
> does aggregatedvalidity give me this ?

would have to take a look at AggregatedValidity... but you're not locked
into AggregatedValidity; you could have your own that tracked the parts
(keyed by e.g. their uri) and their validity.


Re: AW: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Marco Rolappe wrote:
> a cache entry with a composite validity (AggregatedValidity).
yes, but I'd like to know exactly which part is valid and which is not. 
does aggregatedvalidity give me this ?

>>- you mess with lazily fetched collections(hibernate, ojb):
>>   * collections retrieve data from backend during view rendering
>>   * only the needed data is being fetched so you would have to know
>>     EXACTLY what data the view needs and initialize it before caching.
>>     This way all the fun of using OR (hibernate) and templates is
>>     destroyed. When you have a object model with lazy fetching support
>>     you can extend your view without making ANY changes to the
>>     controller code as the model gives you access to much more data that
>>     you need at the moment.
> 
> 
> well, we have different mindsets regarding template usage. for me a template
> should be just a template; template data with holes in it to fill. following
> MVC the model should be agnostic of the view and the view should be agnostic
> of the model. the controller is the one who has all the knowledge. I don't
> like my view to depend on *implementation* details, then it really gets
> messy. anyway, the approach works well for me and I haven't yet stumbled
> over the problems you encountered.
If you use hibernate the view sees the object model as plain objects, 
collections. It does not know the data is being fetched when it's 
requested. This does not break SoC.
	Leszek Gawron

AW: JXTG and caching

Posted by Marco Rolappe <m_...@web.de>.

> -----Ursprungliche Nachricht-----
> Von: dev-return-61761-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61761-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Leszek Gawron
> Gesendet: Mittwoch, 2. Juni 2004 19:46
> An: dev@cocoon.apache.org
> Betreff: Re: AW: JXTG and caching
>
>
> > providing JXTG with a key and validity is the one side, i.e.
> > telling the pipeline infrastructure whether the cached artifact is still
> > valid or has to be regenerated. the other side is the validity of your
> > prepared view data (heavy business stuff), which is a different
> issue. but
> > it's also straightforward and should go something like this:
> >
> > - controller looks up cached data (view data/heavy business stuff)
> For view data this may not be a single cache entry (map:aggregate). what
> then ?

a cache entry with a composite validity (AggregatedValidity).

> > - if cache entry exists check validity
> > 	- if valid
> > 		- 'send' cached view data and validity to flow context
> > 	- if invalid
> > 		- prepare view data
> > 		- cache view data
> I think cocoon should not cache biz data because:
> - it may be memory consuming

regarding efficiency you always have to make tradeoffs between processing
(time) and memory. it also depends on the data's relevance (how often
requested, etc.) and other factors.

> - it is double caching really (if you cache the view also)

well, it's hard to decide for the general case. if the preparation is the
biggest overhead I'd have only that cached and the view not.

> - you mess with lazily fetched collections(hibernate, ojb):
>    * collections retrieve data from backend during view rendering
>    * only the needed data is being fetched so you would have to know
>      EXACTLY what data the view needs and initialize it before caching.
>      This way all the fun of using OR (hibernate) and templates is
>      destroyed. When you have a object model with lazy fetching support
>      you can extend your view without making ANY changes to the
>      controller code as the model gives you access to much more data that
>      you need at the moment.

well, we have different mindsets regarding template usage. for me a template
should be just a template; template data with holes in it to fill. following
MVC the model should be agnostic of the view and the view should be agnostic
of the model. the controller is the one who has all the knowledge. I don't
like my view to depend on *implementation* details, then it really gets
messy. anyway, the approach works well for me and I haven't yet stumbled
over the problems you encountered.


> > 		- 'send' view data and validity to flow context
> > - if cache entry doesn't exist
> > 	- prepare view data
> Which part of view data? If your full view data consists of 100 DB
> fetches and only the part that uses 2 has expired? How would you detect
> that?

well, you're completely free how granular your cache validity information
is. solely depends on your object model.



Re: AW: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Marco Rolappe wrote:
>>see my example of heavy biz data preparation. Suppose every
>>aggregation part
>>needs another part of bizData. If you make it simple and show
> 
> 
> don't know exactly what you mean with aggregation here. <map:aggregate>
> stuff?
yes

>>cached view only
>>if all parts were still valid it would be hardly usable. If you make it
> 
> 
> if you're talking about <map:aggregate> stuff the aggregate would be
> 'invalid'. the still valid parts wouldn't have to be regenerated, tough.
> thus, only the invalid parts and the aggregate would have to be regenerated.
you're right but I am talking also about biz data provided from flow.

> hard to discuss without a concrete exmaple. another point to note is the
> difference between caching the view data/bizdata and the cacheability of the
> pipeline stage; 
this is what I've been talking from the start. That is why we did not 
understand each other.

> providing JXTG with a key and validity is the one side, i.e.
> telling the pipeline infrastructure whether the cached artifact is still
> valid or has to be regenerated. the other side is the validity of your
> prepared view data (heavy business stuff), which is a different issue. but
> it's also straightforward and should go something like this:
> 
> - controller looks up cached data (view data/heavy business stuff)
For view data this may not be a single cache entry (map:aggregate). what 
then ?

> - if cache entry exists check validity
> 	- if valid
> 		- 'send' cached view data and validity to flow context
> 	- if invalid
> 		- prepare view data
> 		- cache view data
I think cocoon should not cache biz data because:
- it may be memory consuming
- it is double caching really (if you cache the view also)
- you mess with lazily fetched collections(hibernate, ojb):
   * collections retrieve data from backend during view rendering
   * only the needed data is being fetched so you would have to know
     EXACTLY what data the view needs and initialize it before caching.
     This way all the fun of using OR (hibernate) and templates is
     destroyed. When you have a object model with lazy fetching support
     you can extend your view without making ANY changes to the
     controller code as the model gives you access to much more data that
     you need at the moment.

> 		- 'send' view data and validity to flow context
> - if cache entry doesn't exist
> 	- prepare view data
Which part of view data? If your full view data consists of 100 DB 
fetches and only the part that uses 2 has expired? How would you detect 
that?

> 	- cache view data
> 	- 'send' view data and validity to flow context

My regards
	LG

AW: JXTG and caching

Posted by Marco Rolappe <m_...@web.de>.

> -----Ursprungliche Nachricht-----
> Von: dev-return-61756-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61756-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Leszek Gawron
> Gesendet: Mittwoch, 2. Juni 2004 15:37
> An: dev@cocoon.apache.org
> Betreff: Re: AW: JXTG and caching
>
>
> Marco Rolappe wrote:
> >
> >>-----Ursprungliche Nachricht-----
> >>Von: dev-return-61751-m_rolappe=web.de@cocoon.apache.org
> >>[mailto:dev-return-61751-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> >>von Leszek Gawron
> >>Gesendet: Mittwoch, 2. Juni 2004 14:21
> >>An: dev@cocoon.apache.org
> >>Betreff: Re: AW: JXTG and caching
> >>
> >>
> >>Marco Rolappe wrote:
> >>
> >>>>If your business object takes an age to instantiate, and you
> can decide
> >>>>whether or not to instantiate it based upon request parameters, then
> >>>>wrap it in a lighter component that does (in pseudocode):
> >>>
> >>>
> >>>IMO that's what the controller would be supposed to do; just
> >>
> >>like preparing
> >>
> >>>the bizData and 'sending' it to the flow context, it would
> determine the
> >>>caching info and send it to the flow context. then JXT would
> >>
> >>use the bizdata
> >>
> >>>and accompanying cache info from the flow context. SoC IMO.
> >>
> >>Imagine that you have a view that is constructed out of several
> >>aggregates
> >>(nested to be harder). Every aggregation part has it's own
> >>validity so while
> >>preparing data you would have to consider the exact view composition and
> >>generate view for those parts which already expired. Not so
> much SoC IMO.
> >
> >
> > composite view would imply composite validity, so I don't see a problem.
> > still SoC IMO ;-)
> see my example of heavy biz data preparation. Suppose every
> aggregation part
> needs another part of bizData. If you make it simple and show

don't know exactly what you mean with aggregation here. <map:aggregate>
stuff?

> cached view only
> if all parts were still valid it would be hardly usable. If you make it

if you're talking about <map:aggregate> stuff the aggregate would be
'invalid'. the still valid parts wouldn't have to be regenerated, tough.
thus, only the invalid parts and the aggregate would have to be regenerated.

> complex the controller needs to know which data to prepare so it
> has to know
> view rendering details which breaks SoC IMO.

hard to discuss without a concrete exmaple. another point to note is the
difference between caching the view data/bizdata and the cacheability of the
pipeline stage; providing JXTG with a key and validity is the one side, i.e.
telling the pipeline infrastructure whether the cached artifact is still
valid or has to be regenerated. the other side is the validity of your
prepared view data (heavy business stuff), which is a different issue. but
it's also straightforward and should go something like this:

- controller looks up cached data (view data/heavy business stuff)
- if cache entry exists check validity
	- if valid
		- 'send' cached view data and validity to flow context
	- if invalid
		- prepare view data
		- cache view data
		- 'send' view data and validity to flow context
- if cache entry doesn't exist
	- prepare view data
	- cache view data
	- 'send' view data and validity to flow context


Re: AW: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Marco Rolappe wrote:
> 
>>-----Ursprungliche Nachricht-----
>>Von: dev-return-61751-m_rolappe=web.de@cocoon.apache.org
>>[mailto:dev-return-61751-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
>>von Leszek Gawron
>>Gesendet: Mittwoch, 2. Juni 2004 14:21
>>An: dev@cocoon.apache.org
>>Betreff: Re: AW: JXTG and caching
>>
>>
>>Marco Rolappe wrote:
>>
>>>>If your business object takes an age to instantiate, and you can decide
>>>>whether or not to instantiate it based upon request parameters, then
>>>>wrap it in a lighter component that does (in pseudocode):
>>>
>>>
>>>IMO that's what the controller would be supposed to do; just
>>
>>like preparing
>>
>>>the bizData and 'sending' it to the flow context, it would determine the
>>>caching info and send it to the flow context. then JXT would
>>
>>use the bizdata
>>
>>>and accompanying cache info from the flow context. SoC IMO.
>>
>>Imagine that you have a view that is constructed out of several
>>aggregates
>>(nested to be harder). Every aggregation part has it's own
>>validity so while
>>preparing data you would have to consider the exact view composition and
>>generate view for those parts which already expired. Not so much SoC IMO.
> 
> 
> composite view would imply composite validity, so I don't see a problem.
> still SoC IMO ;-)
see my example of heavy biz data preparation. Suppose every aggregation part 
needs another part of bizData. If you make it simple and show cached view only 
if all parts were still valid it would be hardly usable. If you make it 
complex the controller needs to know which data to prepare so it has to know 
view rendering details which breaks SoC IMO.


-- 
Leszek Gawron

AW: JXTG and caching

Posted by Marco Rolappe <m_...@web.de>.

> -----Ursprungliche Nachricht-----
> Von: dev-return-61751-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61751-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Leszek Gawron
> Gesendet: Mittwoch, 2. Juni 2004 14:21
> An: dev@cocoon.apache.org
> Betreff: Re: AW: JXTG and caching
>
>
> Marco Rolappe wrote:
> >>If your business object takes an age to instantiate, and you can decide
> >>whether or not to instantiate it based upon request parameters, then
> >>wrap it in a lighter component that does (in pseudocode):
> >
> >
> > IMO that's what the controller would be supposed to do; just
> like preparing
> > the bizData and 'sending' it to the flow context, it would determine the
> > caching info and send it to the flow context. then JXT would
> use the bizdata
> > and accompanying cache info from the flow context. SoC IMO.
> Imagine that you have a view that is constructed out of several
> aggregates
> (nested to be harder). Every aggregation part has it's own
> validity so while
> preparing data you would have to consider the exact view composition and
> generate view for those parts which already expired. Not so much SoC IMO.

composite view would imply composite validity, so I don't see a problem.
still SoC IMO ;-)

> >>It seems to me that Sylvains suggested extension of jxt has a great deal
> >>of power in it.
> >
> >
> > yeah, why don't introduce jx:logic so we can do it directly in
> the template?
> And end up like XSP did ? :)

yeah, that was the 'plan' ;-) why SoC if you can do it all in one place?


Re: AW: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Marco Rolappe wrote:
>>If your business object takes an age to instantiate, and you can decide
>>whether or not to instantiate it based upon request parameters, then
>>wrap it in a lighter component that does (in pseudocode):
> 
> 
> IMO that's what the controller would be supposed to do; just like preparing
> the bizData and 'sending' it to the flow context, it would determine the
> caching info and send it to the flow context. then JXT would use the bizdata
> and accompanying cache info from the flow context. SoC IMO.
Imagine that you have a view that is constructed out of several aggregates 
(nested to be harder). Every aggregation part has it's own validity so while 
preparing data you would have to consider the exact view composition and 
generate view for those parts which already expired. Not so much SoC IMO.


>>It seems to me that Sylvains suggested extension of jxt has a great deal
>>of power in it.
> 
> 
> yeah, why don't introduce jx:logic so we can do it directly in the template?
And end up like XSP did ? :)

-- 
Leszek Gawron                                      lgawron@mobilebox.pl

AW: JXTG and caching

Posted by Marco Rolappe <m_...@web.de>.

> -----Ursprüngliche Nachricht-----
> Von: dev-return-61743-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61743-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Upayavira
> Gesendet: Mittwoch, 2. Juni 2004 11:52
> An: dev@cocoon.apache.org
> Betreff: Re: JXTG and caching
>

<snip/>

> Remember, the jxtemplate pulls the validity from the business object.
> The flow doesn't need to even know that caching might be happening (and
> in fact _shouldn't_ know it is happening). If you have to write
> flowscript to deal with this, then something is probably wrong.

that's what I don't get/where I disagree. flow is supposed to be the
controller, isn't it? the controller is supposed to act as a mediator
between the model and the view. I don't think that the view (jxt) should be
responsible for model cacheability, but the controller should be. the
controller is the one to know about the model and to prepare/populate the
view. this also means the controller is the best place to handle the
cacheability because it knows which data/part of the model is relevant.

>
> If your business object takes an age to instantiate, and you can decide
> whether or not to instantiate it based upon request parameters, then
> wrap it in a lighter component that does (in pseudocode):

IMO that's what the controller would be supposed to do; just like preparing
the bizData and 'sending' it to the flow context, it would determine the
caching info and send it to the flow context. then JXT would use the bizdata
and accompanying cache info from the flow context. SoC IMO.

>
> lightObject.getValidity() {
>   return request.parameters["a"]+ request.parameters["b"];
> }
>
> lightObject.getHeavyBusinessObject() {
>   if (this.heavyBusinessObject == null) {
>      this.heavyBusinessObject = HeavyBusinessObjectBuilder.newObject();
>   }
>   return this.heavyBusinessObject;
> }
>
> Then, in your jxt, you use Sylvains
> jx:cacheKey="lightObject.getValidity()" construct, and then later in
> your jxt, you access your heavy object with
> #{/lightObject/HeavyObject/property1}. This latter expression will only
> be invoked if the page is not cached.
>
> Does that make sense?
>
> It seems to me that Sylvains suggested extension of jxt has a great deal
> of power in it.

yeah, why don't introduce jx:logic so we can do it directly in the template?


Re: JXTG and caching

Posted by Upayavira <uv...@upaya.co.uk>.
Leszek Gawron wrote:

> Replying myself :)
>
> Leszek Gawron wrote:
>
>> It would have to look somewhat like this:
>> function showData() {
>>   var validityKey = "request-param:a!!spacer!!request-param:b";
>>   var pipeline = "view/someview.jx"
>>   if ( someCacheManager.isValid( pipeline, validityKey ) ) {
>>     // hope the cache entry does not expire between isValid and page
>>     // generation
>>     var dummyBizData = {};
>>     cocoon.sendPage( "view/someview.jx", dummyBizData );
>>     return;
>>   }
>>
>>   // the page is not cached
>>   // do the heavy processing here and send new page
>>   var bizData = {};
>>   bizData[ "pageData" ] = fetchBizDataByHeavyDBRetrieval();
>>   var validity = cocoon.createObject( DBViewValidity );
>>   bizData[ "cacheingInfo" ] = validity;
>>   cocoon.sendPage( "view/someview.jx",
>>                    bizData,
>>                    function() {
>>                      cocoon.releaseObject( validity );
>>                    } );
>> }
>>
>> It does not look like SoC.
>
> It does not look like it is possible to implement either if you take 
> aggregated views into account - one part of the view might need 
> rendering because it expired while other could still be fetched from 
> cache. Controller would have to know what parts is the view build of 
> and generate appropriate data which breaks SoC completely.
>
> The conclusion is: the only way we can generate a cached view is to 
> use some technlogogy that "pulls" data (like a java generator or XSP). 
> While xsp is not considered a good practise nowadays it needs a 
> replacement fast.
>
> JXTG could be made cacheable for simple cases - ones that do not need 
> heavy bizData preparation. Still JXTG gets data pushed into which 
> needs prior data retrieval. In cases when data preparation is much 
> more resource consuming than view rendering caching hardly makes sense.

I think you are getting something wrong here.

If you have two objects bizObj1 and bizObj2, and you pass these to a 
pipeline that does

JXT(bizObj1)->tx->tx-\
                      >-aggregate->tx->serlialize
JXT(bizObj2)->tx->tx-/

You would do:

sendPage("your-pipeline", {"bizObj1": bizObj1, "bizObj2": bizObj2});

Then, your objects would implement:

bizObj.getValidity()

And the jxtemplate would ask the business object for its validity as 
Sylvain has described.

Thus, if bizObj1 says it can be cached, and bizObj2 says it can't, then 
at least one of your two aggregated pipelines doesn't need to be 
generated, which will speed up processing.

Remember, the jxtemplate pulls the validity from the business object. 
The flow doesn't need to even know that caching might be happening (and 
in fact _shouldn't_ know it is happening). If you have to write 
flowscript to deal with this, then something is probably wrong.

If your business object takes an age to instantiate, and you can decide 
whether or not to instantiate it based upon request parameters, then 
wrap it in a lighter component that does (in pseudocode):

lightObject.getValidity() {
  return request.parameters["a"]+ request.parameters["b"];
}

lightObject.getHeavyBusinessObject() {
  if (this.heavyBusinessObject == null) {
     this.heavyBusinessObject = HeavyBusinessObjectBuilder.newObject();
  }
  return this.heavyBusinessObject;
}

Then, in your jxt, you use Sylvains 
jx:cacheKey="lightObject.getValidity()" construct, and then later in 
your jxt, you access your heavy object with 
#{/lightObject/HeavyObject/property1}. This latter expression will only 
be invoked if the page is not cached.

Does that make sense?

It seems to me that Sylvains suggested extension of jxt has a great deal 
of power in it.

Regards, Upayavira




Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Replying myself :)

Leszek Gawron wrote:
> It would have to look somewhat like this:
> function showData() {
>   var validityKey = "request-param:a!!spacer!!request-param:b";
>   var pipeline = "view/someview.jx"
>   if ( someCacheManager.isValid( pipeline, validityKey ) ) {
>     // hope the cache entry does not expire between isValid and page
>     // generation
>     var dummyBizData = {};
>     cocoon.sendPage( "view/someview.jx", dummyBizData );
>     return;
>   }
> 
>   // the page is not cached
>   // do the heavy processing here and send new page
>   var bizData = {};
>   bizData[ "pageData" ] = fetchBizDataByHeavyDBRetrieval();
>   var validity = cocoon.createObject( DBViewValidity );
>   bizData[ "cacheingInfo" ] = validity;
>   cocoon.sendPage( "view/someview.jx",
>                    bizData,
>                    function() {
>                      cocoon.releaseObject( validity );
>                    } );
> }
> 
> It does not look like SoC.
It does not look like it is possible to implement either if you take 
aggregated views into account - one part of the view might need rendering 
because it expired while other could still be fetched from cache. Controller 
would have to know what parts is the view build of and generate appropriate 
data which breaks SoC completely.

The conclusion is: the only way we can generate a cached view is to use some 
technlogogy that "pulls" data (like a java generator or XSP). While xsp is not 
considered a good practise nowadays it needs a replacement fast.

JXTG could be made cacheable for simple cases - ones that do not need heavy 
bizData preparation. Still JXTG gets data pushed into which needs prior data 
retrieval. In cases when data preparation is much more resource consuming than 
view rendering caching hardly makes sense.

-- 
Leszek Gawron                                      lgawron@mobilebox.pl

Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:
> Leszek Gawron wrote:
> 
>> While almost everybody buried XSP it has got one great feature that 
>> JXTG  lacks: it's cacheable. I know that there is no way to implement 
>> automatic JXTG caching but what if I know on what env does jx template 
>> operate. If I know that it is for example only request param "abc" and 
>> "bcd" and session attribute "xxx" then I know when I can use cached 
>> entry. The question is: how?
>>
>> Should I extend JXTemplateGenerator so it is a cached component? I 
>> understand it gets the validity methods to implement then. So every 
>> time I call my extended component from flow I pass also the validity 
>> calculated in flow. Something like this:
>>
>> function niceCachedResponse() {
>>   var cacheKey = cocoon.request.get( "abc" ) + "!!spacer!!"
>>                     + cocoon.request.get( "bcd" ) + "!!spacer!!"
>>                     + cocoon.session.getAttribute( "xxx" );
>>   var pageValidity = new DeltaTimeCacheValidity( 60 * 60 * 24 );
>>                                                 // one day
>>   cocoon.sendPage( "view/cached.jx", {
>>                                      JXTGExtCacheKey: cacheKey,
>>                                      JXTGExtpageValidity: pageValidity
>>                                      someOtherBizData: data } );
>> }
>>
>> Does it have a chance to work ?
> 
> 
> 
> Well, it _could_ have a chance to work, but flowscript is definitely not 
> the appropriate location to compute cache information for an element of 
> the view pipeline.

I started to implement the functionality we discussed but then it hit me: it's 
completely useles for my case. Look at this:

function showData() {
   var bizData = {};
   bizData[ "pageData" ] = fetchBizDataByHeavyDBRetrieval();
   var validity = cocoon.createObject( DBViewValidity );
   bizData[ "cacheingInfo" ] = validity;
   cocoon.sendPage( "view/someview.jx",
                    bizData,
                    function() {
                      cocoon.releaseObject( validity );
                    } );
}

What is the point of implementing caching control in JXTG in this case if I do 
  "heavy db retrieval" anyway.

CachingJXTG is good here only for those cases which in which one can omit 
bizData preparation.

It would have to look somewhat like this:
function showData() {
   var validityKey = "request-param:a!!spacer!!request-param:b";
   var pipeline = "view/someview.jx"
   if ( someCacheManager.isValid( pipeline, validityKey ) ) {
     // hope the cache entry does not expire between isValid and page
     // generation
     var dummyBizData = {};
     cocoon.sendPage( "view/someview.jx", dummyBizData );
     return;
   }

   // the page is not cached
   // do the heavy processing here and send new page
   var bizData = {};
   bizData[ "pageData" ] = fetchBizDataByHeavyDBRetrieval();
   var validity = cocoon.createObject( DBViewValidity );
   bizData[ "cacheingInfo" ] = validity;
   cocoon.sendPage( "view/someview.jx",
                    bizData,
                    function() {
                      cocoon.releaseObject( validity );
                    } );
}

It does not look like SoC.
	LG
-- 
Leszek Gawron

Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:

> Well, although not the nicest construct, I still prefer it to the one 
> you initially proposed, as the cache information is still triggered by 
> the generator, even if the corresponding code is in the flowscript.
Right, I didn't think of that. Your solution is a "pull" while mine is "push" one.

-- 
Leszek Gawron

Re: JXTG and caching

Posted by Torsten Curdt <tc...@vafer.org>.
> I actually do think that the JX language can be rewritten as a Jelly 
> taglib. Any taker?

Actually when talking about alternatives to XSP also Jelly
came to my mind. What do you think?

Plus it already comes with a good set of taglibs.

cheers
--
Torsten

Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> Sylvain Wallez wrote:
>
>> Well, it _could_ have a chance to work, but flowscript is definitely 
>> not the appropriate location to compute cache information for an 
>> element of the view pipeline.
>
> not the appropriate .. but really there is no other place ..
>
>> The problem with JXG is that the template is the generate() method 
>> _only_ whereas an XSP is the full class, allowing to define getKey() 
>> and getValidity().
>>
>> So what about adding two optional attributes on the template's root 
>> element, defining the cache information?
>>
>> <page
>>    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>>    jx:cache-key='${cocoon.request.get("abc") + "!!spacer!!" + 
>> cocoon.request.get("bcd")....'
>>    jx:cache-validity='${new DeltaTimeCacheValidity(60 * 60 * 24)}'>
>>    ... template ....
>> </page>
>>
>> WDYT?
>
> Fine for simple cases but limits of this solution will appear very fast.
> I just know what would users do (what I would also do if I wanted 
> something more complex)
>
> the .js file:
>
> function validityForView() {
>   var validity = new SomeVeryHardToComputeInOneLineValidity();
>   return validity;
> }
>
> function keyForView() {
>   var key = someVeryStrangeKeyFetchedFromSomewhere;
>   return key;
> }
>
> function showSomeView() {
>   cocoon.session.setAttribute( "myViewValidityFunc", validityForView );
>   cocoon.session.setAttribute( "myViewKey", keyForView );
>   cocoon.sendPage( "view/myview.jx", {} );
> }
>
> the template:
> <page
>    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>    jx:cache-key='${cocoon.session.myViewKey()}'
>    jx:cache-validity='${cocoon.session.myViewValidityFunc()}'>
>    ... template ....
> </page>


Well, although not the nicest construct, I still prefer it to the one 
you initially proposed, as the cache information is still triggered by 
the generator, even if the corresponding code is in the flowscript.

> This and other examples proove that we need a way to extend jxtemplate 
> generator with flow functions (or other logic "providers") when simple 
> macro is not enough. This may lead to a jsb taglib nightmare but hey: 
> user has got his own brain not to put database inserts in there and if 
> he is he's just asking for troubles.
>
> I still do not understand why we do have jx:formatDate and 
> jx:formatNumber which are just some instances of formatters while 
> there either should be none at all or just pluggable ones.


<snip/>

I totally agree with you. The <jx:macro> is good for simple cases, but 
we need taglibs à la XSP or Jelly.

I actually do think that the JX language can be rewritten as a Jelly 
taglib. Any taker?

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:
> Well, it _could_ have a chance to work, but flowscript is definitely not 
> the appropriate location to compute cache information for an element of 
> the view pipeline.
not the appropriate .. but really there is no other place ..

> The problem with JXG is that the template is the generate() method 
> _only_ whereas an XSP is the full class, allowing to define getKey() and 
> getValidity().
> 
> So what about adding two optional attributes on the template's root 
> element, defining the cache information?
> 
> <page
>    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>    jx:cache-key='${cocoon.request.get("abc") + "!!spacer!!" + 
> cocoon.request.get("bcd")....'
>    jx:cache-validity='${new DeltaTimeCacheValidity(60 * 60 * 24)}'>
>    ... template ....
> </page>
> 
> WDYT?
Fine for simple cases but limits of this solution will appear very fast.
I just know what would users do (what I would also do if I wanted something 
more complex)

the .js file:

function validityForView() {
   var validity = new SomeVeryHardToComputeInOneLineValidity();
   return validity;
}

function keyForView() {
   var key = someVeryStrangeKeyFetchedFromSomewhere;
   return key;
}

function showSomeView() {
   cocoon.session.setAttribute( "myViewValidityFunc", validityForView );
   cocoon.session.setAttribute( "myViewKey", keyForView );
   cocoon.sendPage( "view/myview.jx", {} );
}

the template:
<page
    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
    jx:cache-key='${cocoon.session.myViewKey()}'
    jx:cache-validity='${cocoon.session.myViewValidityFunc()}'>
    ... template ....
</page>

This and other examples proove that we need a way to extend jxtemplate 
generator with flow functions (or other logic "providers") when simple macro 
is not enough. This may lead to a jsb taglib nightmare but hey: user has got 
his own brain not to put database inserts in there and if he is he's just 
asking for troubles.

I still do not understand why we do have jx:formatDate and jx:formatNumber 
which are just some instances of formatters while there either should be none 
at all or just pluggable ones. Like these:

> function stringToSAX( str, consumer, ignoreRootElement ) {
> 	var is = new Packages.org.xml.sax.InputSource( new java.io.StringReader( str ) );
> 	var ignore = ( ignoreRootElement == "true" );
> 	var parser = null;
> 	var includeConsumer = new org.apache.cocoon.xml.IncludeXMLConsumer( consumer, consumer );
> 	includeConsumer.setIgnoreRootElement( true );
> 	try {	
> 		parser = cocoon.getComponent( Packages.org.apache.excalibur.xml.sax.SAXParser.ROLE );
> 		parser.parse( is, includeConsumer );	
> 	} finally {
> 		if ( parser != null ) cocoon.releaseComponent( parser );
> 	}
> }

> function wikiToSAX( str, consumer ) {
> 	var radeoxStr = radeoxEngine.render( str, radeoxContext );
> 	var buffer = new java.lang.StringBuffer("<root>" );
> 	buffer.append( radeoxStr )
> 	buffer.append( "</root>" );
> 	var is = new Packages.org.xml.sax.InputSource( new java.io.StringReader( buffer.toString() ) );
> 
> 	var parser = null;
> 	var includeConsumer = new org.apache.cocoon.xml.IncludeXMLConsumer( consumer, consumer );
> 	includeConsumer.setIgnoreRootElement( true );
> 	try {	
> 		parser = cocoon.getComponent( Packages.org.apache.excalibur.xml.sax.SAXParser.ROLE );
> 		parser.parse( is, includeConsumer );	
> 	} finally {
> 		if ( parser != null ) cocoon.releaseComponent( parser );
> 	}
> }

Now I have to do the same session trick and build a macro:

> 	<jx:macro name="wiki-string">
> 		<jx:parameter name="value"/>
> 		<jx:set var="ignored" value="${cocoon.session.wikify( value, cocoon.consumer )}"/>
> 	</jx:macro>

I did not mean to sound unpleasant by any way. I would like to contribute 
something more than bug reports but I do not know cocoon's internals that much 
to catch all possible problems.

-- 
Leszek Gawron

Re: JXTG and caching

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 1 juin 04, à 10:48, Sylvain Wallez a écrit :
> ...Taking your example of news urgency, the corresponding algorithm 
> would certainly not fit into a single attribute (because of the "if" 
> constructs it's likely to contain), but could simply be written as :
>
>  <page jx:cache-key="news.id" 
> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">

Sounds good!

> Moreover, I find that being limited to a single attribute enforces 
> good SoC by forbidding complicated code in the template.

+1

-Bertrand


Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> Sylvain Wallez wrote:
>
>> Leszek Gawron wrote:
>>
>>> Sylvain Wallez wrote:
>>>
>>>>  <page jx:cache-key="news.id" 
>>>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>>>
>>>
>>>
>>> One question: if this NewsHelper is NOT an avalon component (or 
>>> object instantiated by flow.createObject) then it doesn't have acces 
>>> to any backend logic or data (for example database that stores 
>>> expiration time for different news services). Would it be possible 
>>> to instantiate this object via flow.createObject analogy?
>>
>>
>>
>>
>> Two anwsers to this:
>> - we could add a builtin "createObject()" to the JXG expression 
>> language, thus directly solving your problem. I don't think this is a 
>> good idea as it opens the door to too much code in the template.
>
> I do not like it either.
>
>> - the NewsHelper object can be created in the flow and passed as 
>> viewData. This keeps things clean: the flow gives necessary data to 
>> the view, and the view pulls what it needs from that data, including 
>> means to compute the cache information.
>
> That's fine by me. It's nearly the same what I proposed (if not the 
> same). If somebody gave me some points I would like to implement some 
> version of it.
>
> Thinking out loud:
> 1. flow passes cache info to template via some object instance that 
> provides cache key and validity. It could be 2 objects (one for key 
> one for validity) but it looks like one object is cleaner (we could 
> impose some interface - is there one in cocoon already?)


What flow passes to the view is completely up to your choices.

> 2. There is CachingJXTemplateGenerator that extends the one we have now.
>
> 3. CJXTG implements getKey() and getValidity(). According to cocoon 
> docs if null is passed from these methods the result is not being 
> cached. So we could even modify the existing generator and have only 
> one that does caching or not.


Yep. No need for a cacheable extension.

> 4. "Compiled" page template has to hold 2 more values - cache key and 
> validity assuming the values are Jexl or JXpath expressions.


Yep.

> 5. getValidity() and getKey() have access to the template and flow 
> bizData (via FlowHelper). When invoked they get appropriate 
> jexl(jxpath) expression and evaluate it against bizData. If there is 
> no jx:cache-key jx:validity defined then null is returned and no 
> caching is being performed.


Exactly.

> 6. voilla we have a jxtemplate that is very performant for sites 
> slashdot like  (I mean lots of reads, infrequent page updates).


Yupp.

> I would like to become an active contributor but the entry step is too 
> big without your help. I know it sounds a bit like a waste of time 
> talking some simple functionality over and then doing the review 
> instead of implementing it by oneself but if you choose the first 
> variant you'll have a new active contributor in some future (near I hope).


You also have to consider time: we often don't have the time to 
implement all the nice feature we discuss here, and new contributors are 
therefore very welcome.

So ask whatever you need to get started, we'll try to help you in your 
way inside the Cocoon internals. Others will certainly learn from that 
too. About caching, you may want to read a wiki page [1] explaining how 
the pipeline machinery works.

[1] http://wiki.cocoondev.org/Wiki.jsp?page=WritingForCacheEfficiency

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:

> Leszek Gawron wrote:
> 
>> Sylvain Wallez wrote:
>>
>>>  <page jx:cache-key="news.id" 
>>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>>
>>
>> One question: if this NewsHelper is NOT an avalon component (or object 
>> instantiated by flow.createObject) then it doesn't have acces to any 
>> backend logic or data (for example database that stores expiration 
>> time for different news services). Would it be possible to instantiate 
>> this object via flow.createObject analogy?
> 
> 
> 
> Two anwsers to this:
> - we could add a builtin "createObject()" to the JXG expression 
> language, thus directly solving your problem. I don't think this is a 
> good idea as it opens the door to too much code in the template.
I do not like it either.

> - the NewsHelper object can be created in the flow and passed as 
> viewData. This keeps things clean: the flow gives necessary data to the 
> view, and the view pulls what it needs from that data, including means 
> to compute the cache information.
That's fine by me. It's nearly the same what I proposed (if not the same). If 
somebody gave me some points I would like to implement some version of it.

Thinking out loud:
1. flow passes cache info to template via some object instance that provides 
cache key and validity. It could be 2 objects (one for key one for validity) 
but it looks like one object is cleaner (we could impose some interface - is 
there one in cocoon already?)

2. There is CachingJXTemplateGenerator that extends the one we have now.

3. CJXTG implements getKey() and getValidity(). According to cocoon docs if 
null is passed from these methods the result is not being cached. So we could 
even modify the existing generator and have only one that does caching or not.

4. "Compiled" page template has to hold 2 more values - cache key and validity 
assuming the values are Jexl or JXpath expressions.

5. getValidity() and getKey() have access to the template and flow bizData 
(via FlowHelper). When invoked they get appropriate jexl(jxpath) expression 
and evaluate it against bizData. If there is no jx:cache-key jx:validity 
defined then null is returned and no caching is being performed.

6. voilla we have a jxtemplate that is very performant for sites slashdot like 
  (I mean lots of reads, infrequent page updates).

I would like to become an active contributor but the entry step is too big 
without your help. I know it sounds a bit like a waste of time talking some 
simple functionality over and then doing the review instead of implementing it 
by oneself but if you choose the first variant you'll have a new active 
contributor in some future (near I hope).

-- 
Leszek Gawron

Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> Sylvain Wallez wrote:
>
>>  <page jx:cache-key="news.id" 
>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>
> One question: if this NewsHelper is NOT an avalon component (or object 
> instantiated by flow.createObject) then it doesn't have acces to any 
> backend logic or data (for example database that stores expiration 
> time for different news services). Would it be possible to instantiate 
> this object via flow.createObject analogy?


Two anwsers to this:
- we could add a builtin "createObject()" to the JXG expression 
language, thus directly solving your problem. I don't think this is a 
good idea as it opens the door to too much code in the template.
- the NewsHelper object can be created in the flow and passed as 
viewData. This keeps things clean: the flow gives necessary data to the 
view, and the view pulls what it needs from that data, including means 
to compute the cache information.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Upayavira <uv...@upaya.co.uk>.
Leszek Gawron wrote:

> Upayavira wrote:
>
>> Leszek Gawron wrote:
>>
>>> Sylvain Wallez wrote:
>>>
>>>>  <page jx:cache-key="news.id" 
>>>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>>>
>>>
>>>
>>> One question: if this NewsHelper is NOT an avalon component (or 
>>> object instantiated by flow.createObject) then it doesn't have acces 
>>> to any backend logic or data (for example database that stores 
>>> expiration time for different news services). Would it be possible 
>>> to instantiate this object via flow.createObject analogy?
>>
>>
>>
>> Of course. You could equally well say:
>>
>> sendPage("my-url", {"myObject", myAvalonObject});
>>
>> <page jx-cache-key="myObject.getValidity()">
>>
>> Regards, Upayavira
>>
>>
>>
> But that does not differ from what I proposed in first post and 
> Sylvain wrote it is a bad idea.

I would say it is different, in as much as previously you calculated the 
validity in flowscript. Now, you pass in an object, and the template 
asks the object for its validity. So the object itself knows how to 
decide whether it is valid or not, which seems quite reasonable to me.

Adding that level of complexity to flowscript can make your flowscripts 
very complex very quickly. That is what I believe Sylvain is objecting to.

Regards, Upayavira




Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> Sylvain Wallez wrote:
>
>> Vadim Gritsenko wrote:
>>
>>> Sylvain Wallez wrote:
>>>
>>>> Moreover, you may consider the case of complex pipelines where the 
>>>> actual template depends on the user rights, target browser or 
>>>> whatever else. In that case, the cache information can be different 
>>>> depending on used template.
>>>
>>>
>>>
>>> And for caching to play right with proxies and browsers, Vary header 
>>> (and/or some others) should be set. What mechanism can be used to 
>>> set headers from the JX template?
>>
>>
>>
>> <jx:var name="foo" value="cocoon.request.setHeader('bar', 'baz')"/> ??
>
>
>
> Thought a bit about it... Due to streaming nature of the Cocoon 
> (forget for a second that pipeline's default behavior is to buffer 
> output - default can be changed anyway), this won't work. First 
> startElement() event corresponding to root element is already out, 
> and, under ideal conditions, already written to the output stream.
>
> Thus, this will fail.


You're right. Also, if the pipeline is cached, headers aren't set again. 
Headers must actually be set in setup().

> The only other option I see is to mimic XSP page structure: Introduce 
> root <jx:page/> tag which is *not* printed to the output. Then, 
> everything between the <jx:page/> and first SAX event can perform 
> whatever setup operations needed.


That additional root tag is something I dislike in XSP. Furthermore, a 
valid JX template may not contain any <jx:*> element if only expression 
expansion is used. Adding an additional mandatory root tag breaks this 
simplicity.

Don't know... what about some <jx:header> statements that would be 
allowed only as children of the root element and executed in setup()?

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Sylvain Wallez wrote:

> Vadim Gritsenko wrote:
>
>> Sylvain Wallez wrote:
>>
>>> Moreover, you may consider the case of complex pipelines where the 
>>> actual template depends on the user rights, target browser or 
>>> whatever else. In that case, the cache information can be different 
>>> depending on used template.
>>
>>
>> And for caching to play right with proxies and browsers, Vary header 
>> (and/or some others) should be set. What mechanism can be used to set 
>> headers from the JX template?
>
>
> <jx:var name="foo" value="cocoon.request.setHeader('bar', 'baz')"/> ??


Thought a bit about it... Due to streaming nature of the Cocoon (forget 
for a second that pipeline's default behavior is to buffer output - 
default can be changed anyway), this won't work. First startElement() 
event corresponding to root element is already out, and, under ideal 
conditions, already written to the output stream.

Thus, this will fail.

The only other option I see is to mimic XSP page structure: Introduce 
root <jx:page/> tag which is *not* printed to the output. Then, 
everything between the <jx:page/> and first SAX event can perform 
whatever setup operations needed.

Vadim


Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> Sylvain Wallez wrote:
>
>> Moreover, you may consider the case of complex pipelines where the 
>> actual template depends on the user rights, target browser or 
>> whatever else. In that case, the cache information can be different 
>> depending on used template.
>
>
>
> And for caching to play right with proxies and browsers, Vary header 
> (and/or some others) should be set. What mechanism can be used to set 
> headers from the JX template?


<jx:var name="foo" value="cocoon.request.setHeader('bar', 'baz')"/> ??

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Vadim Gritsenko <va...@reverycodes.com>.
Sylvain Wallez wrote:

> Moreover, you may consider the case of complex pipelines where the 
> actual template depends on the user rights, target browser or whatever 
> else. In that case, the cache information can be different depending 
> on used template.


And for caching to play right with proxies and browsers, Vary header 
(and/or some others) should be set. What mechanism can be used to set 
headers from the JX template?

Vadim


Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> Upayavira wrote:
>
>> Leszek Gawron wrote:
>>
>>> Sylvain Wallez wrote:
>>>
>>>>  <page jx:cache-key="news.id" 
>>>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>>>
>>>
>>>
>>> One question: if this NewsHelper is NOT an avalon component (or 
>>> object instantiated by flow.createObject) then it doesn't have acces 
>>> to any backend logic or data (for example database that stores 
>>> expiration time for different news services). Would it be possible 
>>> to instantiate this object via flow.createObject analogy?
>>
>>
>>
>> Of course. You could equally well say:
>>
>> sendPage("my-url", {"myObject", myAvalonObject});
>>
>> <page jx-cache-key="myObject.getValidity()">
>>
>> Regards, Upayavira
>>
> But that does not differ from what I proposed in first post and 
> Sylvain wrote it is a bad idea.


Please read my last post. There's a subtle but very important 
difference: the cache information is computed *by the template* even if 
the information needed for that is given in the view data.

And this distinction is important, as that's in the template that we 
precisely know what constitutes the cache validity. Moreover, you may 
consider the case of complex pipelines where the actual template depends 
on the user rights, target browser or whatever else. In that case, the 
cache information can be different depending on used template.

So, in simple words: cache information is a concern of pipeline 
components, data needed to compute it can come from anywhere, including 
flowscript view data.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Upayavira wrote:

> Leszek Gawron wrote:
> 
>> Sylvain Wallez wrote:
>>
>>>  <page jx:cache-key="news.id" 
>>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>>
>>
>> One question: if this NewsHelper is NOT an avalon component (or object 
>> instantiated by flow.createObject) then it doesn't have acces to any 
>> backend logic or data (for example database that stores expiration 
>> time for different news services). Would it be possible to instantiate 
>> this object via flow.createObject analogy?
> 
> 
> Of course. You could equally well say:
> 
> sendPage("my-url", {"myObject", myAvalonObject});
> 
> <page jx-cache-key="myObject.getValidity()">
> 
> Regards, Upayavira
> 
> 
> 
But that does not differ from what I proposed in first post and Sylvain wrote 
it is a bad idea.

-- 
Leszek Gawron

Re: JXTG and caching

Posted by Upayavira <uv...@upaya.co.uk>.
Leszek Gawron wrote:

> Sylvain Wallez wrote:
>
>>  <page jx:cache-key="news.id" 
>> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
>
> One question: if this NewsHelper is NOT an avalon component (or object 
> instantiated by flow.createObject) then it doesn't have acces to any 
> backend logic or data (for example database that stores expiration 
> time for different news services). Would it be possible to instantiate 
> this object via flow.createObject analogy?

Of course. You could equally well say:

sendPage("my-url", {"myObject", myAvalonObject});

<page jx-cache-key="myObject.getValidity()">

Regards, Upayavira




Re: JXTG and caching

Posted by Leszek Gawron <lg...@mobilebox.pl>.
Sylvain Wallez wrote:
>  <page jx:cache-key="news.id" 
> jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">
One question: if this NewsHelper is NOT an avalon component (or object 
instantiated by flow.createObject) then it doesn't have acces to any backend 
logic or data (for example database that stores expiration time for different 
news services). Would it be possible to instantiate this object via 
flow.createObject analogy?

-- 
Leszek Gawron

Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Bertrand Delacretaz wrote:

> Le 1 juin 04, à 10:09, Sylvain Wallez a écrit :
>
>> ...Well, it _could_ have a chance to work, but flowscript is 
>> definitely not the appropriate location to compute cache information 
>> for an element of the view pipeline....
>
>
> flowscript maybe not but how about backend java code?
>
> Use-case: for a news site, you might want the cache validity to be 
> dependent on the "urgency" of the news, for example cache older news 
> for a longer time, assuming they won't change much anymore.
>
> In this case the cache validity (or at least hints to the appropriate 
> validity range) needs to come from the backend.
>
>> ...So what about adding two optional attributes on the template's 
>> root element, defining the cache information?
>>
>> <page
>>    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>>    jx:cache-key='${cocoon.request.get("abc") + "!!spacer!!" + 
>> cocoon.request.get("bcd")....'
>>    jx:cache-validity='${new DeltaTimeCacheValidity(60 * 60 * 24)}'>
>>    ... template ....
>> </page>
>
>
> I like the idea, and IIUC it would also allow the Flow to receive the 
> DeltaTimeCacheValidity (for example) from your java code and pass it 
> down to JXTG, right?
>
>     jx:cache-validity='${someObjectComingFromJavaCodeViaFlow}'>
>
> Or, in other words, do you see this as having access to any additional 
> variables passed in the cocoon.sendPage call?


Sure. Since the expression is evaluated, you can access any information 
that is needed, and also some backend code.

Taking your example of news urgency, the corresponding algorithm would 
certainly not fit into a single attribute (because of the "if" 
constructs it's likely to contain), but could simply be written as :

  <page jx:cache-key="news.id" 
jx:cache-validity="com.my.NewsHelper.getValidity(news.urgency)">

Moreover, I find that being limited to a single attribute enforces good 
SoC by forbidding complicated code in the template.

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


Re: JXTG and caching

Posted by Bertrand Delacretaz <bd...@apache.org>.
Le 1 juin 04, à 10:09, Sylvain Wallez a écrit :

> ...Well, it _could_ have a chance to work, but flowscript is 
> definitely not the appropriate location to compute cache information 
> for an element of the view pipeline....

flowscript maybe not but how about backend java code?

Use-case: for a news site, you might want the cache validity to be 
dependent on the "urgency" of the news, for example cache older news 
for a longer time, assuming they won't change much anymore.

In this case the cache validity (or at least hints to the appropriate 
validity range) needs to come from the backend.

> ...So what about adding two optional attributes on the template's root 
> element, defining the cache information?
>
> <page
>    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
>    jx:cache-key='${cocoon.request.get("abc") + "!!spacer!!" + 
> cocoon.request.get("bcd")....'
>    jx:cache-validity='${new DeltaTimeCacheValidity(60 * 60 * 24)}'>
>    ... template ....
> </page>

I like the idea, and IIUC it would also allow the Flow to receive the 
DeltaTimeCacheValidity (for example) from your java code and pass it 
down to JXTG, right?

     jx:cache-validity='${someObjectComingFromJavaCodeViaFlow}'>

Or, in other words, do you see this as having access to any additional 
variables passed in the cocoon.sendPage call?

-Bertrand


Re: JXTG and caching

Posted by Sylvain Wallez <sy...@apache.org>.
Leszek Gawron wrote:

> While almost everybody buried XSP it has got one great feature that 
> JXTG  lacks: it's cacheable. I know that there is no way to implement 
> automatic JXTG caching but what if I know on what env does jx template 
> operate. If I know that it is for example only request param "abc" and 
> "bcd" and session attribute "xxx" then I know when I can use cached 
> entry. The question is: how?
>
> Should I extend JXTemplateGenerator so it is a cached component? I 
> understand it gets the validity methods to implement then. So every 
> time I call my extended component from flow I pass also the validity 
> calculated in flow. Something like this:
>
> function niceCachedResponse() {
>   var cacheKey = cocoon.request.get( "abc" ) + "!!spacer!!"
>                     + cocoon.request.get( "bcd" ) + "!!spacer!!"
>                     + cocoon.session.getAttribute( "xxx" );
>   var pageValidity = new DeltaTimeCacheValidity( 60 * 60 * 24 );
>                                                 // one day
>   cocoon.sendPage( "view/cached.jx", {
>                                      JXTGExtCacheKey: cacheKey,
>                                      JXTGExtpageValidity: pageValidity
>                                      someOtherBizData: data } );
> }
>
> Does it have a chance to work ?


Well, it _could_ have a chance to work, but flowscript is definitely not 
the appropriate location to compute cache information for an element of 
the view pipeline.

The problem with JXG is that the template is the generate() method 
_only_ whereas an XSP is the full class, allowing to define getKey() and 
getValidity().

So what about adding two optional attributes on the template's root 
element, defining the cache information?

<page
    xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
    jx:cache-key='${cocoon.request.get("abc") + "!!spacer!!" + 
cocoon.request.get("bcd")....'
    jx:cache-validity='${new DeltaTimeCacheValidity(60 * 60 * 24)}'>
    ... template ....
</page>

WDYT?

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }


AW: JXTG and caching

Posted by Marco Rolappe <m_...@web.de>.

> -----Ursprungliche Nachricht-----
> Von: dev-return-61667-m_rolappe=web.de@cocoon.apache.org
> [mailto:dev-return-61667-m_rolappe=web.de@cocoon.apache.org]Im Auftrag
> von Leszek Gawron
> Gesendet: Dienstag, 1. Juni 2004 01:05
> An: dev@cocoon.apache.org
> Betreff: JXTG and caching
>
>
> While almost everybody buried XSP it has got one great feature that JXTG
>   lacks: it's cacheable. I know that there is no way to implement
> automatic JXTG caching but what if I know on what env does jx template
> operate. If I know that it is for example only request param "abc" and
> "bcd" and session attribute "xxx" then I know when I can use cached
> entry. The question is: how?

the automatic caching could be done; collect all references to the template
context and make a ParameterValidity from their names and values.

> Should I extend JXTemplateGenerator so it is a cached component? I
> understand it gets the validity methods to implement then. So every time
> I call my extended component from flow I pass also the validity
> calculated in flow. Something like this:
>
> function niceCachedResponse() {
>    var cacheKey = cocoon.request.get( "abc" ) + "!!spacer!!"
>                      + cocoon.request.get( "bcd" ) + "!!spacer!!"
>                      + cocoon.session.getAttribute( "xxx" );
>    var pageValidity = new DeltaTimeCacheValidity( 60 * 60 * 24 );
>                                                  // one day
>    cocoon.sendPage( "view/cached.jx", {
>                                       JXTGExtCacheKey: cacheKey,
>                                       JXTGExtpageValidity: pageValidity
>                                       someOtherBizData: data } );
> }
>
> Does it have a chance to work ?

sure, I've already done something similar, but didn't yet implement the
above mentioned parameter stuff.

in the end there should be those two possibilities;
 1. set custom cacheability info for the bizdata/model/template context in
the flow/JXTG context
 2. have JXTG make a ParameterValidity from the references