• redis学习06使用Spring Data调用Redis


    1、建立spring boot项目,添加依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    2、配置文件:

    spring:
      redis:
        host: ubu
        port: 6379
        password: 123456
        #Redis数据库索引(默认为0)
        database: 0
        #连接超时时间(毫秒)
        connect-timeout: 1800000
        #客户端底层连接方式:lettuce(默认)、jedis二选一
        client-type: lettuce
        lettuce:
          pool:
            #连接池最大连接数(使用负值表示没有限制)
            max-active: 20
            #最大阻塞等待时间(负数表示没限制)
            max-wait: -1
            #连接池中的最大空闲连接
            max-idle: 5
            #连接池中的最小空闲连接
            min-idle: 0

    3、将Lettuce切换为Jedis

    在pom文件中添加jedis引用

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
    </dependency>

    在配置文件中设置:

    client-type: jedis

    4、建立测试类:

     1 package com.example.demo02;
     2 
     3 import org.junit.jupiter.api.Test;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.boot.test.context.SpringBootTest;
     6 import org.springframework.data.redis.connection.RedisConnectionFactory;
     7 import org.springframework.data.redis.core.*;
     8 import redis.clients.jedis.BitOP;
     9 
    10 @SpringBootTest
    11 public class RedisTest {
    12 
    13     @Autowired
    14     StringRedisTemplate redisTemplate;
    15 
    16 
    17 
    18     //读写String
    19     @Test
    20     void testRedis01(){
    21         //HashOperations<String,String,String>:操作hash类型
    22         //ListOperations<String,String>:操作list类型
    23         //SetOperations<String,String>:操作set类型
    24         //ZSetOperations<String,String>:操作zset类型
    25 
    26         //HyperLogLogOperations<String,String>
    27         //GeoOperations
    28 
    29         ValueOperations<String, String> operations = redisTemplate.opsForValue();
    30         operations.set("hello","world");
    31         final String hello = operations.get("hello");
    32         System.out.println(hello);
    33     }
    34 
    35     @Autowired
    36     RedisConnectionFactory factory;
    37 
    38     @Test
    39     void testFactory(){
    40         System.out.println(factory.getClass());
    41     }
    42 }
  • 相关阅读:
    使用60赫兹交流电的关西人
    UI交互细节节选控件使用细则
    PNG button (按钮) files with transparency, for Visual C++ 6.0 and VS2005
    成都第二天:美食
    精力管理与状态转换
    再议“专注”
    成都第一天:印象
    Flash AMF协议
    AS3.0 JSON介绍
    AS3.0 利用AMFPHP与PHP进行通讯 .
  • 原文地址:https://www.cnblogs.com/asenyang/p/15516071.html
Copyright © 2020-2023  润新知