Configure ehcache in spring

Source: Internet
Author: User

1. introduce the required jar 2. configure ehcache and ehcache. xml content is as follows: [html] <ehcache xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: noNamespaceSchemaLocation = "ehcache. xsd "> <defaultCache maxElementsInMemory =" 10000 "eternal =" false "timeToIdleSeconds =" 120 "timeToLiveSeconds =" 120 "overflowToDisk =" false "placement =" 30 "maxElementsOnDisk =" 10000000" diskPersistent = "false" diskExpiryThreadIntervalSeconds = "120" MemoryStoreEvictionPolicy = "LRU"/> <cache name = "userCache" maxElementsInMemory = "1000" eternal = "false" timeToIdleSeconds = "100000" timeToLiveSeconds = "100000" overflowToDisk = "false "/> </ehcache> 3. configure spring management ehcache: [html] <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: p = "http://www.springframework.org/schema/p" xmlns: cache = "http: // Www.springframework.org/schema/cache "xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd "> <! -- Enable the cache annotation function. This is required; otherwise, the annotation will not take effect. In addition, the annotation must be declared to take effect only in the spring main configuration file. --> <cache: annotation-driven cache-manager = "cacheManager"/> <! -- CacheManager factory class, specifying ehcache. xml location --> <bean id = "cacheManagerFactory" class = "org. springframework. cache. ehcache. ehCacheManagerFactoryBean "p: configLocation =" classpath: ehcache. xml "/> <! -- Declare cacheManager --> <bean id = "cacheManager" class = "org. springframework. cache. ehcache. ehCacheCacheManager "p: cacheManager-ref =" cacheManagerFactory "/> </beans> 4. write the test code: [java] public class EhcacheSpringTest {private CacheManager cacheManager; private Cache cache; @ Before public void init () {ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); EhCacheCacheManager ehCacheManager = (EhCacheCacheManager) ctx. getBean ("cacheManager"); cacheManager = ehCacheManager. getCacheManager (); cache = cacheManager. getCache ("userCache") ;}@ After public void destory () {cacheManager. shutdown () ;}@ Test public void testEhcacheString () {String key = "hello"; String value = "world"; cache. put (new Element (key, value); Element element = cache. get (key); Object obj = element. getObjectValue (); Assert. assertEquals (value, obj); System. out. println (obj) ;}@ Test public void testEhcacheObj () {String key = "user"; User value = new User ("zhangsan", "lisi"); cache. put (new Element (key, value); Element element = cache. get (key); Object obj = element. getObjectValue (); Assert. assertEquals (value, obj); System. out. println (obj); Object objSer = element. getValue (); Assert. assertEquals (value, objSer); System. out. println (objSer );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.