redis学习

 jedis

1 引入依赖

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

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jredis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jredis</name>
    <description>JRedis - A Spring Boot Redis Example</description>
    <url>https://example.com/jredis</url>
    <licenses>
        <license>
            <name>The Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <id>developer_id</id>
            <name>Developer Name</name>
            <email>developer@example.com</email>
        </developer>
    </developers>
    <scm>
        <connection>scm:git:git://example.com/jredis.git</connection>
        <developerConnection>scm:git:ssh://example.com/jredis.git</developerConnection>
        <tag>HEAD</tag>
        <url>https://example.com/jredis</url>
    </scm>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.7.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

ency>

2建立链接

3测试

4资源释放

package com.example.jredis;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;

import java.util.Map;

@SpringBootTest
class JredisApplicationTests {
    Jedis jedis;

    @Test
    void contextLoads() {
    }

    @BeforeEach
    void setUp() {
        jedis = new Jedis("127.0.0.1", 6379);
        // jedis.auth("123456"); // 设置密码
        jedis.select(0);
    }

    @Test
    void test() {
        jedis.set("name", "张三"); // 设置值
        System.out.println(jedis.get("name"));
        jedis.hset("user1", "name", "张三");
        jedis.hset("user1", "age", "18");
        Map<String, String> user1 = jedis.hgetAll("user1");
        System.out.println(user1);
    }

    @AfterEach
    void tearDown() {
        if (jedis != null) {
            jedis.close();
        }
    }
}

 配置 JedisConnectFactory

 
package com.example.jredis.util;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisConnectFactory {
    private  static  final JedisPool jedisPool;
    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMaxTotal(8);
        jedisPoolConfig.setMaxWaitMillis(1000);
        jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379,10000);
    }
    public static JedisPool getJedisPool(){
        return jedisPool;
    }
}

package com.example.jredis;

import com.example.jredis.util.JedisConnectFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import redis.clients.jedis.Jedis;

import java.util.Map;

@SpringBootTest
class JredisApplicationTests {
    Jedis jedis;

    @Test
    void contextLoads() {
    }

    @BeforeEach
    void setUp() {
//        jedis = new Jedis("127.0.0.1", 6379);
        jedis = JedisConnectFactory.getJedisPool().getResource();
        // jedis.auth("123456"); // 设置密码
        jedis.select(0);
    }

    @Test
    void test() {
        jedis.set("name", "张三"); // 设置值
        System.out.println(jedis.get("name"));
        jedis.hset("user1", "name", "张三");
        jedis.hset("user1", "age", "18");
        Map<String, String> user1 = jedis.hgetAll("user1");
        System.out.println(user1);
    }

    @AfterEach
    void tearDown() {
        if (jedis != null) {
            jedis.close();
        }
    }
}

 springDateRedis

快速入门

引入依赖

       <!--redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--连接池依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

 配置文件

 

 编写配置文件

 

注入

测试

 

 

 无参构造注解
有参构造注解
 

 

 

 序列化与反序列化

 

 

 

 redis 实战篇

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值