Redis

1、关系型与非关系型数据库对比

2、非关系型数据库优点及应用场景

分布式ID

3、BitMap 存二进制数据(0101010)、HyperLogLog(数据统计(少))

4、redis的命令参数

(1)String字符串命令

|--------|----------------------------------------------------|
| set | 设定值 SET key "value" |
| setnx | key不存在时设置key值 |
| setex | 为指定的 key 设置值及其过期时间,存在会替换值 SETEX mykey 60 redis 单位秒 |
| get | 获取指定的值 |
| incr | 自增1 incr hello:lht:page_view |
| decr | 自减1 decr hello:lht:page_view |
| INCRBY | INCRBY rank 20 增加20 |

(2)key键命令

|-----------------|----------|
| exists key | 判断键是否存在 |
| expire key time | 给键设置过期时间 |
| del key | 删除键 |
| ttl 键 | 查看键的有效时间 |
| | |

(3)List命令

关注,收藏,点赞,队列

|--------|----------------|
| lpush | 将一个或多个值插入到列表头部 |
| rpush | 在列表中添加一个或多个值 |
| lpop | 移出并获取列表的第一个元素 |
| rpop | 移除并获取列表最后一个元素 |
| llen | 获取列表长度 |
| lrange | 获取列表指定范围内的元素 |

(4)set命令

|--------|-----------------|
| sadd | 向集合添加一个或多个成员 |
| spop | 移除并返回集合中的一个随机元素 |
| scard | 获取集合的成员数 |
| sdiff | 返回给定所有集合的差集 |
| sunion | 返回所有给定集合的并集 |
| sinter | 返回给定所有集合的交集 |

(5)zset 有序不重复

应用:排行榜、热搜

|----------------|----------------------------------------------------------------|
| Zincrby 键 分数 值 | zincrby hot:search 1 '葫芦娃' |
| Zrevrange | 返回有序集中,指定区间内的成员(按分数值递减(从大到小)来排列) ZREVRANGE salary 0 -1 |
| | |

(6)Hash

5、redis原始语句

6、问题

​​​​​​​

7、java链接redis的客服端

8、日志输出

9、键通过RestTeplate操作,值通过ValueOperations及以下去操作

10、redis配置

(1)导包

java 复制代码
<!--        redis依赖包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

(2)配置序列化

java 复制代码
package com.smart.community.youth.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.core.ValueOperations;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    /**
     * 序列化配置
     * json序列化 jdk的序列化
     * @param connectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory connectionFactory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(connectionFactory);

        /**
         * 四个设置
         * 1 键的序列化
         * 2 值的序列
         */
        StringRedisSerializer keyRedisSerializer = new StringRedisSerializer();
        redisTemplate.setStringSerializer(keyRedisSerializer);
        redisTemplate.setHashKeySerializer(keyRedisSerializer);
        redisTemplate.setKeySerializer(keyRedisSerializer);

        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setHashKeySerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        return redisTemplate;


    }

    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

}

(3)配置yml配置文件

java 复制代码
server:
  port: 8089

spring:
  redis:
    host: 127.0.0.1
    database: 0
    port: 6379
    password: ""
    # 默认单位毫秒
    timeout: 5s
    # 连接池
    jedis:
      pool:
        #  最大连接数
        max-active: 10
        #  空闲连接数数
        max-idle: 8
        # 空闲最小链接数
        min-idle: 2
        max-wait: 3s
  profiles:
    active: "db,dev,swagger"

logging:
  level:
    "com.smart.community.youth": debug

(4)测试对象

java 复制代码
package com.smart.community.youth.vo;

import lombok.Data;

@Data
public class UserVo {
    private String username;
}

(5)测试

java 复制代码
package com.zw.data.redis.mapper;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@Slf4j
public class KeyRedisTemplateTest {
    @Resource
    RedisTemplate<String, Object> redisTemplate;

    @Test
    public void hasKey() {
        Boolean b = redisTemplate.hasKey("test:key:exists");
        Boolean delete = redisTemplate.delete("test:key:exists");
        redisTemplate.expire("test:key:exists", Duration.ofSeconds(1));
        redisTemplate.expire("test:key:exists", 1, TimeUnit.SECONDS);
        log.debug("" + b);
    }
}
java 复制代码
package com.zw.data.redis.mapper;

import com.zw.data.redis.vo.SysUserVo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.time.Duration;

@SpringBootTest
public class StringRedisTemplateTest {
    @Resource
    RedisTemplate<String, Object> redisTemplate;
    @Resource
    ValueOperations<String, Object> valueOperations;

    // 设置永不过期
    // 设置过期时间  nx
    // 存在不设置值
    // 不存在就设置值
    @Test
    public void setTest() {
        valueOperations.set("test:str:set", "永不过期");
        valueOperations.set("test:str:set:ex", "设置过期时间", Duration.ofMinutes(10));
        // 键不存在则设置值
        valueOperations.setIfAbsent("test:str:set:nx", "键不存在则设置值");
        // 修改操作
        valueOperations.setIfPresent("test:str:set:nx", "键不存在则设置值");

        SysUserVo sysUserVo = new SysUserVo();
        sysUserVo.setUsername("admin");
        valueOperations.set("test:str:set:obj", sysUserVo);
    }

    @Test
    public void incr() {
        Long num = valueOperations.increment("test:str:set:incr");
        Long num1 = valueOperations.decrement("test:str:set:incr");

    }


}

11、Duration 时间类使用

还有1d(一天) 2h(2小时)那些

12、缓存的原理

通过AOP去缓存里面取,取key 类名+方法名+参数+特殊区分符

13、配置了Bean注入之后

这边引入一下就直接用了

14、把RedisTemplate那些方法封装到DAO中去

相关推荐
2301_781668611 小时前
Redis 面试
java·redis·面试
吐泡泡_1 小时前
Redis(缓存)
redis
要一起看日出1 小时前
MVCC-多版本并发控制
数据库·mysql·mvcc
Hx__1 小时前
MySQL InnoDB 的 MVCC 机制
数据库·mysql
速易达网络1 小时前
ASP.NET MVC 连接 MySQL 数据库查询示例
数据库·asp.net·mvc
无名客01 小时前
redis分布式锁为什么采用Lua脚本实现。而不是事务
redis·分布式·lua·事务
玉衡子2 小时前
MySQL基础架构全面解析
数据库·后端
梦中的天之酒壶2 小时前
Redis Stack扩展功能
数据库·redis·bootstrap
GreatSQL2 小时前
GreatSQL分页查询优化案例实战
数据库
Leo.yuan2 小时前
不同数据仓库模型有什么不同?企业如何选择适合的数据仓库模型?
大数据·数据库·数据仓库·信息可视化·spark