• SpringBoot整合Redis


    Redis介绍

    Redis是一个完全开源免费的, 是一个高性能的key-value数据库。

    Redis 与其他 key - value 缓存产品有以下三个特点:

    • Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。

    • Redis不仅仅支持简单的key-value类型的数据,同时还提供结构list,set,zset,hash等数据的存储。

    • Redis支持数据的备份,即master-slave模式的数据备份。

    Redis优势

    • 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。

    • 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。

    • 原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。

    • 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。

    Redis支持五种数据类型:

    string(字符串)、list(列表)、set(集合)、hash(哈希)、zset(sorted set有序集合)

    安装Redis

    redis没有官方版的windows版本,但是GitHub有开源组织维护的版本
    redis windows64位版本
    启动命令redis-server.exe redis.windows.conf

    maven依赖

    1 <!--redis-->
    2 <dependency>
    3     <groupId>org.springframework.boot</groupId>
    4     <artifactId>spring-boot-starter-redis</artifactId>
    5 </dependency>
    6 <dependency>
    7     <groupId>org.springframework.boot</groupId>
    8     <artifactId>spring-boot-starter-data-redis</artifactId>
    9 </dependency>

    properties配置

    1 #端口号
    2 server.port=8066
    3 
    4 #redis配置
    5 spring.redis.database=1
    6 spring.redis.host=localhost
    7 spring.redis.port=6379
    8 spring.redis.timeout=3000

    单元测试类测试存取

     1 package com.example.inchlifc;
     2 
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.boot.test.context.SpringBootTest;
     7 import org.springframework.data.redis.core.StringRedisTemplate;
     8 import org.springframework.test.context.junit4.SpringRunner;
     9 
    10 @RunWith(SpringRunner.class)
    11 @SpringBootTest
    12 public class InchlifcApplicationTests {
    13 
    14    @Autowired
    15    StringRedisTemplate stringRedisTemplate;
    16    @Test
    17    public void contextLoads() {
    18       //保存字符串
    19       stringRedisTemplate.opsForValue().set("redisTest", "小小渔夫");
    20    }
    21 
    22 }

    测试结果

    微信截图_20180412163841.png

    微信截图_20180412163853.png

  • 相关阅读:
    Node.js 基于 ursa 模块的 RSA 加密解密(已与IOS,Android实现加密通信)
    Mac安装SecureCRT
    lodash underscore 对象数组排序 sortBy 小记
    Spring5--@Indexed注解加快启动速度
    spring的元注解
    RxJava原理学习
    RabbitMQ知识点一
    消息中间件知识点
    Android打开doc、xlsx、ppt等office文档解决方案
    IT观察】网络通信、图片显示、数据库操作……Android程序员如何利用开源框架
  • 原文地址:https://www.cnblogs.com/PreachChen/p/8901918.html
Copyright © 2020-2023  润新知