微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

redis的简单使用(java)

re@R_404_6422@的简单使用(java)

re@R_404_6422@官网: http://www.redis.cn/

一 re@R_404_6422@入门

01 导入坐标

<!--使用redistemplate-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-re@R_404_6422@</artifactId>
</dependency>

02 配置文件

spring:
application:
  name: demo-test-template
datasource:
  driver-class-name: com.MysqL.jdbc.Driver
  url: jdbc:MysqL://localhost:3306/ajava?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
  username: root
  password: root

re@R_404_6422@:
  database: 0
  host: 127.0.0.1
  port: 6379
  password:
  timeout: 6000ms # 连接超时时长(毫秒)
  je@R_404_6422@:
    pool:
      max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
      max-wait: -1ms     # 连接池最大阻塞等待时间(使用负值表示没有限制)
      max-idle: 10     # 连接池中的最大空闲连接
      min-idle: 5       # 连接池中的最小空闲连接

 

03 模板对象

模板对象序列化方式序列化效果
Redistemplate JdkSerializationRe@R_404_6422@Serializer  
StringRedistemplate StringRe@R_404_6422@Serializer  

API

//操作string类型
redistemplate.opsForValue();

//操作hash类型
redistemplate.opsForHash();

//操作list类型
redistemplate.opsForList();

//操作set类型
redistemplate.opsForSet();

//操作有序set类型
redistemplate.opsForZSet();

 

04 test类

@SpringBoottest
@RunWith(springrunner.class)
public class AppTest {
  @Autowired
  private StringRedistemplate stringRedistemplate;

  /**
    * re@R_404_6422@测试
    */
  @Test
  public void re@R_404_6422@Servertest(){
      stringRedistemplate.opsForValue().set("key12-15","123", Duration.ofSeconds(100L));
     
  }
}

思考一个问题:

  • 为什么加载了re@R_404_6422@的starter(起步依赖)之后,Redistemplate对象会自动被new出来,并可以@Autowired了呢?

SpringBoot工作原理: https://www.cnblogs.com/lyn8100/p/15837349.html

 

二 re@R_404_6422@失效key触发事件

01 re@R_404_6422@配置类

/**
* Re@R_404_6422@缓存配置类
*/
@Configuration
public class Re@R_404_6422@Configurer extends CachingConfigurerSupport {

  @Bean
  Re@R_404_6422@MessageListenerContainer container(Re@R_404_6422@ConnectionFactory connectionFactory) {

      Re@R_404_6422@MessageListenerContainer container = new Re@R_404_6422@MessageListenerContainer();
      container.setConnectionFactory(connectionFactory);
      return container;
  }

}

02 监听者类

/**
* re@R_404_6422@ 失效key的监听者类
* @Author lyn
*/
@Component
public class Re@R_404_6422@KeyExpirationListener extends KeyExpirationEventMessageListener {

  public Re@R_404_6422@KeyExpirationListener(Re@R_404_6422@MessageListenerContainer listenerContainer) {
      super(listenerContainer);
  }

  /**
    * 针对re@R_404_6422@数据失效事件,进行数据处理
    * @param message
    * @param pattern
    */
  @Override
  public void onMessage(Message message, byte[] pattern) {
      // message.toString()可以获取失效的key
      String expiredKey = message.toString();
      //后去解析key,调用其他服务执行任务
      System.out.println(expiredKey);
  }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐