redis系列(四)-- Spring Boot之Redis

#Spring Boot使用redis
##1.Redis 簡介
Redis 是完全開源免費的,遵守BSD協議,是一個高性能的key-value數據庫。
Redis 與其他 key - value 緩存產品有以下三個特點

  1. Redis支持數據的持久化,可以將內存中的數據保存在磁盤中,重啓的時候可以再次加載進行使用。
  2. Redis不僅僅支持簡單的key-value類型的數據,同時還提供list,set,zset,hash等數據結構的存儲。
  3. Redis支持數據的備份,即master-slave模式的數據備份。

##2.新建項目
新建spring boot項目,具體請參考:
##3.添加依賴
spring boot和redis的整合需要添加redis相關依賴,具體如下:

<!--添加redis支持-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

##4.添加配置信息
在application.properties中配置如下:

########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000

##5.編寫代碼
1、創建model

package com.crazyang.domain;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author crazyang
 * @Desciption:
 * @Date 2018-6-13 9:14
 */
public class RedisEntity implements Serializable {


    private static final long serialVersionUId = 1L;

    private String id;
    private String name;

    public RedisEntity(){
        super();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

2、創建RedisService

package com.crazyang.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author crazyang
 * @Desciption:
 * @Date 2018-6-13 9:15
 */
@Service
public class RedisService {
    
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    
    @Resource(name = "stringRedisTemplate")
    ValueOperations<String,String> valOpsStr;
    
    @Autowired
    RedisTemplate<Object,Object> redisTemplate;
    
    @Resource(name = "redisTemplate")
    ValueOperations<Object,Object> valOpsObj;

    /**
     * 根據指定的key獲取緩存
     * @param key
     * @return
     */
    public String getStr(String key){
        return valOpsStr.get(key);
    }

    /**
     * 設置緩存
     * @param key
     * @param val
     */
    public void setStr(String key,String val){
        valOpsStr.set(key,val);
    }

    /**
     * 刪除指定key
     * @param key
     */
    public void del(String key){
        stringRedisTemplate.delete(key);
    }

    /**
     * 根據指定o獲取Object
     * @param o
     * @return
     */
    public Object getObj(Object o){
        return valOpsObj.get(o);
    }

    /**
     * 設置obj緩存
     * @param o1
     * @param o2
     */
    public void setObj(Object o1,Object o2){
        valOpsObj.set(o1,o2);
    }

    /**
     * 刪除obj緩存
     * @param o
     */
    public void delObj(Object o){
        redisTemplate.delete(o);
    }
    
}

3、創建RedisController

package com.crazyang.controller;

import com.crazyang.domain.RedisEntity;
import com.crazyang.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author crazyang
 * @Desciption:
 * @Date 2018-6-13 9:46
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/setStr")
    public String setStr(String key, String val) {
        try {
            redisService.setStr(key, val);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    @RequestMapping("/getStr")
    public String getStr(String key) {
        return redisService.getStr(key);
    }
    
    @RequestMapping("/delStr")
    public String delStr(String key){
        try {
            redisService.del(key); 
            return "delete success";
        }catch (Exception e){
            e.printStackTrace();
            return "delete false";
        }
    }
    
    @RequestMapping("/setObj")
    public String setObj(String key, RedisEntity o2){
        try {
            redisService.setObj(key,o2);
            return "setObj success";
        }catch (Exception e){
            e.printStackTrace();
            return "setObj false";
        }
    }
    
    @RequestMapping("/getObj")
    public Object getObj(String key){
        return redisService.getObj(key);
    }
    
    @RequestMapping("/delObj")
    public String delObj(String key){
        try {
            redisService.delObj(key);
            return "delObj success";
        }catch (Exception e){
            e.printStackTrace();
            return "delObj false";
        }
    }
}

##6.運行項目
點擊運行*Application,進入瀏覽器後進行測試:
1.StringRedisTemplate

http://127.0.0.1:8080/redis/setStr?key=t1&val=this is t1

http://127.0.0.1:8080/redis/getStr?key=t1

http://127.0.0.1:8080/redis/delStr?key=t1

2.RedisTemplate

http://127.0.0.1:8080/redis/setObj?key=t1&id=1&name=test

http://127.0.0.1:8080/redis/getObj?key=t1

http://127.0.0.1:8080/redis/delObj?key=t1

運行部分截圖如下所示:
1、設置字符串
這裏寫圖片描述
2、提取字符串
這裏寫圖片描述
3、刪除字符串
這裏寫圖片描述
4、提取字符串
這裏寫圖片描述

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