侧边栏壁纸
博主头像
CoderKui

坐中静,舍中得,事上练

  • 累计撰写 51 篇文章
  • 累计创建 69 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Redis快速入门

CoderKui
2022-10-09 / 0 评论 / 0 点赞 / 309 阅读 / 1,443 字 / 正在检测是否收录...

数据类型

Redis存储的是key-value结构的数据,其中key是宇符串类型,value有5种常用的数据类型:

  • 字符串 string
  • 哈希 hash
  • 列表 list
  • 集合 set
  • 有序集合 sorted set
截屏2022-10-05 11.45.31

常用命令

字符串string操作命令

  • SET key value 设置指定key的值
  • GET key 获取指定key的值
  • SETEX key seconds value 设置指定key的值,并将 key 的过期时间设为 seconds 秒
  • SETNX key value 只有在 key 不存在时设置 key 的值

哈希hash操作命令

hash是一个string类型的field 和value 的映射表,hash特别适合用于存储对象,常用命令:

  • HSET key field value 将哈希表 key 中的字段field 的值设为 value
  • HGET key field 获取存储在哈希表中指定字段的值
  • HDEL key field 删除存储在哈希表中的指定字段
  • KEYS key 获取哈希表中所有字段
  • VALS key 荻取哈希表中所有值
  • HGETALL key 获取在哈希表中指定 key 的所有字段和值
截屏2022-10-06 08.28.44

列表list操作命令

Redis 列表是简单的字符串列表,按照插入顺序排序,常用命令:

  • LPUSH key value1 [value2] 将一个或多个值插入到列表头部
  • LRANGE key start stop 获取列表指定范围内的元素
  • RPOP key 移除并获取列表最后一个元素
  • LLEN kev 获取列表长度
  • BRPOP key1 [key2 ] timeout 移出并获取列表的最后一个元素,如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止

集合set操作命令

Redis set 是string类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据,常用命令:

  • SADD key member1 [member2] 向集合添加一个或多个成员
  • MEMBERS key 返回集合中的所有成员
  • SCARD key 荻取集合的成员数
  • SINTER key1 [key2] 返回给定所有集合的交集
  • UNION key1 [key2] 返回所有给定集合的并集
  • DIFF key1 [key2] 返回给定所有集合的差集
  • REM key member1 [member2] 移除集合中一个或多个成员

有序集合sorted set操作命令

Redis sorted set 有序集合是string 类型元素的集合,且不允许重复的成员。每个元素都会关联一个double类型的分数(score)。redis正是通过分数来为集合中的成员进行从小到大排序。有序集合的成员是唯一的,但分数却可以重复。
常用命令:

  • ZADD key score1 member1 [score2 member2] 向有序集合添加一个或多个成员,或者更新已存在成员的分数
  • RANGE key start stop [WITHSCORES] 通过索引区间返回有序集合中指定区间内的成员
  • ZINCRBY key increment member 有序集合中对指定成员的分数加上增量 increment
  • REM key member [member…] 移除有序集合中的一个或多个成员

通用命令

  • KEYS pattern 查找所有符合给定模式( pattern)的key
  • EXISTS key 检查给定key 是否存在
  • TYPE key 返回 kev 所储存的值的类型
  • TTL key 返回给定 key 的剩余生存时间(TTL,time to live),以秒为单位
  • DEl key 该命令用于在 key 存在是删除 key

在Java中操作Redis

Redis 的Java 客户端很多,官方推荐的有三种:

  • Jedis
  • Lettuce
  • Redisson

Spring 对Redis 客户端进行了整合,提供了 Spring Data Redis, 在Spring Boot项目中还提供了对应的Starter,即spring-boot-starter-data-redis
Jedis:

  <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.8.0</version>
  </dependency>
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

public class JedisTest {

    @Test
    public void testRedis(){
        // 1 获取连接
        Jedis jedis = new Jedis("localhost", 6379);
        // 2 执行具体操作
        jedis.set("username", "wkk");
        // 3 关闭连接
        jedis.close();
    }
}

Spring Data Redis:
Spring Data Redis中提供了一个高度封装的类:RedisTemplate,针对jedis客户端中大量api进行了归类封装,将同一类型操作封装为operation接口,具体分类如下:

  • ValueOperations: 简单K-V操作
  • Setoperations: set类型数据操作
  • ZSetoperations: zset类型数据操作
  • HashOperations:针对map类型的数据操作
  • Listoperations:针对list类型的数据操作
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
spring:
  #redis相关配置
  redis:
    host: localhost
    port: 6379
    database: 0
    jedis:
      #redis连接池子配置
      pool:	
      max-active: 8  #最大连接数
      max-wait: 1ms  #连接池最大阻塞等待时间
      max-idle: 4    #连接池中的最大空闲连接
      min-idle: 0    #连接池中的最小空闲连接
import org.springframework.cache.annotation.CachingConfigurerSupport;
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
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        //配置Redis序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());;
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(connectionFactory);
        return redisTemplate;
    }
}
import com.kui.reggie.ReggieApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = ReggieApplication.class)
@RunWith(SpringRunner.class)
public class SpringDataRedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void testString() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("city", "LanZhou");

    }
}

缓存运用思路

一般用于热点数据,即频繁访问的数据,也常用于验证码,可以设置失效时间。
注意在数据库更新/删除/新增数据时,要对缓存进行相应的清空,以保证数据显示的一致。

0

评论区