Redis缓存是真的真的很不错
<!--spring 集成redis所需的common-pool2-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
<!--将作为redis对象序列化器-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.53</version>
</dependency>
#配置redis数据库连接
redis:
host: 127.0.0.1
port: 6379
database: 0 # 设置数据库索引为0 默认为0
password: # 密码为空
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 2 # 连接池中的最小空闲连接
timeout: 2000 # 连接超时时间(毫秒)
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
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;
@Configuration
@EnableCaching
public class RedisConfiguration extends CachingConfigurerSupport {
//缓存管理器
@Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheManager cacheManager = RedisCacheManager.builder(factory).build();
return cacheManager;
}
//自定义缓存key生成策略
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object target, java.lang.reflect.Method method, Object... params) {
StringBuffer sb = new StringBuffer();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
System.out.println("调用Redis生成key:" + sb.toString());
return sb.toString();
}
};
}
}
@CacheConfig(cacheNames=“deparment”)//定义缓存名称
@Cacheable //开启缓存
在对象dto类加上 implements Serializable 不然会报java.io.NotSerializableException
最后
大功告成,之前要十分钟如今只要半秒,香