SiddP February 2016

Is it possible to change flushInterval value in myBatis's own cache implementation dynamically

There are two scenarios where I want my flush interval to be 300000 (5 mins) and in some cases I dont want flush interval at all i.e. I want my cache to be constant throughout the session i.e. I want flush to be disabled and similarly if I release freeze it should again operate after every 5 mins.

                  <cache
                  eviction="FIFO"
                  flushInterval="300000"
                  size="512"
                  readOnly="true"/>


   <select id="getStoreIdAndEqId"  resultType="String" flushCache="false"  useCache="true">
           select count(author) from blog
        </select>

Technology I am using is MyBatis and Spring.

Answers


Florian Schaetz February 2016

So, I have debugged a little and unfortunately, the answer to your question is... "kind of"...

When I use your cache configuration, then I get the following cache structure (by Configuration.MappedStatement( ID ).Cache ):

A SynchronizedCache that delegates the caching to a LogingCache that delegates the caching to a ScheduledCache that delegates the caching to a FifoCache that delegates the caching to a PerpetualCache. Every one of these delegates does a little bit of work (logging, etc.) and then delegates the rest.

Unfortunately, there seems to be no "normal" way to get the delegate of the SynchronizedCache and that class offers no way to modify the settings of underlying Cache objects. Of course you could use reflection to get them, but that's not a good idea. So, I don't see way to do so with the default caches.

Which leads us to the "kind of" answer... You can write your own Cache implementation, for example MyCacheImpl, to do the work and set it via...

<cache type="com.example.MyCacheImpl"/>

Unfortunately, it will be wrapped in a LoggingCache (that delegates to it) and, you guessed right, LoggingCache also doesn't offer a way to get the delegate. So you will probably have to do some workaround there, for example by registering the Caches in a static HashMap or something like this. Of course, this can easily open up a whole can of bugs, so tread carefully. When you do so, you can access your Caches directly and call any behaviour altering methods you want.

Post Status

Asked in February 2016
Viewed 1,485 times
Voted 10
Answered 1 times

Search




Leave an answer