SpringBoot Filling Pit | Shiro and Redis Multilevel Cache Problem

  Cache, java, redis, shiro, springboot

WeChat Public Number: An Outstanding Disabled Person. If you have any questions, please leave a message backstage. I won’t listen anyway.

一个优秀的废人

Preface

Contributions from a younger brother who did not want to be named. This article mainly talks about the pits encountered by multiple caches and the solutions.

Find a problem

In a project practice, multi-level Cache was implemented, including Shiro’s cache. I thought it was a very simple thing to open redis cache. I only need to add @EnableCaching annotation to the startup class to start cache management, but the problem appeared.

Screenshot of Important Error Log

java.lang.IllegalStateException: @Bean method ShiroConfig.cacheManager called as a bean reference for type [org.apache.shiro.cache.ehcache.EhCacheManager] but overridden by non-compatible bean instance of type [org.springframework.data.redis.cache.RedisCacheManager]. Overriding bean of same name declared in: class path resource [org/springframework/boot/autoconfigure/cache/RedisCacheConfiguration.class]

Error log analysis

Looking at the log, we probably found an illegal status exception. We will continue to look at the following log. There is a very important log, Overriding bean of same name, which translates to help you rewrite a bean with the same name. I will look at the log again and see that RediscacheManager and CacheManager implemented by myself are mentioned here. I have already felt the problem. The following figure is a partial implementation code of RediscoacheManager. Figure 2 shows my own implementation of Shiro’s cacheManager.

RedisCacheManager 部分源码

RedisCacheManager 部分源码

Solve the problem

Everyone with Spring foundation should remember that Spring does not allow the same Bean to appear. Now the problem is that Redis cache manager and Shiro’s cache manager are renamed, and both are managed by Spring, so Spring conflicts when reading the two. The solution to the problem is very simple: when implementing EhCacheManager yourself, you can specify @Bean with a name like this @Bean(name =”ehCacheManager “), and there are other ways you can think of to implement it.

Conclusion

Although we all know that Spring reports a lot of errors, it is very useful to find out the problem in Spring’s error reporting log. The log will give you feedback for most errors.

If this article is of any help to you, please help me look good. Your good looks are my motivation to persist in writing.

In addition, after the attention is sent1024Free study materials are available.

For details, please refer to this old article:Python, C++, Java, Linux, Go, Front End, Algorithm Data Sharing

一个优秀的废人,给你讲几斤技术