一、Redis

NoSQL:泛指非关系型数据库,英文:Not Only SQL (不仅仅是SQL)

Redis入门

目前使用比较多的,Redis(ke-value数据库,缓存)、MongoDB(海量存储数据)

需求:发送绑定邮件后, 用户会在24小时内,完成邮件绑定, 这时需要保存邮件中**码24小时, 会使用redis技术完成 。

Redis:内存数据库 (key-value存储结构)

1.1、Redis 下载安装

官网下载: http://redis.io/download

使用资料安装包

Redis入门

解压到D盘的software目录下。

Redis入门

使用对应位数操作系统文件夹下面命令启动redis
Redis入门

其中:

redis-server.exe 服务启动程序

redis-cli.exe 客户端命令行工具

redis.conf 服务配置文件

通过redis-server.exe启动服务,默认端口 6379

通过redis-cli.exe 启动客户端工具

Redis入门

表示连接成功!

1.2、Redis注册服务

如果每次启动服务,比较麻烦,能否把redis服务注册到windows上?

Redis入门

  • 注册服务:
redis-server --service-install redis.windows.conf --loglevel verbose
  • 卸载服务:
redis-server --service-uninstall
  • 启动Redis:
redis-server --service-start

停止Redis:

redis-server --service-stop

Redis入门

1.3、Redis基本命令

  • set key value,如果该key存在则进行覆盖操作。

Redis入门

  • get key,根据key获取值

Redis入门

  • KEYS 获取符合规则的key

Redis入门

Redis入门

Redis入门

Redis入门

  • EXISTS 判断一个key是否存在

Redis入门

  • DEL 删除键

Redis入门

  • TYPE 获取key的数据类型

  • Redis入门

  • HELP 帮助命令

Redis入门

1.4、Redis数据类型之字符串

Redis入门

  • INCR 递增

Redis入门

  • INCRBY 递增指定的整数

Redis入门

  • DECR 递减

Redis入门

  • APPEND 追加

Redis入门

  • STRLEN 获取字符串的长度

Redis入门

1.5、Redis之生存时间

Redis入门

  • 清除生存时间

Redis入门

剩余生成时间:

-1:永久生效

-2:键不存在

1.6、Jedis 使用和redis图形界面工具

2.6.1 jedis的基本使用

通过java程序操作redis, 使用jedis 工具。

网址: https://github.com/xetorthio/jedis

Redis入门

根据上面的案例编写测试代码:

第一步:在bos-parent中导入坐标

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
  • 使用jedis连接池

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        //创建连接池config对象
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(10);
        config.setMaxTotal(50);
        //创建jedis连接池
        JedisPool pool = new JedisPool(config , "127.0.0.1",  6379 );
        //获取连接
        Jedis jedis = pool.getResource();
        jedis.set("itheima12", "好样的,你竟然找了男的做女朋友");
    
        String result = jedis.get("itheima12");
        System.out.println(result);
        jedis.close();
    }
    
  • 设置key有效周期

    //jedis.set("school", "传智大学");
    jedis.setex("school", 30 , "传智大学");//30:表示30秒钟后过期
    
  • 使用redis-cli.exe查看测试

Redis入门

1.6.2 安装redis图形化界面

类似SQLyog操作mysql数据库

Redis入门

打开:“RedisDesktopManager”添加连接

Redis入门

添加信息

Redis入门

查下key和value

Redis入门

其中:TTL 是redis的key 有效时间,显示- ,说明没有设置key的有效期 ,那么如何设置key的有效期呢?

Redis入门

1.7、Spring Data Redis 使用

SprignBoot Data家族是Spring中专业的跟数据打交道的技术。

Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在Java中的redis- client。spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis自然是针对Redis的独立封装了,而且Spring Data Redis是对redis客户端(如jedis)的高度封装,支持多种客户端。

Redis入门

官网: http://projects.spring.io/spring-data-redis/

2、SpringBoot整合spring data Redis

  • common-parent:pom.xml中添加redis坐标

    <!-- 配置使用redis启动器 -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    
  • 配置信息

    ########################################################
    ###REDIS (RedisProperties) redis基本配置;
    ########################################################
    # database name
    spring.redis.database=0
    # server host1
    spring.redis.host=127.0.0.1
    # server password
    #spring.redis.password=
    #connection port
    spring.redis.port=6379
    # pool settings ...
    spring.redis.jedis.pool.max-idle=8
    spring.redis.jedis.pool.min-idle=0
    spring.redis.jedis.pool.max-active=8
    spring.redis.jedis.pool.max-wait=-1
    # name of Redis server
    #spring.redis.sentinel.master=
    # comma-separated list of host:port pairs
    #spring.redis.sentinel.nodes=
    
  • 当SpringBoot整合SpringDataRedis之后,我们使用的对象的是RedisTemplate

相关文章:

  • 2022-01-12
猜你喜欢
  • 2021-07-04
  • 2021-10-23
相关资源
相似解决方案