键值数据库Redis——Windows环境下载安装+命令行基本操作+Java操纵Redis


前言

善始者繁多,克终者盖寡。

按照存储类型进行划分,Redis属于键值数据库的一种,其他常见的数据库如下图所示。Redis是基于内存的数据库,所有在数据读写方面它比mysql、mongodb等数据库要快,当数据库中数据量足够巨大时,系统的性能也会受到影响,所以通常情况下Redis数据库中的键都会有一个过期时间(Time To Live),与之相对的为了让数据更长久的保存下来,Redis需要进行数据持久化操作,具体来说有RDB和AOF两种方式,RDB是Redis默认持久化方式,可以理解为定期持久化,AOF类似于实时持久化。因为Redis数据库的数据读写速度快,所有它常常被用在缓存、计数器(秒杀功能)、消息队列上。
在这里插入图片描述

一、下载与安装(Windows环境)

Windows环境下下载Redis主要有两种渠道:官方下载和社区下载。推荐使用社区下载!!!

Redis官方发布的Linux和其他平台的Redis最新版本为7.2,但是!Windows版本还停留在3.x版本,据说官方已不再更新和维护Windows版本,也就是从官方渠道只能下载3.x的Windos版Redis。

第三方开发团队提供了更新的Windows版Redis,目前最新版本为5.0.14.1。

Windows版的Redis文件结构非常简单,将压缩包解压后可得到下图文件。

在这里插入图片描述

各程序功能描述如下图所示。使用Redis的第一步操作时启动Redis服务器,第二步是启动Redis客户端(默认地址和端口为127.0.0.1:6379)

在这里插入图片描述

** 检查数据库连接状态 **

Redis服务器启动成功后会显示Reids版本、端口号、进程号、Redis配置文件地址等信息。
在这里插入图片描述

使用“ping”指令可以检查当前客户端与服务器连接情况,返回“PONG”时表明连接没有问题。

ping

在这里插入图片描述

** 查看Redis数据库信息 **

使用“info”指令可以查看当前Redis数据库服务器、客户端、内存使用情况、持久化等信息。

在这里插入图片描述

二、Redis五种数据结构与基本操作

获取所有的key——keys *

keys *
通过该指令可以用来验证指令和操作是否生效。

清空所有的key——flushall

flushall
通过该指令可以清空内存中所有的key。

2.1 字符串操作

  1. 赋值与取值
    set key value [NX/UX]
    get key [NX/UX]
    NX:不覆盖,不存在就自动创建
    XX:覆盖已存在,不存在时不会自动创建

  2. 取值后赋值
    getset key value
    该指令会返回原来key的值。

  3. 批量赋值和取值
    mset key1 value1 key2 value2 key3 value3…
    mget key1 key2 key3

  4. 判断字段是否存在
    exists key

  5. 递增/递减
    incr/decr key
    该指令使用默认步长(1或-1)进行递增和递减
    incrby/decrby key increment/decrement
    该指令可指定步长

  6. 获取字符串
    strlen key
    该指令返回字符串长度。
    getrange key start end
    该指令获取指定范围的字符,可以将end设置为-1,表示获取字符串第start个到最后一个字符。
    setrange key start end
    该指令用于覆盖原来的字符串,从指定位置开始覆盖
    append key value

  7. 删除字段
    del key1 key2 key3…

2.2 散列操作

可以使用散列存储对象。

  1. 赋值与取值
    hset key field value
    hget key field
    hsetNX:不覆盖,不存在就自动创建
    hgetXX:覆盖,不存在不会自动创建
    可以将key理解为对象的名字,例如car,将field理解为对象的属性,例如price、type。
  2. 批量赋值和取值
    hmest key field1 value1 field2 value2
    hmget key field1 field2
    hgetall key
    该指令获取指定散列的所有信息,包括field和value。
    hkeys key
    该指令获取指定散列的所有field,返回的是一个集合(因为field不能重复)。
    hvals key
    该指令获取指定散列的所有field,返回的是一个列表(value可以重复)。
  3. 判断字段是否存在
    hexists key field
  4. 递增/递减
    hincrby key field increment/decrement
    hincrbyfloat key field increment/decrement
    递增递减操作都可以使用递增命令实现(递减就是加上一个负数),在散列中需要指定递增递减的步长。
  5. 获取长度
    hstrlen key field
    该指令返回指定散列某个field的值的长度,例如car的price是500,则返回3。
    hlen key
    该指令返回指定散列field的个数,例如car有price、color、type、city四个field,则返回4。
  6. 删除字段
    hdel key field

2.3 列表操作

Redis中的列表类似于数据结构中的双向链表实现的队列,既可以从两端取元素又可以从两端存元素,并且每个元素还有索引(序号),大大加快了获取指定索引元素的速度。

  1. 添加元素
    lpush/rpush key value1 value2 value3
    lpush表示从依次向左侧添加元素;rpush表示依次向右侧添加元素,序号是从左往右排序的。
  2. 设置指定位置元素
    lset key index value
  3. 弹出元素
    lpop/rpop key
    lpop表示从左侧弹出元素;rpop表示从右侧弹出元素。
    rpoplpush key1 key2
    该指令表示将右侧弹出元素并添加到左侧,返回被弹出的元素。
  4. 获取列表长度
    llen key
  5. 获取指定位置的元素
    lindex key index
    lrange key 0 -1
    该指令表示获取列表所有的元素,-1表示列表的最后一个元素。

2.4 集合操作

  1. 添加元素
    sadd key value1 value2 value3
    集合中不会存在相同元素。
  2. 删除/移动元素
    srem key value1 value2 value3
    smove key1 key2 value1 value2 value3
    该指令表示将集合key1中的value移动到集合key2中。
  3. 获取集合中元素个数
    smembers key
    scard key
  4. 判断元素是否在集合中
    sismember key value
  5. 集合运算
    sinter key1 key2 key3 ##交集
    sunion key1 key2 key3 ##并集
    sdiff key1 key2 key3 ##差集
    sinterstore/sunionstore/sdiffstore store key1 key2 key3
    该指令表示将key1、key2、key3集合的交、并、差操作结果存储在集合store中。

2.5 位图操作

位图是Redis中特别有意思的一种数据结构,它使用二进制数表示数据,常常用于签到次数、点击次数等应用场景中。下图显示了“A”与“a”两个字符的unicode编码和十进制数,接下来在Redis客户端中进行验证。

在这里插入图片描述
在这里插入图片描述

依次取出key1的前8位数值,发现前8位数值与对应unicode编码相同。

在这里插入图片描述

接着将key1的第3位值由0改为1(系统返回值为该位置原来的值),再取出key1,发现key1由“A”变成了“a”。

在这里插入图片描述

  1. 赋值和取值
    setbit key index value
    getbit key index
  2. 位元操作
    bitcount key start end
    该指令统计字符串被设置为1的bit数。
    bitpos key value start end
    该指令获取第一个为0或1的bit位。
    bitop and/or/xor destkey key1 key2
    bitop not key
    上述两条指令表示与、或、异或、非位运算。

三、Java操纵Redis

** Jedis **

Jedis是Java开发中使用最广泛的Redis客户端库之一,它封装了大多数Redis的操作指令,让Java与Redis的交互变得容易。后续操作均借助Jedis完成。

3.1 字符串操作

3.1.1 获取Redis连接

    public static Jedis getRedisConnection(){
        Jedis jedis = new Jedis("localhost",6379);
        return jedis;
    }

3.1.2 赋值与取值

    public static void setAndGet(){
        Jedis connection = new Jedis("localhost",6379);
        connection.set("hello", "redis");
        System.out.println(connection.get("hello"));
        String oldValue = connection.getSet("hello", "jedis");
        System.out.println(oldValue);
        System.out.println(connection.get("hello"));
        connection.close();
    }

3.1.3 截取与追加

    //截取字符串
    public static void subString(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.set("hello", "redis");
        System.out.println(connection.strlen("hello"));
        System.out.println(connection.getrange("hello", 1, 3));
        connection.close();
    }

    //向末尾添加字符串
    public static void appendString(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.set("hello", "redis");
        System.out.println(connection.append("hello", " 123"));
        System.out.println(connection.get("hello"));
        connection.close();
    }

3.1.4 设置过期时间

如前言中所述,Redis是一款基于内存的数据库,当数据量足够庞大时必然影响系统的运行速度,所有为“键”设置过期时间(Time To Live)十分有必要。

    //设置键的过期时间
    public static void setExpire(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.set("hello", "redis");
        //expire中传入的时间单位为“秒”
        connection.expire("hello", 1);
        try {
        	//Thread中传入的时间单位是“毫秒”
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(connection.get("hello"));
        /*
            用于显示键的过期时间
            -1表示键存在但未设置过期时间,意味着永久存在
            -2表示键不存在
            整数n表示当前键剩余过期时间
         */
        System.out.println(connection.ttl("hello"));
        connection.close();
    }

3.2 散列操作

3.2.1 赋值与取值

    public static void setAndGetHash(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.hset("car", "color", "white");
        HashMap<String, String> carMap = new HashMap<>();
        carMap.put("brand", "BMW");
        carMap.put("price", "500");
        connection.hmset("car", carMap);
        System.out.println(connection.hgetAll("car"));
        connection.close();
    }

3.2.2 递增递减操作

    public static void incrementDecrement(){
        Jedis connection = new Jedis("localhost", 6379);
        System.out.println(connection.hgetAll("car"));
        connection.hincrBy("car", "price", 500);
        System.out.println(connection.hgetAll("car"));
        connection.close();
    }

3.2.3 获取所有键和值

    //获取所有的键和值
    public static void getAllKeysAndValues(){
        Jedis connection = new Jedis("localhost", 6379);
        Set<String> keys = connection.hkeys("car");
        for (String key : keys) {
            System.out.println(key);
        }
        List<String> values = connection.hvals("car");
        for (String value : values) {
            System.out.println(value);
        }
        connection.close();
    }

3.3 列表操作

3.3.1 存取数据

    //列表操作之存取数据
    public static void listPushAndPop(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.lpush("fruit","apple","banana");
        connection.rpush("fruit", "orange","grape", "pear");
        //获取列表fruit的长度并输出其中的内容
        while (connection.llen("fruit") > 0){
            System.out.println(connection.lpop("fruit"));
        }
        connection.close();
    }

3.3.2 获取指定位置元素

    //获取列表中指定位置的元素
    public static void getListIndex(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.rpush("fruit", "apple","banana","orange","grape","pear");
        System.out.println(connection.lindex("fruit", 1));
        //此处分隔表示和后面结果相区分
        System.out.println("*************");
        List<String> fruits = connection.lrange("fruit", 2, 4);
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
        connection.close();
    }

3.3.3 删除指定元素

    //删除列表中指定元素
    public static void removeListValue(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.lpush("list","a","b","c","a","a","d");
        //从左往右删除列表中的两个a
        connection.lrem("list", 2, "a");
        //通常情况下-1表示数据结构的最后一个元素
        List<String> list = connection.lrange("list", 0, -1);
        for (String s : list) {
            System.out.println(s);
        }
        connection.close();
    }

3.4 集合操作

3.4.1 存取数据

    //集合操作之存取数据
    public static void addAndRemoveValue(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.sadd("set","a","b","c","a","a","d");
        System.out.println("删除前的元素:");
        Set<String> set = connection.smembers("set");
        for (String s : set) {
            System.out.println(s);
        }
        System.out.println("删除后的元素");
        connection.srem("set", "a");
        set = connection.smembers("set");
        for (String s : set) {
            System.out.println(s);
        }
        connection.close();
    }

3.4.2 集合运算

    //集合的运算
    public static void setOperation(){
        Jedis connection = new Jedis("localhost", 6379);
        //删除redis所有的键值对
        connection.flushAll();

        connection.sadd("fruit", "apple","tomato");
        connection.sadd("vegetable", "tomato","potato", "carrot");

        //求集合的交集
        Set<String> sinter = connection.sinter("fruit", "vegetable");
        System.out.println("交集是:");
        for (String s:sinter){
            System.out.println(s);
        }

        //求集合的并集
        Set<String> sunion = connection.sunion("fruit", "vegetable");
        System.out.println("\n并集是:");
        for (String s:sunion){
            System.out.println(s);
        }

        //求集合的差集fruit减去vegetable
        Set<String> sdiff = connection.sdiff("fruit", "vegetable");
        System.out.println("\n差集是:");
        for (String s:sdiff){
            System.out.println(s);
        }

        connection.close();
    }

四、完整代码

4.1 工具类——RedisUtil

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class RedisUtil {
    public static void main(String[] args) {

    }

    public static Jedis getRedisConnection(){
        Jedis jedis = new Jedis("localhost",6379);
        return jedis;
    }

    //设置、获取字符串的键值
    public static void setAndGet(){
        Jedis connection = new Jedis("localhost",6379);
        connection.set("hello", "redis");
        System.out.println(connection.get("hello"));
        String oldValue = connection.getSet("hello", "jedis");
        System.out.println(oldValue);
        System.out.println(connection.get("hello"));
        connection.close();
    }

    //截取字符串
    public static void subString(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.set("hello", "redis");
        System.out.println(connection.strlen("hello"));
        System.out.println(connection.getrange("hello", 1, 3));
        connection.close();
    }

    //向末尾添加字符串
    public static void appendString(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.set("hello", "redis");
        System.out.println(connection.append("hello", " 123"));
        System.out.println(connection.get("hello"));
        connection.close();
    }

    //设置键的过期时间
    public static void setExpire(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.set("hello", "redis");
        connection.expire("hello", 1);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(connection.get("hello"));
        /*
            用于显示键的过期时间
            -1表示键存在但未设置过期时间,意味着永久存在
            -2表示键不存在
            整数n表示当前键剩余过期时间
         */
        System.out.println(connection.ttl("hello"));
        connection.close();
    }

    //散列的赋值与取值
    public static void setAndGetHash(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.hset("car", "color", "white");
        HashMap<String, String> carMap = new HashMap<>();
        carMap.put("brand", "BMW");
        carMap.put("price", "500");
        connection.hmset("car", carMap);
        System.out.println(connection.hgetAll("car"));
        connection.close();
    }

    //递增递减操作
    public static void incrementDecrement(){
        Jedis connection = new Jedis("localhost", 6379);
        System.out.println(connection.hgetAll("car"));
        connection.hincrBy("car", "price", 500);
        System.out.println(connection.hgetAll("car"));
        connection.close();
    }

    //获取所有的键和值
    public static void getAllKeysAndValues(){
        Jedis connection = new Jedis("localhost", 6379);
        Set<String> keys = connection.hkeys("car");
        for (String key : keys) {
            System.out.println(key);
        }
        List<String> values = connection.hvals("car");
        for (String value : values) {
            System.out.println(value);
        }
        connection.close();
    }

    //列表操作之存取数据
    public static void listPushAndPop(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.lpush("fruit","apple","banana");
        connection.rpush("fruit", "orange","grape", "pear");
        //获取列表fruit的长度并输出其中的内容
        while (connection.llen("fruit") > 0){
            System.out.println(connection.lpop("fruit"));
        }
        connection.close();
    }

    //获取列表中指定位置的元素
    public static void getListIndex(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.rpush("fruit", "apple","banana","orange","grape","pear");
        System.out.println(connection.lindex("fruit", 1));
        //此处分隔表示和后面结果相区分
        System.out.println("*************");
        List<String> fruits = connection.lrange("fruit", 2, 4);
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
        connection.close();
    }

    //删除列表中指定元素
    public static void removeListValue(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.lpush("list","a","b","c","a","a","d");
        //从左往右删除列表中的两个a
        connection.lrem("list", 2, "a");
        //通常情况下-1表示数据结构的最后一个元素
        List<String> list = connection.lrange("list", 0, -1);
        for (String s : list) {
            System.out.println(s);
        }
        connection.close();
    }

    //集合操作之存取数据
    public static void addAndRemoveValue(){
        Jedis connection = new Jedis("localhost", 6379);
        connection.sadd("set","a","b","c","a","a","d");
        System.out.println("删除前的元素:");
        Set<String> set = connection.smembers("set");
        for (String s : set) {
            System.out.println(s);
        }
        System.out.println("删除后的元素");
        connection.srem("set", "a");
        set = connection.smembers("set");
        for (String s : set) {
            System.out.println(s);
        }
        connection.close();
    }

    //集合的运算
    public static void setOperation(){
        Jedis connection = new Jedis("localhost", 6379);
        //删除redis所有的键值对
        connection.flushAll();

        connection.sadd("fruit", "apple","tomato");
        connection.sadd("vegetable", "tomato","potato", "carrot");

        //求集合的交集
        Set<String> sinter = connection.sinter("fruit", "vegetable");
        System.out.println("交集是:");
        for (String s:sinter){
            System.out.println(s);
        }

        //求集合的并集
        Set<String> sunion = connection.sunion("fruit", "vegetable");
        System.out.println("\n并集是:");
        for (String s:sunion){
            System.out.println(s);
        }

        //求集合的差集fruit减去vegetable
        Set<String> sdiff = connection.sdiff("fruit", "vegetable");
        System.out.println("\n差集是:");
        for (String s:sdiff){
            System.out.println(s);
        }

        connection.close();
    }
}

4.2 测试类——RedisUtilTest

测试工具为Junit5,开发环境为IDEA,在Eclipse中测试方法必须以test开头,此处与IDEA有所区别。

import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

import static org.junit.jupiter.api.Assertions.*;

class RedisUtilTest {

    @Test
    void getRedisConnection() {
        Jedis connection = RedisUtil.getRedisConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    void setAndGet() {
        RedisUtil.setAndGet();
    }

    @Test
    void subString() {
        RedisUtil.subString();
    }

    @Test
    void appendString() {
        RedisUtil.appendString();
    }

    @Test
    void setExpire() {
        RedisUtil.setExpire();
    }

    @Test
    void setAndGetHash() {
        RedisUtil.setAndGetHash();
    }

    @Test
    void incrementDecrement() {
        RedisUtil.incrementDecrement();
    }

    @Test
    void getAllKeysAndValues() {
        RedisUtil.getAllKeysAndValues();
    }

    @Test
    void listPushAndPop() {
        RedisUtil.listPushAndPop();
    }

    @Test
    void getListIndex() {
        RedisUtil.getListIndex();
    }

    @Test
    void removeListValue() {
        RedisUtil.removeListValue();
    }

    @Test
    void addAndRemoveValue() {
        RedisUtil.addAndRemoveValue();
    }

    @Test
    void setOperation() {
        RedisUtil.setOperation();
    }
}
  • 24
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的墨菲特

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值