zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Unity 保存游戏效果图片,并显示;
    Unity OnTriggerEnter问题
    Unity NGUI 批量点击跳转场景
    Unity调用手机摄像头进行摄像,并显示
    Unity3d NGUI 动态显示字体

    IDE的使用
    【树形Dp】【JSOI2008】【BZOJ1017魔兽地图DotR】
    【数学题】【Codeforces 164 Div2 E】【Playlist】
    【数学期望】【2012 ACM/ICPC 成都赛区现场赛】【B.Candy】
  • 原文地址:https://www.cnblogs.com/asenyang/p/15516071.html
Copyright © 2011-2022 走看看