Springboot中使用Redis

Redis 是一个基于内存的key-value的结构数据库
适合存储热点数据

Macos安装Redis

https://redis.io/docs/getting-started/installation/install-redis-on-mac-os/
安装redis

brew install redis

查看安装信息:

brew info redis

前台启动redis:

redis-server

后台启动redis:

brew services start redis

查看信息:

brew services info redis

停止:

brew services stop redis

配置

打开/opt/homebrew/etc/redis.conf配置文件
修改密码

requirepass 123456

登录redis

redis-cli -h localhost -p 6379 -a 123456

数据结构

字符串

SET key value #设置
GET key #获取
SETEX key seconds value #设置指定key过期时间,单位s
SETNX key vvalue 只有在key不存在时才设置

哈希

HSET key fiele value  将key设置为value
HGET key field 获取
HDEL key field 删除 
HKEYS key 获取所有字段
HVALS key 获取所有值

列表

LPUSH key value1[value2] 插入到头部 左侧
LRANGE key start stop 获取指定范围达到元素
RPOP key 移除并获取列表最后一个元素 右侧
LLEN key 获取列表长度

集合

SADD key number1 [number2] 插入一个成员
SMEMBERS key 返回集合中所有成员
SCARD key 获取成员数
SINTER key1[key2] 返回定义所哟集合的交集
SUNION key1 [key2] 返回所有集合的并集
SREM key number1[numer2] 删除集合中成员

有序集合

ZADD key score1 member1 [score2 member2] 向有序集合添加一个成员
ZRANGE key start stop [WITHSCORES] 通过索引区间返回有序集合指定区间内的成员
ZINCRBY key increment member 对指定成员分数加上增量increment
ZREM key member [member..]  删除成员

通用命令

KEYS pattern 查找所有符合给定模式的key
EXISTS key 检查给定key是否存在
TYPE key 返回key 所存储的值的类型
DEL key 在key存在时删除key

SpringBoot中使用Redis

导入依赖

<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

配置Redis

spring:
	redis:
    host: localhost
    port: 6379
    password: 123456
    database: 0

编写配置类

@Slf4j
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        // 设置redis连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置redis的String/Value的默认序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        log.info("RedisTemplate注入成功");
        return redisTemplate;
    }
}

使用Redis

@SpringBootTest
class RedisConfigurationTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testString() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("name", "cxk");
        String name = (String) valueOperations.get("name");
        System.out.println(name);
        valueOperations.set("code", "1234", 3, TimeUnit.HOURS); //3小时后过期
        valueOperations.setIfAbsent("code1", "1234"); //如果不存在则设置
    }

    @Test
    public void testHash() {
        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("user", "name", "cxk");
        hashOperations.put("user", "age", "18");
        String name = (String) hashOperations.get("user", "name");
        System.out.println(name);

        Set user = hashOperations.keys("user");
        System.out.println(user);

        List user1 = hashOperations.values("user");
        System.out.println(user1);

        hashOperations.delete("user", "name");
    }

    @Test
    public void testList() {
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("list", "a", "b", "c");
        listOperations.leftPush("list", "d");

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

        listOperations.rightPop("list");

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

    @Test
    public void testSet() {
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set", "a", "b", "c", "d", "e");
        Set set = setOperations.members("set");
        System.out.println(set);

        setOperations.remove("set", "a", "b");
        set = setOperations.members("set");
        System.out.println(set);


        setOperations.add("set2", "a", "b", "c", "d", "e");
        Set union = setOperations.union("set", "set2");
        Set intersect = setOperations.intersect("set", "set2");
        Set difference = setOperations.difference("set", "set2");
        System.out.println(union);
        System.out.println(intersect);
        System.out.println(difference);
    }
    
    @Test
    public void testZset(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset", "a", 1);
        zSetOperations.add("zset", "b", 2);
        zSetOperations.add("zset", "c", 3);

        Set zset = zSetOperations.range("zset", 0, -1);
        System.out.println(zset);
        
        zSetOperations.incrementScore("zset", "a", 1);
        zSetOperations.remove("zset", "a");
    }
    @Test
    public void testCommon(){
        Set keys = redisTemplate.keys("*");
        System.out.println(keys);

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

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

你可能感兴趣的:(JAVA,spring,boot,redis,后端)