Query JCR_SQL2
HI,
Within a Model associated with a Component a JCR_SQL2 Query is performed with the method resolver.findResources(query, Query.JCR_SQL2);
Is there a way to cache the result of the query and not repeat it every time?
Thank you
HI,
Within a Model associated with a Component a JCR_SQL2 Query is performed with the method resolver.findResources(query, Query.JCR_SQL2);
Is there a way to cache the result of the query and not repeat it every time?
Thank you
The provided solution is not good - the cached resource will contain the resource resolver that is used for accessing the related nodes, so it won't be closed after the query is finished. Next time when those resources are accessed, they will be returned using credentials from another request, so possible there might be a security issue. After the resolver is closed automatically (I don't know exactly - in several minutes, or hours, maybe after a day), the code will return error so exception will be thrown which will break the whole page where this code is executed. It will be a very vague bug that will crash your prod at unpredictable times and give you a huge headache.
This way you are able to store resources only in a model that will be re-created for the next web request, so it would work only if you use the same model in several places on a page and then purge them when the current SlingHttpServletRequest is destroyed.
However, the provided approach can be fixed if you adapt your resources to necessary sling models and cache those models, without storing a resource, the resource resolver, or other critical things in their fields. Also, you should use WeakHashMap / weak references to store such cached objects.
In almost all cases the default dispatcher cache + out-of-the-box sling models adapter factory cache are enough for good publisher performance. In other cases where it is not enough the provided solution (or the ehcache solution) is ok while you store adapted models, but you should take care of invalidating/updating the cache once your resources are updated (new pages are published, nodes' properties are updated as a result of a workflow execution, etc.).
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.