Search code examples
javaspringehcacheshiro

Java - Shiro - ehcache config


I am using Shiro(v1.2.3) in a java web project using Spring (v4.1.4).

I enabled Ehcache for shiro, to help store shiro session.


My question is:

Do I need to define a <cache> entity for shiro session in ehcache.xml config file, or shiro will manage that automatically?

In one word, do I need to put following config segment into ehcache-shiro.xml:

<cache name="shiro-activeSessionCache" maxElementsInMemory="10000" overflowToDisk="true" eternal="true"
    timeToLiveSeconds="0" timeToIdleSeconds="0" diskPersistent="true" diskExpiryThreadIntervalSeconds="600" />

Solution

  • No you don't need to if the default settings are fine for you. An ehcache.xml file is part of the shiro-ehcache module and would be used by default. The setting you are quoting is identical to the one defined in this file. As the Shiro doc about session management mentions:

    By default, the EhCacheManager uses a Shiro-specific ehcache.xml file that sets up the Session cache region and the necessary settings to ensure Sessions are stored and retrieved properly.

    However, if you wish to change the cache settings, or configure your own ehcache.xml or EHCache net.sf.ehcache.CacheManager instance, you will need to configure the cache region to ensure that Sessions are handled correctly.

    If you look at the default ehcache.xml file, you will see the following shiro-activeSessionCache cache configuration:

    <cache name="shiro-activeSessionCache"
            maxElementsInMemory="10000"
            overflowToDisk="true"
            eternal="true"
            timeToLiveSeconds="0"
            timeToIdleSeconds="0"
            diskPersistent="true"
            diskExpiryThreadIntervalSeconds="600"/> 
    

    If you wish to use your own ehcache.xml file, ensure that you have defined a similar cache entry for Shiro's needs