[关闭]
@TedZhou 2020-06-28T06:01:04.000000Z 字数 2528 阅读 139

Springboot with redis

spring boot redis


Springboot集成redis可以实现三个目标:

  1. 缓存
  2. 集中存储session
  3. Pub/Sub消息队列

pom和properties配置

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.session</groupId>
  7. <artifactId>spring-session-data-redis</artifactId>
  8. </dependency>
  1. #spring.data.redis.repositories.enabled=false
  2. spring.cache.type=redis
  3. # 用redis存储session
  4. spring.session.store-type=redis
  5. spring.session.redis.namespace=myproject:session
  6. spring.redis.host=localhost
  7. spring.redis.port=6379
  8. spring.redis.password=

启动类加@EnableCaching 启用缓存

  1. @SpringBootApplication
  2. @EnableCaching
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

使用缓存

在类和方法分别用@CacheConfig和@Cacheable、@CachePut、@CacheEvict等注解即可。

  1. @Service
  2. @CacheConfig(cacheNames = "myprojectcache:somentity")
  3. public class SomentityService {
  4. @Cacheable(key="#id")
  5. public Somentity findById(Integer id) {
  6. //TODO:
  7. }
  8. }

如果注解方式不能满足要求,可以注入@Resource cacheManager调用其方法处理缓存。

  1. @Resource private CacheManager cacheManager;
  2. //获取cache
  3. cacheManager.getCache("myprojectcache:somentity").get/put/evict(...);

声明消息队列所需的@Bean messageListenerContainer

  1. @Configuration
  2. public class RedisConfig {
  3. @Bean
  4. public RedisMessageListenerContainer messageListenerContainer(RedisConnectionFactory connectionFactory) {
  5. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  6. container.setConnectionFactory(connectionFactory);
  7. return container;
  8. }
  9. }

定义MessageHandler

  1. @Component
  2. public interface RedisMessageHandler {
  3. public void handleMessage(String message);
  4. }
  5. @Slf4j
  6. public class MyMessageHandler implements RedisMessageHandler {
  7. @Override
  8. public void handleMessage(String text) {
  9. log.debug(text);
  10. }
  11. }

在合适的类里注入messageListenerContainer,实现添加消息监听频道的方法,以及发送消息的方法

  1. @Autowired RedisMessageListenerContainer messageListenerContainer;
  2. @Synchronized public void addMessageListener(String channel) {
  3. if (!exists(channel) {
  4. MessageListenerAdapter listener = new MessageListenerAdapter(new MyMessageHandler(channel));
  5. listener.afterPropertiesSet();
  6. messageListenerContainer.addMessageListener(listener, new ChannelTopic(channel));
  7. }
  8. }
  9. @Autowired private StringRedisTemplate stringRedisTemplate;
  10. public void sendMsg(String channel, String message){
  11. stringRedisTemplate.convertAndSend(channel, message);
  12. }

StringRedisTemplate的几个常见用法

  1. ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
  2. Long value = ops.increment(key) : ops.decrement(key);
  3. Set<String> keys = stringRedisTemplate.keys(keyPrefix);
  4. List<String> values = ops.multiGet(keys);
  5. stringRedisTemplate.delete(key);
  6. stringRedisTemplate.remame(key, newkey);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注