牛客网项目18:Redis

1. Redis介绍

牛客网项目18:Redis_第1张图片

注: 下载地址:https://github.com/microsoftarchive/redis

2. Spring整合Redis

(1) 引入依赖

        
            org.springframework.boot
            spring-boot-starter-data-redis
        

(2) 配置

①配置数据库参数:

# Redis
spring.redis.database=11
spring.redis.host=localhost
spring.redis.port=6379

②编写配置类,构造RedisTemplate

在config目录下新建配置类:RedisConfig

package com.nowcoder.mycommunity.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //设置key的序列化方式
        template.setKeySerializer(RedisSerializer.string());
        //设置vaule的序列化方式
        template.setValueSerializer(RedisSerializer.json());
        //设置hash的key序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        //设置hsah的vaule的序列化方式
        template.setHashKeySerializer(RedisSerializer.json());
        template.afterPropertiesSet();
        return template;
    }
}

3. 测试

package com.nowcoder.mycommunity;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;

import java.util.concurrent.TimeUnit;

@SpringBootTest
public class RedisTest {
    @Autowired
    RedisTemplate redisTemplate;

    //String类型
    @Test
    public void test(){
        String redisKey="test:count";
        redisTemplate.opsForValue().set(redisKey,1);
        System.out.println(redisTemplate.opsForValue().get(redisKey));
        System.out.println(redisTemplate.opsForValue().increment(redisKey));
        System.out.println(redisTemplate.opsForValue().decrement(redisKey));
    }

    //Hash类型
    @Test
    public void test1(){
        String redisKey="test:user";
        redisTemplate.opsForHash().put(redisKey,"id",1);
        redisTemplate.opsForHash().put(redisKey,"username","zhangsan");
        System.out.println(redisTemplate.opsForHash().get(redisKey,"id"));
        System.out.println(redisTemplate.opsForHash().get(redisKey,"username"));
    }

    //列表类型
    @Test
    public void test2(){
        String redisKey = "test:ids";

        redisTemplate.opsForList().leftPush(redisKey, 101);
        redisTemplate.opsForList().leftPush(redisKey, 102);
        redisTemplate.opsForList().leftPush(redisKey, 103);
        System.out.println(redisTemplate.opsForList().size(redisKey));
        System.out.println(redisTemplate.opsForList().index(redisKey, 0));
        System.out.println(redisTemplate.opsForList().range(redisKey, 0 , 2));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
        System.out.println(redisTemplate.opsForList().leftPop(redisKey));
    }

    //集合类型(无序)
    @Test
    public void test3(){
        String redisKey = "test:teacher";
        redisTemplate.opsForSet().add(redisKey, "刘备", "关羽", "张飞", "赵云");
        System.out.println(redisTemplate.opsForSet().size(redisKey));
        System.out.println(redisTemplate.opsForSet().pop(redisKey)); //随机弹出一个值
        System.out.println(redisTemplate.opsForSet().members(redisKey)); //统计数量
    }

    //集合类型(有序)
    @Test
    public void test4(){
        String redisKey = "test:student";
        redisTemplate.opsForZSet().add(redisKey, "huo1", 80);
        redisTemplate.opsForZSet().add(redisKey, "huo2", 70);
        redisTemplate.opsForZSet().add(redisKey, "huo3", 60);
        redisTemplate.opsForZSet().add(redisKey, "huo4", 50);
        redisTemplate.opsForZSet().add(redisKey, "huo5", 40);
        System.out.println(redisTemplate.opsForZSet().zCard(redisKey)); //统计数量
        System.out.println(redisTemplate.opsForZSet().score(redisKey, "huo2"));
        System.out.println(redisTemplate.opsForZSet().rank(redisKey, "huo2")); //由小到大的排名
        System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey, "huo2")); //由大到小的排名
        System.out.println(redisTemplate.opsForZSet().range(redisKey, 0, 2));  //由小到大的前三名
    }

    //其他操作
    @Test
    public void test5(){
        redisTemplate.delete("test:user");  //删除
        System.out.println(redisTemplate.hasKey("test:user"));  //是否存在
        redisTemplate.expire("test:students", 10, TimeUnit.SECONDS); //设置过期时间
    }

    //多次访问同一个key
    @Test
    public void test6(){
        String redisKey = "test:count";
        BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);
        operations.increment();
        operations.increment();
        operations.increment();
        System.out.println(operations.get());
    }


    //redis管理事务是把所有的操作命令管理到队列中,事务中的命令,不会立即执行,在提交的时候把所有命令一起执行(批量执行)
    //    在事务的管理中,不能立即查询否则将会出现幻读
    //    spring中支持声明式事务,声明式事务只能精确到一个方法,编程式事务把事务的范围缩小
    @Test
    public void test7(){
       Object object= redisTemplate.execute(new SessionCallback() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                String key = "test:tx";
                operations.multi();  //启用事务
                operations.opsForSet().add(key, "zhangsan");
                operations.opsForSet().add(key, "lisi");
                operations.opsForSet().add(key, "wangwu");
                //查询不出结果,zairedis中做事务管理的时候不能进行查询操作
                System.out.println(operations.opsForSet().members(key));
                return operations.exec();  //提交事务
            }
       });
        System.out.println(object);
    }
}

你可能感兴趣的:(JAVA,redis,数据库,database,java,spring,boot)