How do I limit a cache size by bytes instead of by the entry count?

For a partitioned cache, use the attached cache extension which measures its units in bytes. Configure the high-units to be in bytes, as shown below. Be sure to specify the class of the cache extension, too.

<local-scheme>
  <scheme-name>byte-limited</scheme-name>
  <class-name>com.tangosol.examples.coherence.MemoryLimitedCache</class-name>
  <!-- this is the max bytes of memory: 1MB -->
  <high-units>1000000</high-units>
</local-scheme>

Since the MemoryLimitedCache implementation only supports Binary values, it can be easily used as the backing map for a partitioned cache – but not as an object cache (e.g. within the front side of a Near Cache):

<near-scheme>
  <scheme-name>near-limited</scheme-name>
 
  <front-scheme>
    <local-scheme>
      <!-- this is the max  count of objects to keep locally on the  "client" side -->
      <high-units>1000</high-units>
    </local-scheme>
  </front-scheme>
 
  <back-scheme>
    <distributed-scheme>
      <service-name>dist-limited</servic e-name>
 
      <backing-map-scheme>
        <local-scheme>
          <class-name>c om.tangosol.examples.coherence.MemoryLimitedCache</class-name>
          <!-- this&n bsp;is the max bytes of memory to use pe r JVM on the "server" side -->
          <high-units>1 000000</high-units>
        </local-scheme>
      </backing-map-scheme>
    </distributed-scheme>
  </back-scheme>
 
  <autostart>true</autostart>
</near-scheme>

There is a <unit-calculator> element in the <local scheme> element. Adding <unit-calculator>BINARY</unit-calculator> will cause the low-units/high-units elements to control maximum byte count rather than object count.


Attachments:
MemoryLimitedCache.java (text/plain)