SpringBoot中使用ehcache

1.引入依赖



    net.sf.ehcache
    ehcache


    org.springframework.boot
    spring-boot-starter-cache

2.创建EhCache的配置文件ehcache.xml


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">

    
    <diskStore path="java.io.tmpdir" />

    
    <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="600"
                  timeToLiveSeconds="600" overflowToDisk="true" />

    <cache name="users" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
           timeToLiveSeconds="600" overflowToDisk="true" />

ehcache>

3.application.yml中,指明缓存类型并声明Ehcache配置文件的位置

	server:
	  port: 8899
	
	spring:
	  profiles:
	    active: dev
	  cache:
	    type: ehcache
	    ehcache:
	      config: classpath:/ehcache.xml

4.SpringBoot启动类,@EnableCaching开启Spring Cache缓存功能

@EnableCaching
@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
    
    @Bean  
	public EhCacheCacheManager ehCacheCacheManager(){
	   EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean (); 
	   cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
	   cacheManagerFactoryBean.setShared(true); 
	   return new EhCacheCacheManager(cacheManagerFactoryBean.getObject());
	}
}

5.使用缓存

5.1 使用注解的方式使用缓存
@Service
@Transactional     //事务,表示该类下所有的都受事务控制
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper usermapper;
    
    @Override
    @Cacheable(value="users",key="#id")
    public User selectUserById(int id) {
        User user=this.usermapper.selectUserById(id);
        return user;
    }
 
}
5.2 通过编程方式使用缓存(注解无效的情况下)

由于@Cacheable基于SpringAOP的动态代理机制,程序执行时会在代理的方法前做缓存处理,但是如果一个类(比如一个加了@Service注解的)内部方法A调用内部的@Cacheable方法B,则会直接调用方法不会进行相应的处理
解决办法:1.把A、B两个方法放进不同的Service中 2.不采用注解的方式,直接采用编程方式


@Service
@Transactional    
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper usermapper;
    
    @Override
    @Cacheable(value="users",key="#id")
    public User selectUserById(int id) {
        User user=selectUserById2(id);
        return user;
    }

	public User selectUserById2(int id) {
        User user=this.usermapper.selectUserById(id);
        return user;
    }
}

通过EhCacheUtil使用缓存

@Service
@Transactional    
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper usermapper;
    
    @Override
    public User selectUserById(int id) {
        User user=selectUserById2(id);
        return user;
    }

	public User selectUserById2(int id) {
	    if(EhCacheUtil.get("users",id) != null){
	    	User user = (User)EhCacheUtil.get("users",id);
	    	return user;
		}
        User user=this.usermapper.selectUserById(id);
        EhCacheUtil.put("users",id,user);
        return user;
    }
}
EhCacheUtil
/**
 * @ClassName:EhCacheUtil
 * @Description: 

Ehcache工具类

*/
public class EhCacheUtil { /** * @Description: 获取缓存 * @param cacheName 缓存名字 * @return Cache */ private static Cache getCache(String cacheName) { CacheManager cacheManager = CacheManager.getInstance(); if (null == cacheManager) { return null; } Cache cache = cacheManager.getCache(cacheName); if (null == cache) { return null; } return cache; } /** * @Description: 新增缓存记录 * @param cacheName 缓存名字 * @param key 缓存key */ public static void put(String cacheName, String key, Object value) { Cache cache = getCache(cacheName); if (null != cache) { Element element = new Element(key, value); cache.put(element); } } /** * @Description: 删除缓存记录 * @param cacheName 缓存名字 * @param key 缓存key * @return boolean 是否成功删除 */ public static boolean remove(String cacheName, String key) { Cache cache = getCache(cacheName); if (null == cache) { return false; } return cache.remove(key); } /** * @Description: 删除全部缓存记录 * @param cacheName 缓存名字 */ public static void removeAll(String cacheName) { Cache cache = getCache(cacheName); if (null != cache) { //logOnRemoveAllIfPinnedCache(); cache.removeAll(); } } /** * @Description: 获取缓存记录 * @param cacheName 缓存名字 * @param key 缓存key * @return Object 缓存记录数据Object */ public static Object get(String cacheName, String key) { Cache cache = getCache(cacheName); if (null == cache) { return null; } Element cacheElement = cache.get(key); if (null == cacheElement) { return null; } return cacheElement.getObjectValue(); } }

你可能感兴趣的:(SpringBoot,java)