1 Reply Latest reply on Feb 13, 2008 4:35 AM by mircea.markus

    Can I ask a cache for its current size?

    nanreh

      I can see the total element count for my cache in a JMX console but I'm wondering if there's any way to ask an instance of Cache for that value without going through JMX.
      I've looked over the Cache API and I may be missing it.
      thnx...
      n

        • 1. Re: Can I ask a cache for its current size?
          mircea.markus

          The cleanest way would be to programmatically perform a call to JMX server to obtain this info. Another way is to work with the CacheMgmtInterceptor directly. This is the interceptors that supplies those statistics, you can access it through ((CacheSPI)cache).getInterceptorChain() - returns a List of Interceptor objects.
          Some notes on the method itself: on each call to the method, the cache will be iterated in order to count the nodes, which might be expensive. Also, no lock is acquired, so if an node is evicted or removed *during* iteration then this might not be reflected in the final result. If you want to avoid this, you can write your own counting mechanism (might not be such a bad idea, as it would keep your hands clean from working with cache internals/SPI).