Macos下安装使用Redis

Redis 是一个基于内存的key-value的结构数据库

适合存储热点数据

Macos安装Redis

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

安装redis

java 复制代码
brew install redis

查看安装信息:

java 复制代码
brew info redis

前台启动redis:

java 复制代码
redis-server

后台启动redis:

java 复制代码
brew services start redis

查看信息:

java 复制代码
brew services info redis

停止:

java 复制代码
brew services stop redis

配置

打开/opt/homebrew/etc/redis.conf配置文件

修改密码

java 复制代码
requirepass 123456

登录redis

java 复制代码
redis-cli -h localhost -p 6379 -a 123456

数据结构

字符串

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

哈希

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

列表

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

集合

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

有序集合

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

通用命令

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

SpringBoot中使用Redis

导入依赖

xml 复制代码
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置Redis

yaml 复制代码
spring:
	redis:
    host: localhost
    port: 6379
    password: 123456
    database: 0

编写配置类

java 复制代码
@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

java 复制代码
@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");
    }
}
相关推荐
shengli7221 小时前
Python在金融科技(FinTech)中的应用
jvm·数据库·python
xcLeigh1 小时前
IoTDB Python原生接口全攻略:从基础读写到高级实战
开发语言·数据库·python·api·iotdb·原生接口·读写数据
xcLeigh1 小时前
Python操作国产金仓数据库(KingbaseES)全流程:从环境搭建到实战应用
开发语言·数据库·python·国产数据库·kingbasees·金仓数据库
人道领域2 小时前
Day | 11 【苍穹外卖统计业务的实现:含详细思路分析】
java·数据库·后端·苍穹外卖
YDS8298 小时前
黑马点评 —— 分布式锁详解加源码剖析
java·spring boot·redis·分布式
ZTLJQ8 小时前
数据的基石:Python中关系型数据库完全解析
开发语言·数据库·python
升鲜宝供应链及收银系统源代码服务9 小时前
《IntelliJ + Claude Code + Gemini + ChatGPT 实战配置手册升鲜宝》
java·前端·数据库·chatgpt·供应链系统·生鲜配送
跟着珅聪学java9 小时前
js编写中文转unicode 教程
前端·javascript·数据库
小江的记录本9 小时前
【Redis】Redis全方位知识体系(附《Redis常用命令速查表(完整版)》)
java·数据库·redis·后端·python·spring·缓存
还是做不到嘛\.10 小时前
Dvwa靶场-SQL Injection
数据库·sql·web安全