springboot+redis簡單學習記錄

Redis

什麼是redis?

Redis 是一個開源(BSD許可)的,內存中的數據結構存儲系統,它可以用作數據庫、緩存和消息中間件。 它支持多種類型的數據結構,如 字符串(strings)散列(hashes)列表(lists)集合(sets)有序集合(sorted sets) 與範圍查詢, bitmapshyperloglogs地理空間(geospatial) 索引半徑查詢。 Redis 內置了 複製(replication)LUA腳本(Lua scripting)LRU驅動事件(LRU eviction)事務(transactions) 和不同級別的 磁盤持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)。

Redis 的功能:

用來充當臨時數據庫,或者是中間件,暫時沒有用的其他功能

第一步:在windows10 安裝Redis

下載安裝包:https://github.com/microsoftarchive/redis/releases

選擇zip壓縮包下載

下載完成後將壓縮包解壓在D:\Program Files\

將根目錄重命名爲Redis

進入dos界面,切換根目錄下,執行:

redis-server.exe redis.windows.conf

出現如下圖成功:

 

新開dos界面,切換目錄到rides下,執行:

redis-cli.exe -h 127.0.0.1 -p 6379

設置鍵值對:

set myKey abc

獲取鍵值:

get mykey

搭建springboot+rides:

創建maven項目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>rides</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>rides</name>
    <description>Demo project for Spring Boot</description>
​
    <properties>
        <java.version>1.8</java.version>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
​
</project>
​

創建yml:

server:
  port: 8080
# Redis數據庫索引(默認爲0)
# Redis服務器地址
# Redis服務器連接端口
# 連接池最大連接數(使用負值表示沒有限制)
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
# 連接池中的最大空閒連接
# 連接池中的最小空閒連接
# 連接超時時間(毫秒)
spring:
  redis:
      database: 0
      host: 127.0.0.1
      port: 6379
      jedis:
        pool:
            max-active: 20
            max-wait: -1
            max-idle: 10
            min-idle: 0
        timeout: 1000
​

創建 RedisService.java

package com.example.rides.service;
​
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
​
import java.util.concurrent.TimeUnit;
​
/**
 * @author:
 * @date: 2020/1/6
 * @description:
 **/
@Service
public class RidesService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired(required = false)
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisSerializer stringSerializer = new StringRedisSerializer();//序列化爲String
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);//序列化爲Json
       
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        
        this.redisTemplate = redisTemplate;
    }
​
    public String setMsg(String key,String msg) {
        redisTemplate.opsForValue().set(key,msg);
        return "success";
    }
    public String getMsg(String key) {
        return (String) redisTemplate.opsForValue().get(key);
    }
​
    /**
     * 設置寫入的有效時間
     */
    public String setMsgTime(String key,String msg,Long expiredTime){
        redisTemplate.opsForValue().set(key,msg,expiredTime,TimeUnit.SECONDS);// 這裏設置的時間單位爲秒
        return   "success";
    }
​
}

創建controller:Redishandier.java

package com.example.rides.controller;
​
import com.example.rides.service.RidesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
​
/**
 * @author:
 * @date: 2020/1/6
 * @description:
 **/
@RestController
@RequestMapping("/msg")
public class RidesHandler {
    @Autowired
    RidesService ridesService;
​
    @GetMapping("/set")
    public String setMsg(@RequestParam(value = "key") String key, @RequestParam(value = "msg") String msg){
        return ridesService.setMsg(key,msg);
    }
    @GetMapping("/setMsgTime")
    public String setMsg(@RequestParam(value = "key") String key, @RequestParam(value = "msg") String msg,@RequestParam(value = "expiredTime") Long expiredTime){
        return ridesService.setMsgTime(key,msg,expiredTime);
    }
    @GetMapping("/get")
    public String getMsg(@RequestParam(value = "key") String key){
        return ridesService.getMsg(key);
    }
}

啓動項目,然後用postMan請求set,請求成功後,到redis 查看,或者get一下。

這裏我使用的是redis Desktop Manager 用來連接redis.

下面是連接成功的示例

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章