<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Add caching to REST Builder</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120484054" />
  <subtitle>Add caching to REST Builder</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120484054</id>
  <updated>2026-04-04T21:28:11Z</updated>
  <dc:date>2026-04-04T21:28:11Z</dc:date>
  <entry>
    <title>RE: Add caching to REST Builder</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120506215" />
    <author>
      <name>Javier De Arcos</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120506215</id>
    <updated>2020-12-16T09:09:12Z</updated>
    <published>2020-12-16T09:09:12Z</published>
    <summary type="html">&lt;p&gt;The ResourceImpl files are always created with the
  ServiceScope.PROTOTYPE scope, but these files will not be regenerated
  if they already exist, not as the files marked with @Generated annotation.&lt;/p&gt;
&lt;p&gt;So if you need another scope you can change it in the file and won't
  be modified in next generations&lt;/p&gt;</summary>
    <dc:creator>Javier De Arcos</dc:creator>
    <dc:date>2020-12-16T09:09:12Z</dc:date>
  </entry>
  <entry>
    <title>Add caching to REST Builder</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120484053" />
    <author>
      <name>Pietro Pagani</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120484053</id>
    <updated>2020-12-02T16:57:11Z</updated>
    <published>2020-12-02T14:47:14Z</published>
    <summary type="html">&lt;p&gt;Hi all,&lt;br /&gt;I have implemented a couple of endpoint with REST
  Builder which expose data fetched from an external
  datasource.&lt;br /&gt;Since the data don't change very often, I would like
  to enable caching, so I injected a singleVMPool within my component&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;@Reference
private SingleVMPool _singleVMPool;
private static PortalCache&amp;lt;String, List&amp;lt;Building&amp;gt;&amp;gt; _portalCache;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and then in its initialization method (marked with @Activate) I added
  something like:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;_portalCache = (PortalCache&amp;lt;String, List&amp;lt;MyObject&amp;gt;&amp;gt;) _singleVMPool.getPortalCache(&amp;quot;MyCache&amp;quot;);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After that I properly configured cache in XML&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;ehcache dynamicConfig=&amp;quot;true&amp;quot; monitoring=&amp;quot;off&amp;quot;
	name=&amp;quot;module-single-vm&amp;quot; updateCheck=&amp;quot;false&amp;quot;
	xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;
	xsi:noNamespaceSchemaLocation=&amp;quot;http://www.ehcache.org/ehcache.xsd&amp;quot;&amp;gt;
	&amp;lt;cache eternal=&amp;quot;false&amp;quot; maxElementsInMemory=&amp;quot;10000&amp;quot;
		name=&amp;quot;MyCache&amp;quot; overflowToDisk=&amp;quot;false&amp;quot;
		timeToIdleSeconds=&amp;quot;600&amp;quot;&amp;gt;
	&amp;lt;/cache&amp;gt;
&amp;lt;/ehcache&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem is that the component has been created automatically by
  REST Builder and is marked as &amp;quot;scope =
  ServiceScope.PROTOTYPE&amp;quot;&lt;br /&gt;This means that at each requests, a
  new instance of the component will be created and hence a new cache
  will be initialized: what happens at the end is that my cache is
  always empty.&lt;/p&gt;
&lt;p&gt;Is it possible to avoid marking REST Builder component as scope =
  ServiceScope.PROTOTYPE? Or shall I configure cache in a different way?&lt;/p&gt;</summary>
    <dc:creator>Pietro Pagani</dc:creator>
    <dc:date>2020-12-02T14:47:14Z</dc:date>
  </entry>
</feed>
