zoukankan      html  css  js  c++  java
  • Spring boot集成redis初体验

    pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>redis-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>redis-demo</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.9.0</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.14</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

     Redis配置:

    debug=true
    # REDIS (RedisProperties)
    # Redis数据库索引(默认为0)
    spring.redis.database=0
    # Redis服务器地址
    spring.redis.host=192.168.2.138
    # Redis服务器连接端口
    spring.redis.port=6379
    # Redis服务器连接密码(默认为空)
    spring.redis.password=123456
    # 连接池最大连接数(使用负值表示没有限制)
    spring.redis.lettuce.pool.max-active=8
    # 连接池最大阻塞等待时间(使用负值表示没有限制)
    spring.redis.lettuce.pool.max-wait=-1
    # 连接池中的最大空闲连接
    spring.redis.lettuce.pool.max-idle=8
    # 连接池中的最小空闲连接
    spring.redis.lettuce.pool.min-idle=8
    # 连接超时时间(毫秒)
    spring.redis.timeout=1000

    User.java

    import lombok.Getter;
    import lombok.Setter;
    
    import java.io.Serializable;
    
    /**
     * Created by zenglw on 2018/3/11.
     */
    
    public class User implements Serializable {
    
        private static final long serialVersionUID = -1L;
    
        private @Getter @Setter String username;
        private @Getter @Setter Integer age;
    
        public User(String username, Integer age) {
            this.username = username;
            this.age = age;
        }
    }

    RedisConf.java

    @Configuration
    public class RedisConfig {
    
        @Bean(name = "userRedisTemplate")
        public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, User> template = new RedisTemplate<String, User>();
            template.setConnectionFactory(factory);
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new RedisObjectSerializer());
            return template;
        }
    }

     RedisDemoApplicationTests.java

    package com.example.demo;
    
    import com.example.demo.bean.User;
    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    import redis.clients.jedis.Jedis;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class RedisDemoApplicationTests {
    
        @Autowired
        private StringRedisTemplate stringRedisTemplate;
    
        @Autowired
        @Qualifier(value = "userRedisTemplate")
        private RedisTemplate<String, User> userRedisTemplate;
    
        @Test
        public void test() throws Exception {
            // 保存字符串
            stringRedisTemplate.opsForValue().set("aaa", "111");
            RedisConnectionFactory connectionFactory = stringRedisTemplate.getConnectionFactory();
    
            Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
        }
    
        @Test
        public void test1() throws Exception {
            Jedis jedis = new Jedis("192.168.2.138",6379);
            jedis.auth("123456");
            jedis.set("age", "1");
            System.out.println(jedis.get("age"));
        }
    
        @Test
        public void testUser() throws Exception {
    
            User user1 = new User("小红", 10);
            User user2 = new User("小白", 20);
            User user3 = new User("小李", 30);
    
            userRedisTemplate.opsForValue().set(user1.getUsername(), user1);
            userRedisTemplate.opsForValue().set(user2.getUsername(), user2);
            userRedisTemplate.opsForValue().set(user3.getUsername(), user3);
    
            Assert.assertEquals(10, userRedisTemplate.opsForValue().get("小红").getAge().longValue());
            Assert.assertEquals(20, userRedisTemplate.opsForValue().get("小白").getAge().longValue());
            Assert.assertEquals(30, userRedisTemplate.opsForValue().get("小李").getAge().longValue());
        }
    }

     github

  • 相关阅读:
    【Python3网络爬虫开发实战】 1-开发环境配置
    Elasticsearch 基本介绍及其与 Python 的对接实现
    深度学习 GPU环境 Ubuntu 16.04 + Nvidia GTX 1080 + Python 3.6 + CUDA 9.
    React组件方法中为什么要绑定this
    中级前端开发推荐书籍
    20万行代码,搞得定不?
    华为云数据库TaurusDB性能挑战赛,50万奖金等你来拿!
    00036_private
    使用spring等框架的web程序在Tomcat下的启动顺序及思路理清
    http304状态码缓存设置问题
  • 原文地址:https://www.cnblogs.com/jimboi/p/8546526.html
Copyright © 2011-2022 走看看