SpringBoot+EhCache整合

﹏ヽ暗。殇╰゛Y 2022-12-15 03:47 278阅读 0赞

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

1.pom文件

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-cache</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.apache.shiro</groupId>
  7. <artifactId>shiro-ehcache</artifactId>
  8. <version>1.5.3</version>
  9. </dependency>

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

  1. file = ResourceUtils.getFile("classpath:ehcache.xml");来获取 打成jar总是有问题 ,获取不到文件名,传统美德!!!!!
  2. package com.linkcheers.app.configuration;
  3. import org.springframework.cache.annotation.EnableCaching;
  4. import org.springframework.cache.ehcache.EhCacheCacheManager;
  5. import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.core.io.ClassPathResource;
  9. @Configuration
  10. @EnableCaching
  11. public class EhCacheConfig {
  12. /* *//**
  13. * ehcache 主要的管理器
  14. *//*
  15. @Bean
  16. public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
  17. return new EhCacheCacheManager (bean.getObject ());
  18. }*/
  19. /**
  20. * 据shared与否的设置,Spring分别通过CacheManager.create()或new CacheManager()方式来创建一个ehcache基地.
  21. */
  22. @Bean
  23. public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
  24. EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
  25. cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
  26. cacheManagerFactoryBean.setShared (true);
  27. return cacheManagerFactoryBean;
  28. }
  29. }

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

  1. package com.linkcheers.app.utils;
  2. import net.sf.ehcache.Cache;
  3. import net.sf.ehcache.CacheManager;
  4. import net.sf.ehcache.Element;
  5. import org.springframework.cache.ehcache.EhCacheCacheManager;
  6. import java.util.Iterator;
  7. import java.util.Set;
  8. public class CacheUtils {
  9. private static EhCacheCacheManager ehCacheCacheManager;
  10. private static final String LOGIN_CACHE = "userSession";
  11. private static final String UTIL_CACHE = "utilsCache";
  12. /**
  13. * 写入用户信息LOGIN_CACHE缓存
  14. *
  15. * @param key
  16. * @return
  17. */
  18. public static void putUser(String key, Object value) {
  19. put(LOGIN_CACHE, key, value);
  20. }
  21. /**
  22. * 获取用户信息从LOGIN_CACHE缓存
  23. *
  24. * @param key
  25. * @return
  26. */
  27. public static Object getUserCache(String key) {
  28. return getCache(LOGIN_CACHE).get(key).getObjectValue();
  29. }
  30. /**
  31. * 写入用户信息UTIL_CACHE缓存
  32. *
  33. * @param key
  34. * @return
  35. */
  36. public static void putUtil(String key, Object value) {
  37. put(UTIL_CACHE, key, value);
  38. }
  39. /**
  40. * 获取用户信息从UTIL_CACHE缓存
  41. *
  42. * @param key
  43. * @return
  44. */
  45. public static Object getUtilCache(String key) {
  46. final Object objectValue = getCache(UTIL_CACHE).get(key).getObjectValue();
  47. return objectValue;
  48. }
  49. /**
  50. * 获得一个Cache 通用方法。
  51. * @param cacheName
  52. * @return
  53. */
  54. private static Cache getCache(String cacheName) {
  55. try {
  56. EhCacheCacheManager ehCacheCacheManager = ApplicationContextHelper.getBean(EhCacheCacheManager.class);
  57. final CacheManager cacheManager = ehCacheCacheManager.getCacheManager();
  58. Cache cache = cacheManager.getCache(cacheName);
  59. if (cache == null) {
  60. throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
  61. }
  62. return cache;
  63. } catch (Exception ex) {
  64. ex.printStackTrace();
  65. }
  66. return null;
  67. }
  68. /**
  69. * 写入缓存 通用方法
  70. *
  71. * @param cacheName
  72. * @param key
  73. * @param value
  74. */
  75. private static void put(String cacheName, String key, Object value) {
  76. Element element = new Element(key,value);
  77. Cache cache = getCache(cacheName);
  78. cache.put(element);
  79. }
  80. /**
  81. * 从缓存中移除 通用方法
  82. *
  83. * @param cacheName
  84. * @param key
  85. */
  86. public static void remove(String cacheName, String key) {
  87. getCache(cacheName).remove(key);
  88. }
  89. /**
  90. * 从缓存中移除所有
  91. *
  92. * @param cacheName
  93. */
  94. public static void removeAll(String cacheName) {
  95. final Cache cache = getCache(cacheName);
  96. cache.removeAll();
  97. }
  98. }

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

  1. package com.linkcheers.app.utils;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.ApplicationContextAware;
  5. import org.springframework.stereotype.Component;
  6. @Component
  7. public class ApplicationContextHelper implements ApplicationContextAware {
  8. /**
  9. * 工厂bean工具类 获取想要的service的实例对象
  10. */
  11. private static ApplicationContext applicationContext;
  12. public ApplicationContextHelper() {
  13. }
  14. @Override
  15. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  16. ApplicationContextHelper.applicationContext = applicationContext;
  17. }
  18. public static Object getBean(String beanName) {
  19. return applicationContext != null?applicationContext.getBean(beanName):null;
  20. }
  21. public static <T> T getBean(Class<T> clazz) {
  22. return applicationContext != null?applicationContext.getBean(clazz):null;
  23. }
  24. }

完结。

发表评论

表情:
评论列表 (有 0 条评论,278人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ssm整合

    在mybatis和spring整合后 , 在把springmvc整合进来 在maven里创建web工程 然后进行mybatis和spring的整合步骤(写在其他博客里)