zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Cesium加载Geowebcache切片
    Vue开发--脚手架的搭建
    OpenLayers动态测量距离和面积,并可自定义测量的线样式
    OpenLayers要素拖拽
    改造SuperMap的DrawHandler接口,自定义绘制的图形样式
    Cesium动态绘制实体(点、标注、面、线、圆、矩形)
    ArcMap制图遇到的小问题
    GeoServer 2.15.2版本跨域问题解决方法
    MySQL 8.0 主从同步
    Service__cmd--MySQL安装并连接SQLyog
  • 原文地址:https://www.cnblogs.com/PreachChen/p/8901918.html
Copyright © 2011-2022 走看看