苍穹外卖项目学习日记(7)

苍穹外卖项目学习日记(7) day05-1

Windows-Redis安装与运行

  • 直接解压day05下的zip压缩文件,在解压目录下进入cmd,运行redis-server.exe redis.windows.conf即可运行成功
  • 如果出现Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功完成。,表示端口被占用,需要结束占用端口的程序
  • 结束程序:进入管理员cmd,netstat -ano | findstr 6379,得到对应的PID后,就可以直接taskkill -PID 得到的pid -F,结束程序。也可以使用tasklist | findstr 得到的pid来查看是什么程序占用了端口
  • 运行redis-cli.exe即可进入redis的操作界面,还可以输入redis-cli.exe -h ip地址 -p 端口号可以远程访问其他主机
  • 增加密码:打开redis.windows.conf文件,搜索requirepass,后面跟的就是密码,修改成自己密码即可,并且取消注释,后续客户端需要连接,需要加上-a+密码参数,例如:redis-cli.exe -h ip地址 -p 端口号 -a 密码
  • 安装文件夹中另外一个软件,为redis的图形化界面

Redis常用命令

  • redis常用数据类型
    苍穹外卖项目学习日记(7)_第1张图片
    苍穹外卖项目学习日记(7)_第2张图片

字符串操作

苍穹外卖项目学习日记(7)_第3张图片

哈希操作

苍穹外卖项目学习日记(7)_第4张图片

列表操作

苍穹外卖项目学习日记(7)_第5张图片

集合操作

苍穹外卖项目学习日记(7)_第6张图片

有序集合

苍穹外卖项目学习日记(7)_第7张图片

常用命令

苍穹外卖项目学习日记(7)_第8张图片

JAVA中操作Redis

  • spring data redis使用
    苍穹外卖项目学习日记(7)_第9张图片

  • 导入坐标(项目中已经导入)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  • 配置数据源,放在spring下一级,即和datasource同级
  • application.yml
  redis:
    host: ${sky.redis.host}
    port: ${sky.redis.port}
    database: ${sky.redis.database}
    password: ${sky.redis.password}
  • application-dev.yml
  redis:
    host: localhost
    port: 6379
    password: 123456
    database: 0
  • 创建配置类RedisConfiguration
  • RedisConfiguration.java
package com.sky.config;

import lombok.extern.slf4j.Slf4j;
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.StringRedisSerializer;

@Configuration
@Slf4j
public class RedisConfiguration {

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        log.info("开始创建redis模板对象。。。。");
        RedisTemplate redisTemplate = new RedisTemplate();
        //设置redis连接工厂对象
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置redis key的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}

  • 创建测试类,操作redis数据
  • SpringDataRedisTest.java
package com.sky.test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class SpringDataRedisTest {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testRedisTemplate(){
        System.out.println(redisTemplate);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        HashOperations hashOperations = redisTemplate.opsForHash();
        ListOperations listOperations = redisTemplate.opsForList();
        SetOperations setOperations = redisTemplate.opsForSet();
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    }

    @Test
    public void testString(){
        //set get setex setnx
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("city","西宁");
        String city = (String) valueOperations.get("city");
        valueOperations.set("code","1234",3, TimeUnit.MINUTES);
        valueOperations.setIfAbsent("lock","1");
        valueOperations.setIfAbsent("lock","2");
        System.out.println(city);
    }

    @Test
    public void testHash(){
        //hset hget hdel hkeys hvals
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("100","name","tjx");
        hashOperations.put("100","age","23");
        String name = (String) hashOperations.get("100", "name");
        System.out.println(name);

        Set keys = hashOperations.keys("100");
        System.out.println(keys);
        List values = hashOperations.values("100");
        System.out.println(values);
        hashOperations.delete("100","age");
    }
    @Test
    public void testList(){
        //lpush lrange rpop llen
        ListOperations listOperations = redisTemplate.opsForList();

        listOperations.leftPushAll("mylist","a","b","c");
        listOperations.leftPush("mylist","d");

        List mylist = listOperations.range("mylist", 0, -1);
        System.out.println(mylist);

        listOperations.rightPop("mylist");

        Long size = listOperations.size("mylist");
        System.out.println(size);
    }

    @Test
    public void testSet(){
        //sadd smembers scard sinter sunion srem
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set1","a","b","c","d");
        setOperations.add("set2","a","b","x","y");

        Set set1 = setOperations.members("set1");
        System.out.println(set1);

        Long set11 = setOperations.size("set1");
        System.out.println(set11);

        Set intersect = setOperations.intersect("set1", "set2");
        System.out.println(intersect);

        Set union = setOperations.union("set1", "set2");
        System.out.println(union);

        setOperations.remove("set1","a","b");

    }

    @Test
    public void testZSet(){
        //zadd zrange zincrby zrem
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();

        zSetOperations.add("zset1","a",10);
        zSetOperations.add("zset1","b",12);
        zSetOperations.add("zset1","c",14);

        Set zset1 = zSetOperations.range("zset1", 0, -1);
        System.out.println(zset1);

        zSetOperations.incrementScore("zset1","c",10);

        zSetOperations.remove("zset1","a","b");
    }

    @Test
    public void testCommon(){
        //keys exists type del
        Set keys = redisTemplate.keys("*");
        System.out.println(keys);

        Boolean name = redisTemplate.hasKey("name");
        Boolean set1 = redisTemplate.hasKey("set1");

        for (Object key : keys) {
            DataType type = redisTemplate.type(key);
            System.out.println(type.name());
        }

        redisTemplate.delete("mylist");
    }
}

你可能感兴趣的:(苍穹外卖,spring,boot,java)