<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.7.RELEASE</version> </dependency>
package com.config; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; /** * @author : ccf0537 * 1.在任一config类里使用@EnableCaching注解启用注解驱动的缓存 * 2.声明缓存管理器,spring内置了很多缓存管理器 * 3.在需要使用缓存的方法上添加@Cacheable和@CacheEvict * @date : 2020/11/25 10:14 */ @Configuration @EnableCaching public class CacheConfig { /** * 声明缓存管理器,spring提供了如下的缓存管理器,以CaheMange接口类型返回其中一个即可 * SimpleCacheManager * NoOpCacheManager * ConcurrentMapCacheManager * CompositeCacheManager * EhCacheCacheManager * RedisCacheManager * GemfireCacheManager * @return CacheManager spring缓存管理器接口 */ @Bean public CacheManager cacheManager(RedisTemplate redisTemplate){ System.out.println("declear CacheManager"); return new RedisCacheManager(redisTemplate); } /** * Redis连接工厂bean * @return */ @Bean public RedisConnectionFactory redisConnectionFactory(){ System.out.println("declear RedisConnectionFactory"); JedisConnectionFactory jcf = new JedisConnectionFactory(); jcf.setHostName("127.0.0.1"); jcf.setPort(6379); jcf.setPassword("123456"); return jcf; } /** * RedisTemplate bean * @return */ @Bean public RedisTemplate<String,String> redisTemplate(){ System.out.println("declear RedisTemplate"); RedisConnectionFactory rcf = redisConnectionFactory(); RedisTemplate<String,String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(rcf); return redisTemplate; } }
@Cacheable(value = "userNameCache") public String getUserName(String Id){ System.out.println("query user from db..."); return "polly"; }
原文:https://www.cnblogs.com/ccf0537/p/14042315.html