SpringBoot+EhCache整合

对于Ehcache缓存来说,也是初次的和springboot集成一起使用,还有很多不足,请指出,

1.pom文件

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.5.3</version>
        </dependency>

2.对于配置Ehcache来说,读取ehcache.xml文件,之前用

file = ResourceUtils.getFile("classpath:ehcache.xml");来获取 打成jar总是有问题 ,获取不到文件名,传统美德!!!!!
package com.linkcheers.app.configuration;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
@EnableCaching
public class EhCacheConfig {
   /* *//**
     * ehcache 主要的管理器
     *//*
    @Bean
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
        return new EhCacheCacheManager (bean.getObject ());
    }*/

    /**
     * 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.
     */
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
        cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
        cacheManagerFactoryBean.setShared (true);
        return cacheManagerFactoryBean;
    }
}

3.进行cache工具类的设置,其中有一个方法getCache是获取Ehcache管理器,注意。

package com.linkcheers.app.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import java.util.Iterator;
import java.util.Set;

public class CacheUtils {

    private static EhCacheCacheManager ehCacheCacheManager;

    private static final String LOGIN_CACHE = "userSession";

    private static final String UTIL_CACHE = "utilsCache";


    /**
     * 写入用户信息LOGIN_CACHE缓存
     *
     * @param key
     * @return
     */
    public static void putUser(String key, Object value) {
        put(LOGIN_CACHE, key, value);
    }

    /**
     * 获取用户信息从LOGIN_CACHE缓存
     *
     * @param key
     * @return
     */
    public static Object getUserCache(String key) {
        return getCache(LOGIN_CACHE).get(key).getObjectValue();
    }

    /**
     * 写入用户信息UTIL_CACHE缓存
     *
     * @param key
     * @return
     */
    public static void putUtil(String key, Object value) {
        put(UTIL_CACHE, key, value);
    }

    /**
     * 获取用户信息从UTIL_CACHE缓存
     *
     * @param key
     * @return
     */
    public static Object getUtilCache(String key) {
        final Object objectValue = getCache(UTIL_CACHE).get(key).getObjectValue();
        return objectValue;
    }


    /**
     * 获得一个Cache 通用方法。
     * @param cacheName
     * @return
     */
    private static Cache getCache(String cacheName) {
        try {
            EhCacheCacheManager ehCacheCacheManager = ApplicationContextHelper.getBean(EhCacheCacheManager.class);
            final CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
            Cache cache = cacheManager.getCache(cacheName);
            if (cache == null) {
                throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
            }
            return cache;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * 写入缓存 通用方法
     *
     * @param cacheName
     * @param key
     * @param value
     */
    private static void put(String cacheName, String key, Object value) {
        Element element = new Element(key,value);
        Cache cache = getCache(cacheName);
        cache.put(element);
    }
    /**
     * 从缓存中移除 通用方法
     *
     * @param cacheName
     * @param key
     */
    public static void remove(String cacheName, String key) {
        getCache(cacheName).remove(key);
    }

    /**
     * 从缓存中移除所有
     *
     * @param cacheName
     */
    public static void removeAll(String cacheName) {
        final Cache cache = getCache(cacheName);
        cache.removeAll();
    }


}

4.工具类从spring的工厂中获取bean的工具类

package com.linkcheers.app.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHelper implements ApplicationContextAware {

    /**
     * 工厂bean工具类 获取想要的service的实例对象
     */
    private static ApplicationContext applicationContext;

    public ApplicationContextHelper() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHelper.applicationContext = applicationContext;
    }

    public static Object getBean(String beanName) {
        return applicationContext != null?applicationContext.getBean(beanName):null;
    }
    public static <T> T getBean(Class<T> clazz) {
        return applicationContext  != null?applicationContext.getBean(clazz):null;
    }
}

完结。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值